gpt4 book ai didi

java - 无法从 ActionPerformed 调用构造函数组件

转载 作者:行者123 更新时间:2023-12-01 09:20:18 25 4
gpt4 key购买 nike

执行bottomLabel.setVisible(true);时,我遇到 Unresolved 编译问题:

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)

最佳答案

bottomLabel 仅在构造函数内可见,因为它未在类级别声明。

在类级别声明它(就像您对 xlowhigh 等所做的那样)以使其正常工作:

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

关于java - 无法从 ActionPerformed 调用构造函数组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40208409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com