单击布尔值更改按钮

发布于 2024-10-10 04:31:02 字数 112 浏览 0 评论 0原文

每次用户按下某个按钮时布尔值会发生变化,最简单的解决方案是什么?我的按钮操作监听器中唯一的事情就是调用某个方法,例如 Method1(); 或者使用 int 会是一个更可行的解决方案吗?

谢谢。

What would be the most simple solution each time a user presses a certain button the bool value changes? The only thing in my button action listener would be a call to a certain method for example Method1();
Or would working with a int be a more viable solution?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫离伤花 2024-10-17 04:31:02

更简单:

buttonFlag = ! buttonFlag;

Even simpler:

buttonFlag = ! buttonFlag;
柏拉图鍀咏恒 2024-10-17 04:31:02

在类中的字段上有一个标志变量。

示例:

private boolean bottonFlag = false.

在您的方法中执行以下操作:

bottonFlag = bottonFlag == true ? false : true; // this is to switch between true and false

Have a flag variable on the fields in your class.

Example:

private boolean bottonFlag = false.

in your method do:

bottonFlag = bottonFlag == true ? false : true; // this is to switch between true and false
烟火散人牵绊 2024-10-17 04:31:02

对于这种情况,我建议使用 JToggleButton 或 JCheckBox。这对用户来说会更自然,并且任何一个组件都可以存储其状态而无需声明任何进一步的布尔值。

例如

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ToggleButton {

    public static void main(String[] args) {

        final JLabel result = new JLabel("Hit the button!");

        final JToggleButton switchButton = new JToggleButton("Switch");
        switchButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                result.setText("" + switchButton.isSelected());
            }
        } );

        JPanel p = new JPanel(new GridLayout(0,1));
        p.add(switchButton);
        p.add(result);

        JOptionPane.showMessageDialog(null, p);
    }
}

I would recommend using a JToggleButton or JCheckBox for this case. It would be more natural to the user and either component can store it's state without declaring any further booleans.

E.G.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ToggleButton {

    public static void main(String[] args) {

        final JLabel result = new JLabel("Hit the button!");

        final JToggleButton switchButton = new JToggleButton("Switch");
        switchButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                result.setText("" + switchButton.isSelected());
            }
        } );

        JPanel p = new JPanel(new GridLayout(0,1));
        p.add(switchButton);
        p.add(result);

        JOptionPane.showMessageDialog(null, p);
    }
}
初懵 2024-10-17 04:31:02

我认为这正是您所寻找的!希望有帮助。

    private boolean booly; 

private class WinkAction implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {

        if (!booly){
            booly = true; 

        }else {
            booly = false;


        }

    }
}

I think this is exactly what your looking for!! Hope it helps.

    private boolean booly; 

private class WinkAction implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {

        if (!booly){
            booly = true; 

        }else {
            booly = false;


        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文