Getting checkbox values on clicking the button in swings [closed]

你离开我真会死。 提交于 2019-12-25 18:17:53

问题


I need a small code in java for the following scenario:

Button should get the selected checkboxes and executing the code for those checkboxes in my form.


回答1:


Here is an example I made for you:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class TestJCheckBox {

    private JFrame frame;
    private JCheckBox jcb;
    private JButton button;

    public TestJCheckBox() {
        initComponents();
    }

    private void initComponents() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        jcb = new JCheckBox("JCheckBox1");

        button = new JButton("Is JCheckBox selected?");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (jcb.isSelected()) {
                    JOptionPane.showMessageDialog(frame, "JCheckBox is selected");
                } else {
                    JOptionPane.showMessageDialog(frame, "JCheckBox is NOT selected");

                }
            }
        });

        frame.add(jcb, BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestJCheckBox();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/13348601/getting-checkbox-values-on-clicking-the-button-in-swings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!