Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to get this code to run what do I need to do to fix it? import java.awt.e

ID: 3638472 • Letter: T

Question

Trying to get this code to run what do I need to do to fix it?


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

class Dice extends JLabel {
private int value;

Dice() {
super();
setValue(0);

addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
setValue(getValue() + 1);
}
});
}

public int getValue() {
return value;
}

void setValue(int value) {
this.value = value;
switch (value) {
case 0:
this.setIcon(getImage("images\one.GIF"));
break;
case 1:
this.setIcon(getImage("images\two.GIF"));
break;
case 2:
this.setIcon(getImage("images\three.GIF"));
break;
case 3:
this.setIcon(getImage("images\four.GIF"));
break;
case 4:
this.setIcon(getImage("images\five.GIF"));
break;
case 5:
this.setIcon(getImage("images\six.GIF"));
break;
}
}


ImageIcon getImage(String name) {
Object o = getClass().getResource(name);
return new ImageIcon(getClass().getResource(name));
}

public static void main(String[] a) {
JPanel panel = new JPanel();
Dice dice1 = new Dice();

dice1.setValue(2);

panel.add(dice1);

JFrame app = new JFrame();
app.getContentPane().add(panel);
app.setLocation(300, 200);
app.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
app.pack();
app.show();
}

}

Explanation / Answer

Dice() { super(); setValue(0); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { setValue(getValue() + 1); } }); } try looking at this part out of the whole code you gave this part seems the most iffy, right now it looks like you're trying to define a method in your constructor, call on some method that doesn't seem defined and the ");" looks sort of awkward

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote