【问题标题】:toggle a variable to true/false with a JButton使用 JButton 将变量切换为真/假
【发布时间】:2012-09-24 09:22:18
【问题描述】:

您好,我想知道。如何使用 JButton 将变量设置为 true 或 false,反之亦然?我的第一个想法是创建变量,如

private boolean value1, value2;

还有类似的按钮

private JButton toggle1, toggle2;

//见下面的代码

问题是它不会以某种方式对按钮做出反应。这种方式可以吗还是我必须使用其他方法?

编辑:这是相关代码。 (我的 ActionListener)

    public void actionPerformed(ActionEvent e) {

    if( e.getSource() == toggle1 ) {

        if(aan1 == false) {

            aan1 ^= true;
            System.out.println(aan1);

        }
        else if(aan1 == true) {

            aan1 ^= false;
        }

    }

    try {
        // controleer of de ingevulde waarde tussen de 0 en de 15 is
        if( e.getSource() == burn && Integer.parseInt(textfield.getText()) < 16 && Integer.parseInt(textfield.getText()) > 0) {
            // brand kaars
            if( height > 15 && aan1 == true) {

                int aantal = Integer.parseInt(textfield.getText());
                height -= aantal;
            }


            if( height2 > 15 && aan2 == true) {
                int aantal = Integer.parseInt(textfield.getText());
                height2 -= aantal;
            }

            // opnieuw tekenen
            repaint();

        }
        else {

            JOptionPane.showMessageDialog(null, "error: vul een getal tussen de 0 en 15 in!"); // alertbox melding

        }
    }
    catch(NumberFormatException error) {

        JOptionPane.showMessageDialog(null, "error: een van de velden bevat geen cijfer of is niet ingevuld!"); // alertbox melding

    }

}

【问题讨论】:

  • 1. 这种方式有可能还是我必须使用其他方法?当您不提供相关代码时很难说(创建JButton和action/ActionListener) 2. if ( value1==false) 更短且更易读的版本是if(!value1) 3. 要切换一个值,只需写value1 = !value1; 4. 在您​​的操作方法中重新声明一个局部变量value1!!!这只是找麻烦
  • @GuillaumePolet 我发布了 actionlistener,现在如果我使用按钮,它会从 false 变为 true,但如果我再次点击它,它就不会变回来..
  • 去掉包装if(!aan)
  • 我再次对其进行了修改,但我仍然获得了单向更改。一旦它是真的,它就不会再假了。
  • 对不起,我理解错了!现在可以了,非常感谢!

标签: java swing jbutton


【解决方案1】:

就这样做?:

value1 != value1;

这将当前值取反:所以如果为假,它将变为真,反之亦然。

编辑: 应该是:

value = !value;

【讨论】:

  • 但这也是一种不使用按钮的方式。不幸的是,我必须使用一个按钮来打开或关闭它
  • 这可以完美地使用按钮吗?把它扔给你的actionlistener(或者它是如何被调用的,因为我已经有一段时间编写java了:))
【解决方案2】:

正如How to Use Buttons 中所建议的,JToggleButton 可能是一个不错的选择,因为isSelected() 谓词反映了按钮的状态。

state = button.isSelected()

可以在hereherehere 找到示例。

【讨论】:

    【解决方案3】:

    如果您想在按下按钮时切换布尔变量,您应该使用JCheckBox 而不是JButton,因为它具有内部布尔状态并且它会自行更新此变量。该复选框还使该布尔状态对用户可见。

    当你需要布尔变量时,你可以用JCheckBox.isSelected()方法询问它。

    【讨论】:

    • 我正在做一个必须使用按钮的练习。复选框在这里不是一个选项:)
    • @Reshad 复选框是JToggleButton,而AbstractButton...所以它是一个按钮
    【解决方案4】:

    不确定您要问的是什么,但要切换您可以使用的布尔值:

    value1 = !value1;
    

    value1 ^= true;
    

    然后打印你的变量:

    System.out.println(value1);
    

    【讨论】:

    • 嗯,谢谢!不知道.. 但问题是我的 jButton 不起作用。 if( e.getSource() == toggle1 ) { 如果我点击按钮没有任何反应。
    猜你喜欢
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多