java 如何在java上制作开/关按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14866994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make an on/off button on java?
提问by Danilo Bambi
I need a button that, when pressed, enables all the other buttons, and changes the name of a label from "Off" to "On", and when pressed again, disables all the buttons and turns the switch to "off" back again, like an on/off switch. The thing is, I can "turn it" on, but I can't turn it back off.
我需要一个按钮,当按下时,启用所有其他按钮,并将标签名称从“关”更改为“开”,再次按下时,禁用所有按钮并将开关再次转回“关” ,就像一个开/关开关。问题是,我可以“打开”它,但不能将其关闭。
回答by Hovercraft Full Of Eels
If Swing, then perhaps you will want to place the buttons that you wish to control into an array or an ArrayList<AbstractButton>
. That way the ActionListener of the control button can simply iterate through the array or the collection with a for loop, calling setEnabled(true)
or false on the buttons. Consider making the controlling button be a JCheckBox or a JToggleButton.
如果是 Swing,那么您可能希望将希望控制的按钮放入数组或ArrayList<AbstractButton>
. 这样,控制按钮的 ActionListener 可以简单地使用 for 循环遍历数组或集合,setEnabled(true)
在按钮上调用或 false。考虑将控制按钮设为 JCheckBox 或 JToggleButton。
Note, if you use a JToggleButton, then add an ItemListener to it. If you do so, there's no need to use booleans. Just check the state of the ItemEvent passed into the ItemListener's itemStateChanged method. If the getStateChanged()
returns ItemEvent.SELECTED, then iterate through your JButton collection enabling all buttons. If it returns ItemEvent.DESELECTED, do the opposite.
请注意,如果您使用 JToggleButton,则向其添加 ItemListener。如果这样做,则无需使用布尔值。只需检查传递给 ItemListener 的 itemStateChanged 方法的 ItemEvent 的状态。如果getStateChanged()
返回 ItemEvent.SELECTED,则遍历您的 JButton 集合以启用所有按钮。如果它返回 ItemEvent.DESELECTED,则执行相反的操作。
Also note as per Byron Hawkins's comment:
另请注意拜伦霍金斯的评论:
You may want to consider that the
ItemListener
will receive events when the button is programmatically toggled, and also when the user toggles the button. TheActionListener
only gets fired on input from the human user. I've often had bugs because I picked the wrong one.
您可能需要考虑
ItemListener
在以编程方式切换按钮时以及用户切换按钮时将接收事件。该ActionListener
只拿到输入来自人类用户解雇。我经常遇到错误,因为我选错了。
回答by Byron Hawkins
If your button is pressed down and won't pop back up, chances are you have overridden a method in JToggleButton
without calling super
's version of it. Instead of overriding methods, create an ActionListener
and use addActionListener()
to attach to the button. When your listener is notified of button actions, check whether the toggle button is up or down and setEnabled()
on the other buttons accordingly.
如果您的按钮被按下并且不会弹回,那么您很可能在JToggleButton
没有调用super
它的版本的情况下覆盖了一个方法。不是重写方法,而是创建一个ActionListener
并用于addActionListener()
附加到按钮。当您的侦听器收到按钮操作通知时,请检查切换按钮是向上还是向下以及setEnabled()
其他按钮上的相应按钮。
回答by goravine
try use this simple code, use the variable as the flag
尝试使用这个简单的代码,使用变量作为标志
public int status = 0; //0 = on, 1=off
public void button_clicked()
{
//on button clicked
if(status == 0)
{
//logic here
status = 1;
buttonname.setText("Off");
}
//off button clicked
else if(status == 1)
{
//logic here
status = 0;
buttonname.setText("On");
}
}
回答by amarunowski
you'll need a boolean to represent the state of the button.
您需要一个布尔值来表示按钮的状态。
In other words, when your button is off (your boolean variable is false), from your onClick listener, you'll call a method "turnButtonOn()" or something of that nature.
换句话说,当您的按钮关闭时(您的布尔变量为 false),您将通过 onClick 侦听器调用方法“turnButtonOn()”或类似的方法。
If your boolean variable is true, then you'll call a method turnButtonOff()
如果您的布尔变量为真,那么您将调用方法 turnButtonOff()
public void onClick() {
if(buttonOn){
turnOff();
}
else {
turnOn();
}
buttonOn = !buttonOn;
}