简体   繁体   English

如果在ActionPerformed事件期间未执行语句

[英]If statement not executing during ActionPerformed event

Trying to write a Swing GUI. 尝试编写Swing GUI。 When clicking a button, I want to test if the other button has already been clicked. 单击一个按钮时,我要测试是否已单击另一个按钮。 If it has, then execute the statements in the "if" statements. 如果有,则执行“ if”语句中的语句。 But it looks like the "if" statements never execute? 但是看起来“ if”语句永远不会执行?

private void radSingleBurgerActionPerformed(java.awt.event.ActionEvent evt) {
    if(radDoubleBurger.isSelected()){
        newItemPrice = Double.parseDouble(lblItemPrice.getText());
        newItemPrice -= doublePrice;
        lblTest.setText(String.valueOf(newItemPrice));//test to see if working
    }
    lblItemPrice.setText(String.valueOf(newItemPrice += singlePrice));
}                                               

private void radDoubleBurgerActionPerformed(java.awt.event.ActionEvent evt) {
     if(radSingleBurger.isSelected()){
        newItemPrice = Double.parseDouble(lblItemPrice.getText());
        newItemPrice -= singlePrice;
        lblTest.setText(String.valueOf(newItemPrice));//test to see if working
    }
    lblItemPrice.setText(String.valueOf(newItemPrice += doublePrice));
}

Clicking a JButton does not make it (permanently) selected. 单击JButton不会(永久)选择它。 Clicking is only a temporary action. 单击只是一个临时动作。

Maybe you want to use a JToggleButton . 也许您想使用JToggleButton This allows you to click a button to make it selected and then click it again to make it unselected. 这使您可以单击一个按钮使其变为选中状态,然后再次单击使其变为未选中状态。

Read the section from the Swing tutorial on How to Use Buttons for more information. 阅读Swing教程中有关如何使用按钮的部分, 获取更多信息。

Or if you just want to know if the user has clicked on a regular JButton , then you will need to maintain a Boolean variable yourself that you update in the ActionListener of the button. 或者,如果您只是想知道用户是否单击了常规JButton ,则需要自己维护一个布尔变量,该变量在按钮的ActionListener中进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM