从另一个按钮更改切换按钮的状态

发布于 2024-12-14 06:23:40 字数 276 浏览 0 评论 0原文

我正在使用 Swing 与 Eclipse 和 Window Builder Pro 创建 Java GUI。我正在使用 JButtonsJToggleButtons。我想从另一个按钮更改切换按钮的状态。

在此处输入图像描述

例如,当我单击清除网格时,所有切换按钮都将“未选中”。

我该怎么做?我必须使用哪些方法来切换按钮和按钮?

I'm creating a Java GUI using Swing with Eclipse and Window Builder Pro. I'm using JButtons and JToggleButtons. I want to change toggle button's state from another button.

enter image description here

For example, when I click the clear grid, all the toggle buttons will be 'not selected'.

How can I do this? What are the methods that I have to use for toggle buttons and buttons?

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

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

发布评论

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

评论(4

紫罗兰の梦幻 2024-12-21 06:23:40

toggleButton.setSelected(布尔值b)

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class JToggleButtonAction {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Selecting Toggle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JToggleButton toggleButton = new JToggleButton("Toggle Button");
        final JToggleButton toggleButton1 = new JToggleButton("Another Toggle Button");
        ActionListener actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
                boolean selected = abstractButton.getModel().isSelected();
                System.out.println("Action - selected=" + selected + "\n");
                toggleButton1.setSelected(selected);
            }
        };
        toggleButton.addActionListener(actionListener);
        frame.add(toggleButton, BorderLayout.NORTH);
        frame.add(toggleButton1, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
}

toggleButton.setSelected(boolean b)

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class JToggleButtonAction {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Selecting Toggle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JToggleButton toggleButton = new JToggleButton("Toggle Button");
        final JToggleButton toggleButton1 = new JToggleButton("Another Toggle Button");
        ActionListener actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
                boolean selected = abstractButton.getModel().isSelected();
                System.out.println("Action - selected=" + selected + "\n");
                toggleButton1.setSelected(selected);
            }
        };
        toggleButton.addActionListener(actionListener);
        frame.add(toggleButton, BorderLayout.NORTH);
        frame.add(toggleButton1, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
}
盗琴音 2024-12-21 06:23:40

将actionListener 添加到您的JButton 中,并在actionPerformed(ActionEvent) 方法中更改所有JToggleButton 的状态。确保所有 JToggleButton 都可以在此方法中访问。一个简单的例子是..

    JFrame frame = new JFrame("Panel image demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setLayout(new FlowLayout());

    final JToggleButton[] button = new JToggleButton[10];
    for (int i = 0; i < button.length; i++) {
        button[i] = new JToggleButton("Toggle Us");
        frame.add(button[i]);
    }
    JButton jButton = new JButton("Toggle that button");
    jButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (JToggleButton jToggleButton : button) {
                jToggleButton.setSelected(!jToggleButton.isSelected()); // <-- this will change the state of toggle button
            }
        }
    });

    frame.add(jButton);
    frame.setVisible(true);

Add actionListener to your JButton and in actionPerformed(ActionEvent) method change the state of all JToggleButtons. Make sure all your JToggleButton is accessible in this method. A simple example will be..

    JFrame frame = new JFrame("Panel image demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setLayout(new FlowLayout());

    final JToggleButton[] button = new JToggleButton[10];
    for (int i = 0; i < button.length; i++) {
        button[i] = new JToggleButton("Toggle Us");
        frame.add(button[i]);
    }
    JButton jButton = new JButton("Toggle that button");
    jButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (JToggleButton jToggleButton : button) {
                jToggleButton.setSelected(!jToggleButton.isSelected()); // <-- this will change the state of toggle button
            }
        }
    });

    frame.add(jButton);
    frame.setVisible(true);
扛起拖把扫天下 2024-12-21 06:23:40

ActionListener 注册到 JButton 实例,并确保您可以访问其中的切换按钮来操纵其状态。

Register an ActionListener to the JButton instance and make sure you can access the toggle buttons therein to manipulate their state.

墟烟 2024-12-21 06:23:40

我正在寻找这个,这可能会帮助某人

B1.setSelected(false);

我做了一个方法,使我的所有按钮都为假(当我想要它时取消选择切换)

i was looking for this, this might help somebody out

B1.setSelected(false);

i made a method that make all my button false (unselect the toggles when i wanted it)

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