Java adds multiple checkbox control example shares through the checkbox control array implementation

  • 2020-04-01 03:00:49
  • OfStack

The idea is as follows:

Create a JPanel panel object;
Use the setLayout(0,4) method of JPanel class to set the grid layout manager, that is, the number of columns is 4, the number of rows is automatically adjusted;
Creates a string one-dimensional array as a control text array;
Creates a JCheckBox one-dimensional array as an array of controls.
Use the for loop to iterate over the control array, initialize the checkbox components in the array, and add the array elements (that is, each checkbox) to the panel using the add() method of the JPanel class.
The code is as follows:


package cn.edu.xidian.crytoll;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.UIManager;

public class CheckBoxArray extends JFrame {

    
    private static final long serialVersionUID = -5338362310060106193L;
    private JPanel contentPane;
    private JPanel panel;

    
    public static void main(String[] args) {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CheckBoxArray frame = new CheckBoxArray();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    
    public CheckBoxArray() {
        setTitle(" Add multiple checkbox controls through the checkbox control array implementation ");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 409, 331);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JLabel label = new JLabel(
                " What are your hobbies: ");
        contentPane.add(label, BorderLayout.NORTH);
        contentPane.add(getPanel(), BorderLayout.CENTER);
    }

    private JPanel getPanel() {
        if (panel == null) {
            panel = new JPanel();//Create panel object
            panel.setLayout(new GridLayout(0, 4));//Set up the grid layout manager
            //Creates an array of control text
            String[] labels = { " football ", " basketball ", " The magic ", " Table tennis ", " See a movie ", " World of warcraft ", "CS team ",
                    " badminton ", " swimming ", " tourism ", " Climbing the mountain ", " Sing a song ", " Writing a blog ", " The world of animals ", " Taking pictures ", " Playing the guitar ",
                    " Read the newspaper ", " Motorcycle racing ", " shopping ", " The mall ", " mahjong ", " Reading a book ", " Looking at information online ", " news ", " military ",
                    " gossip ", " Keeping in good health ", " Drinking tea " };
            JCheckBox[] boxs = new JCheckBox[labels.length];//Create an array of controls
            for (int i = 0; i < boxs.length; i++) {//Traverses an array of controls
                boxs[i] = new JCheckBox(labels[i]);//Initializes the checkbox component in the array
                panel.add(boxs[i]);//Add the array elements (that is, each check box) to the panel
            }
        }
        return panel;
    }
}


Related articles: