gpt4 book ai didi

java - 使用 JButton 将变量切换为 true/false

转载 作者:行者123 更新时间:2023-12-01 17:29:09 25 4
gpt4 key购买 nike

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

private boolean value1, value2;

以及诸如

之类的按钮
private JButton toggle1, toggle2;

//见下面的代码

问题是它不会以某种方式对按钮使用react。这种方式可行还是我必须使用其他方法?

编辑:这是相关代码。 (我的 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

}

}

最佳答案

不确定您到底在问什么,但要切换可以使用的 boolean 值:

value1 = !value1;

value1 ^= true;

然后打印你的变量:

System.out.println(value1);

关于java - 使用 JButton 将变量切换为 true/false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12708653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com