【发布时间】:2014-02-08 04:17:01
【问题描述】:
如何通过按下一个按钮来禁用按钮,并且当按下的按钮分配给的任务已经完成时,我希望启用所有按钮。例如;
UpButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
DownButton.setEnabled(false);
LeftButton.setEnabled(false);
RightButton.setEnabled(false);
System.out.printline("Up Button");
}
});
DownButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
UpButton.setEnabled(false);
LeftButton.setEnabled(false);
RightButton.setEnabled(false);
System.out.printline("Down Button");
}
});
LeftButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
DownButton.setEnabled(false);
UpButton.setEnabled(false);
RightButton.setEnabled(false);
System.out.printline("Left Button");
}
});
RightButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
DownButton.setEnabled(false);
LeftButton.setEnabled(false);
UpButton.setEnabled(false);
System.out.printline("Right Button");
}
});
我尝试过这样做,但它不起作用。我想要的是,例如,我的 Upbutton 正在打印出来(“Up button”),所以当用户按下这个按钮时,我希望禁用所有其他按钮,直到它完成了它应该执行的命令,在这种情况下打印出一个文本,但稍后我将添加诸如添加两个用户输入之类的内容,例如我将有一个按钮,要求用户输入数字 1,然后输入数字 2,然后计算机会将它们添加起来,在此过程中,我希望所有按钮都被禁用,期待已被点击的按钮,直到用户已给出所有数字,计算机已给出输出。
我希望我已经正确解释了自己,如果没有,请告诉我,我会尽力提供更多信息。提前致谢。
【问题讨论】:
-
只有一个问题,请问这个问题是关于......
-
在 dot 之后无法使用代码标签(永远不要举手,也许有人会使用 html 格式这样做)
-
使用 JRadioButtons 添加到 ButtonGroup,使用 JToggleButtons 可以轻松实现
-
您需要了解
SwingWorkerAPI。您可以禁用按钮并启动工作人员。完成后,它可以重新启用按钮。 -
请学习常见的Java naming conventions(特别是用于名称的大小写)用于类、方法和属性名称并一致地使用它们。
标签: java eclipse swing button awt