执行bottomLabel.setVisible(true);
时,我得到一个未解决的编译问题:
The type Assignment1 must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
就像我应该创建一个新变量,因为它不能从构造函数中读取。我的错误在哪里?这是尽可能简化的代码:
public class Assignment1 extends JFrame implements ActionListener{
//declare variables
int x= 101;
int low = 0;
int high = 100;
int guess = (high + low) / 2;
int counter = 0;
private static final long serialVersionUID = 1L;
//main method
//...some code...
//constructor
public Assignment1(){
//...some code...
//declare buttons
JButton correct = new JButton("correct!");
//add buttons
//...some code...
//declare TextField and Labels
JTextField numberTextField = new JTextField(20);
JLabel topLabel = new JLabel("T");
JLabel bottomLabel = new JLabel("G");
//add TextField and Labels and position them on the layout
//...some code...
bottomLabel.setBounds(110, 300, 400, 20);
add(bottomLabel);
bottomLabel.setVisible(false);
//add ActionListener to each button
//...some code...
correct.addActionListener(this);
}
@Override
//define ActionPerformed when an Event is parsed
public void actionPerformed(ActionEvent e) {
String buttonClicked = e.getActionCommand();
if(buttonClicked.equals("Yes, correct!")){
System.out.println("correct");
bottomLabel.setVisible(true);
}
}
}
这是完整的堆栈跟踪:
at Assignment1.Assignment1.actionPerformed(Assignment1.java:12)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:1)
bottomLabel
仅在构造函数中可见,因为它未在类级别声明。
在课程级别声明(与x
,low
,high
等一样)以使其有效:
public class Assignment1 extends JFrame implements ActionListener {
// declare variables
int x = 101;
int low = 0;
int high = 100;
int guess = (high + low) / 2;
int counter = 0;
JLabel bottomLabel; // <==
public Assignment1() {
// [...] other assignments
bottomLabel = new JLabel("Game Over, your number is NN, i got it in N times. Wanna play again?");
// [...] rest of class