[英]Disable a JButton from a listener that's in a separate class
I'm writing a pretty big class and don't want to post it here. 我正在写一个相当大的类,不想在这里发布。 The question is the following, how do I refer to the button that was pressed in the constructor of a different class?
问题如下,我该如何引用在其他类的构造函数中按下的按钮? Let's say, I want to disable it after some actions in the listener.
假设我想在侦听器中执行一些操作后将其禁用。 If the listener were anonymus or were an inner class of the SomeClass, I would just use the name of the variable like this:
如果侦听器是匿名者或SomeClass的内部类,我将使用变量名称,如下所示:
button.setEnabled(false);
But how can I do it when my listener is a separate class? 但是,当我的听众是一个单独的班级时,我该怎么办? Tried using e.getModifiers().setEnabled(false) and e.getSource().setEnabled(false), didn't work.
使用e.getModifiers()。setEnabled(false)和e.getSource()。setEnabled(false)尝试过,不起作用。
public class SomeClass extends JPanel {
private JButton button = new JButton("Button");
public SomeClass() {
button.setActionCommand("button");
button.addActionListener(new ButtonListener());
}
public static void main(String[] args) {
// TODO code application logic here
}
}
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String src = e.getActionCommand();
if (src.equals("button")) {
//some actions here
//then
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.