可变数量的复选框

时间:2016-03-03 19:08:59

标签: java swing checkbox

我有一个Student对象的ArrayList,它有String名称和布尔不存在, 当我打开程序时,我希望它使用ArrayList来创建可变数量的复选框,具体取决于有多少学生。我尝试了每个循环,为每个学生创建一个新的复选框,但这会导致整个程序崩溃。

这是具有GUI视图的类

public class SchedulerView extends JFrame implements ActionListener
{
    private String[] shownames = {"show1","show2"};

    private JPanel contentPane;
    private static ArrayList<Student> studentlist1 = new ArrayList<Student>();
    private static ArrayList<Song> songlist1 = new ArrayList<Song>();

    private static int show;

    public SchedulerView() 
    {

        setTitle("School of Rock Scheduler");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblSelectShow = new JLabel("Select Show:");
        lblSelectShow.setBounds(77, 11, 107, 14);
        contentPane.add(lblSelectShow);

        JComboBox showlist = new JComboBox(shownames);
        showlist.setSelectedIndex(0);
        showlist.addActionListener(this);
        showlist.setBounds(184, 8, 149, 20);
        contentPane.add(showlist);

        for(Student student:studentlist1)
        {
            JCheckBox chbx = new JCheckBox(Student.getName(student));
            chbx.setBounds(19, 60, 97, 23);
            contentPane.add(chbx);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        JComboBox cb = (JComboBox)e.getSource();
        int i = cb.getSelectedIndex();
        show = i;
        System.out.println(show);

    }

    public static void main(String[] args) 
    {
        Song song1 = new Song("Holiday", false);
        Song song2 = new Song("Boulevard of Broken Dreams", false);
        Song song3 = new Song("Learn to Fly", false);
        Song song4 = new Song("Come As You Are", false);
        Song song5 = new Song("Everlong", false);

        songlist1.add(song1);songlist1.add(song2);
        songlist1.add(song3);songlist1.add(song4);
        songlist1.add(song5);

        Student student1 = new Student("Dustin",false,song1);
        Student student2 = new Student("Deloris",false,song2);
        Student student3 = new Student("Gianna",false,song3);
        Student student4 = new Student("Kelly",false,song4);
        Student student5 = new Student("Reggie",false,song5);

        studentlist1.add(student1);studentlist1.add(student2);
        studentlist1.add(student3);studentlist1.add(student4);
        studentlist1.add(student5);

        Schedule.createSchedule(studentlist1, songlist1);



        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SchedulerView frame = new SchedulerView();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

0 个答案:

没有答案