Java 如何检查 JButton 是否被按下?如果 isEnable() 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20799131/
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 can I check that JButton is pressed? If the isEnable() is not work?
提问by Dexter Moregan
How can I check that JButton is pressed? I know that there is a method that its name is "isEnabled"
如何检查 JButton 是否被按下?我知道有一种方法,它的名字是“isEnabled”
So I try to write a code to test.
所以我试着写一个代码来测试。
- this code have 2 Jbuttons which are "Add" Button and "Checkout" button.
- the code will show the "Add button is pressed" message when I press "Checkout" button after I press "Add" button but If the "Add" Button is not pressed before the "Checkout" Button is pressed, the code will show the "Add Button is not pressed" message.
- 此代码有 2 个 Jbutton,分别是“添加”按钮和“结帐”按钮。
- 当我按下“添加”按钮后按下“结帐”按钮时,代码将显示“按下添加按钮”消息,但如果在按下“结帐”按钮之前未按下“添加”按钮,则代码将显示“未按下添加按钮”消息。
Here the code:
这里的代码:
final JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
panel.add(btnAdd);
JButton btnConfirm = new JButton("Check Out");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnAdd.isEnabled()) {
System.out.println("Add Button is pressed");
}
if (!btnAdd.isEnabled()) {
System.out.println("Add Button is not pressed");
}
}
});
When I run this code,the code give only the " Add button is pressed" although I didn't press the "Add" Button. Why does it occur like that?
当我运行此代码时,尽管我没有按下“添加”按钮,但代码仅给出“按下添加按钮”。为什么会这样?
回答by Sage
JButton
has a modelwhich answers these question:
JButton
有一个模型可以回答这些问题:
isArmed()
,isPressed()
,isRollOVer()
isArmed()
,isPressed()
,isRollOVer()
etc. Hence you can ask the model for the answer you are seeking:
等等。因此,您可以向模型询问您正在寻找的答案:
if(jButton1.getModel().isPressed())
System.out.println("the button is pressed");
回答by alex2410
Seems you need to use JToggleButton
:
似乎你需要使用JToggleButton
:
JToggleButton tb = new JToggleButton("push me");
tb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JToggleButton btn = (JToggleButton) e.getSource();
btn.setText(btn.isSelected() ? "pushed" : "push me");
}
});
回答by MadProgrammer
JButton#isEnabled
changes the user interactivity of a component, that is, whether a user is able to interact with it (press it) or not.
JButton#isEnabled
更改组件的用户交互性,即用户是否能够与其交互(按下它)。
When a JButton
is pressed, it fires a actionPerformed
event.
当 aJButton
被按下时,它会触发一个actionPerformed
事件。
You are receiving Add button is pressed
when you press the confirm button because the add button is enabled. As stated, it has nothing to do with the pressed start of the button.
Add button is pressed
当您按下确认按钮时,您正在接收,因为添加按钮已启用。如前所述,它与按下按钮的启动无关。
Based on you code, if you tried to check the "pressed" start of the add button within the confirm button's ActionListener
it would always be false, as the button will only be in the pressed state while the add button's ActionListener
s are being called.
根据您的代码,如果您尝试检查确认按钮中添加按钮的“按下”开始,ActionListener
它将始终为 false,因为该按钮只会在ActionListener
调用添加按钮时处于按下状态。
Based on all this information, I would suggest you might want to consider using a JCheckBox
which you can then use JCheckBox#isSelected
to determine if it has being checked or not.
基于所有这些信息,我建议您可能需要考虑使用 a JCheckBox
,然后您可以使用JCheckBox#isSelected
它来确定它是否已被检查。
Take a closer look at How to Use Buttonsfor more details
仔细查看如何使用按钮以获取更多详细信息
回答by AJ.
Just do System.out.println(e.getActionCommand());
inside actionPerformed(ActionEvent e)
function. This will tell you which command is just performed.
只做System.out.println(e.getActionCommand());
内部actionPerformed(ActionEvent e)
功能。这将告诉您刚刚执行了哪个命令。
or
或者
if(e.getActionCommand().equals("Add")){
System.out.println("Add button pressed");
}
回答by nillas
The method you are trying to use checks if the button is active:
您尝试使用的方法检查按钮是否处于活动状态:
btnAdd.isEnabled()
btnAdd.isEnabled()
When enabled, any component associated with this object is active and able to fire this object's actionPerformed method.
启用后,与此对象关联的任何组件都处于活动状态并能够触发此对象的 actionPerformed 方法。
This method does not check if the button is pressed.
此方法不检查按钮是否被按下。
If i understand your question correctly, you want to disable your "Add" button after the user clicks "Check out".
如果我正确理解您的问题,您希望在用户单击“结帐”后禁用“添加”按钮。
Try disabling your button at start: btnAdd.setEnabled(false)
or after the user presses "Check out"
尝试在开始时禁用您的按钮:btnAdd.setEnabled(false)
或在用户按下“结帐”后
回答by mastrmind-dev
Use this Command
使用这个命令
if(JButton.getModel().isArmed()){
//your code here.
//your code will be only executed if JButton is clicked.
}
Answer is short. But it worked for me. Use the variable name of your button instead of "JButton".
答案很短。但它对我有用。使用按钮的变量名称而不是“JButton”。