What is the result of running the following program? import java.awt.event.Actio
ID: 3917417 • Letter: W
Question
What is the result of running the following program?
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class SimpleEvents {
private int changeValue = -1;
public static void main(String[] args) {
new NoSwitch();
}
public SimpleEvents() {
WidgetViewer wv = new WidgetViewer();
JButton sophie = new JButton("5");
JButton jack = new JButton("-5");
sophie.addActionListener(new ButtonHandler(jack));
jack.addActionListener(new ButtonHandler(sophie));
wv.add(sophie, 10, 50, 70, 20);
wv.add(jack, 10, 80, 70, 20);
}
class ButtonHandler extends WidgetViewerActionEvent {
private JButton buttonToChange;
public ButtonHandler(JButton myButton) {
buttonToChange = myButton;
}
@Override public void actionPerformed(ActionEvent e) {
String s = buttonToChange.getText();
int val = Integer.parseInt(s);
val += changeValue;
buttonToChange.setText(String.valueOf(val));
}
}
}
The program will compile, but will have a runtime error when a button is clicked because JButton is immutable.
?
When the user clicks one button, the other button is modified.
?
The program will not compile because ButtonHandler must be defined above its use.
?
The program will compile, but will have a runtime error when a button is clicked because an event handler for one button cannot modify a different button.
?
The program will not compile because an event handler can have only an actionPerformed method.
?
When the user clicks a button, it is modified.
?
The program will compile, but will have a runtime error because only one instance of an event handler class can be instantiated.
?
The program will not compile because no class except SimpleEvents can access its private instance variable.
Explanation / Answer
The result of the following program would be,
The program will compile, but will have a runtime error because only one instance of an event handler class can be instantiated.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.