所以我总共有 3 个按钮。禁用按钮我希望它在单击时禁用其他 2 个按钮。一旦它们被禁用,禁用按钮文本将被设置为启用。现在禁用按钮是一个启用按钮,可以启用另外 2 个按钮。我在禁用按钮时没有遇到问题。我在启用它们时遇到问题。 这就是我遇到麻烦的地方。
btnOk.addActionListener(this);
btnCancel.addActionListener(this);
btnDisable.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// set both buttons
btnOk.setText("ok");
btnCancel.setText("cancel");
btnDisable.setText("disable");
JButton clickedButton = (JButton) e.getSource();
if (btnDisable == clickedButton) {
clickedButton.setText("enable");
} else {
clickedButton.setText("Clicked!");
}
if (btnDisable == clickedButton) {
btnOk.setText("enable");
btnOk.setVisible(false);
btnCancel.setVisible(false);
disable = true;}
}
public void actionPerformed2(ActionEvent e) {
}
最佳答案
按钮具有 isEnabled() 方法。你可以做
if (button.isEnabled()){
button.setEnabled(false);
} else {
button.setEnabled(true);
关于JAVA。一个按钮可以禁用其他两个按钮,然后单击即可重新启用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60020629/