[英]JButton ActionListener not responding
I couldn't find the answer anywhere else online, so I came here. 我在网上其他任何地方都找不到答案,所以我来到了这里。 I apologize in advance if the mistake in my code is very obvious; 如果我的代码中的错误非常明显,我会提前道歉; I'm still quite new to java swing. 我对java swing还是很陌生。 Here's what's going on: I have created a JButton named toggleElevators
, and I want it to change text when clicked. 这是怎么回事:我创建了一个名为toggleElevators
的JButton,我希望它在单击时可以更改文本。 I have already created an ActionListener and added it to toggleElevators
. 我已经创建了一个ActionListener并将其添加到toggleElevators
。 All I want right now is for the JButton to change text when clicked from Click me
to Clicked
. 我现在想要的只是让JButton在单击Click me
到Clicked
时更改文本。
First, here's a picture of what the JFrame looks like when executed: 首先,这是执行JFrame时的外观图:
NOTE: There is a third class, but it is purely for drawing the picture on the left. 注意:有一个第三类,但这纯粹是为了在左侧绘制图片。 It has nothing to do with the GridLayout or the JButton. 它与GridLayout或JButton无关。
Run
class (created frame and adds toggleElevators
JButton: Run
类(创建框架并添加toggleElevators
:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
public class Run extends Input{
Input i = new Input();
public static void main(String[] args) {
new Run();
}
public Run() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Elevators");
frame.setLayout(new GridLayout(0, 3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Elevators(Color.MAGENTA, true));
frame.add(new Elevators(Color.ORANGE, false));
frame.setSize(800,600);
frame.setResizable(false);
frame.getContentPane().add(toggleElevators); //adds toggleElevators button to JFrame
i.addButtonListeners(); //calls method defined in Input class, which adds the ActionListener to the toggleElevators button
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Input
class (creates toggleElevators
JButton and its ActionListener): Input
类(创建toggleElevators
JButton及其ActionListener):
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Input {
JButton toggleElevators = new JButton("Click me.");
public void addButtonListeners() {
toggleElevators.addActionListener(new toggleElevatorsListener());
}
class toggleElevatorsListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
toggleElevators.setText("Clicked.");
System.out.println("ActionListener called."); //I know the ActionListener is not being called because this line is not being printed out in the console
}
}
}
Your Run class extends Input, but also HAS an Input named i
. 您的Run类扩展Input,但还具有一个名为i
的Input。 You're adding this.toggleElevators
to the frame, but you're adding a listener to i.toggleElevators
. 您将this.toggleElevators
添加到框架中,但您将监听器添加到i.toggleElevators
。
Remove the i
field from your class. 从您的班级中删除i
字段。 I would also forget completely about defining and extending an Input class. 我也会完全忘记定义和扩展Input类。 It doesn't serve any purpose, and seems to confuse more than help you. 它没有任何作用,似乎比帮助您更令人困惑。
You create a new Input
in your Run
class, while the Run
class also extends Input
. 您可以在Run
类中创建一个新的Input
,而Run
类还可以扩展Input
。
When you call i.addButtonListeners();
调用i.addButtonListeners();
the action listeners are added on the toggleElevators
from i
and not on the toggleElevators
you inherited from the Input
class. 操作侦听器将添加到i
的toggleElevators
,而不是您从Input
类继承的toggleElevators
。
Try addButtonListeners()
. 尝试addButtonListeners()
。
Your Run
class extends Input
. 您的Run
类扩展了Input
。 Therefore it has its own toggleElevators
which is the one it sets in the frame. 因此,它有自己的toggleElevators
,它是在框架中设置的那个。 However, i
has is own toggleElevators
where it sets the event listeners. 但是, i
有自己的toggleElevators
,用于设置事件侦听器。 So they are not set on the one in the frame but on one that never gets used. 因此,它们不是设置在框架中的一个上,而是设置在永不使用的框架上。
You can simply delete the i
object. 您可以简单地删除i
对象。 As Run
extends Input
, it can call the method directly, and then the listener will be added to its own toggleElevators
. 当Run
扩展Input
,它可以直接调用该方法,然后将侦听器添加到其自己的toggleElevators
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.