mardi 25 octobre 2016

I have to create a 2-D array of Checkboxes in java and arrange them in 2 columns using grid layout. Can anyone help me with this?

I have create a JPanel and have added the checkboxes to the panel.

import java.awt.*;
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

This is my work up till now, This is a 1-D array, And it shows a single column of checkboxes onthe left side. How can i add another column just alongside the existing one.

public class UserInterface extends JFrame 
{

    private JPanel club;
    private JCheckBox[] check = new JCheckBox[10];

    public UserInterface()
    {
        super("SPORTS CLUB");
        club=new JPanel();

        add(club);

        club.setLayout(new GridLayout(10,2));

        check[0] = new JCheckBox("101");
        check[1] = new JCheckBox();
        check[2] = new JCheckBox();
        check[3] = new JCheckBox();
        check[4] = new JCheckBox();
        check[5] = new JCheckBox();
        check[6] = new JCheckBox();
        check[7] = new JCheckBox();
    check[8] = new JCheckBox();

        check[9] = new JCheckBox();

        club.setAlignmentY(JComponent.LEFT_ALIGNMENT);

        for (int i = 0; i < 10; i++) 
        {
            club.add(check[i]);
        }
    }


}

This is my class Main containing the main() function;

import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) 
    {
        UserInterface data=new UserInterface();
        data.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        data.setSize(300,200);
        data.setVisible(true);
    }

}

Any little help is appreciated, Even references are welcomed.




Aucun commentaire:

Enregistrer un commentaire