JFrame中的组件

时间:2018-08-23 18:23:52

标签: jframe jcombobox

我有such interface。选择儿童数量后,必须出现带有年龄的组合框才能选择like this

我的问题是,如果我重新选择孩子的数量,则带有年龄的组合框的总数不会更新。可以增加like this。但是我需要更新它。代码是这样的:

public class Selection extends JFrame {
    JLabel rooms;
    JComboBox r;
    JComboBox[] adults;
    JComboBox[] children;
    public Selection() {
        final GridBagLayout gbl = new GridBagLayout();
        setLayout(gbl);
        Font f = new Font("Times New Roman", Font.PLAIN, 23);
        final GridBagConstraints c = new GridBagConstraints();
        rooms = new JLabel("Rooms:");
        rooms.setFont(f);
        String[] x = {"1", "2", "3", "4", "5"};
        r = new JComboBox(x);
        r.setBackground(Color.WHITE);
        r.setFont(f);
        String[] str = {"1", "2", "3", "4", "5"};
        String[] str1 = {"0", "1", "2", "3", "4", "5"};
        final String[] str2 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17"};
        adults = new JComboBox[5];
        children = new JComboBox[5];
        final JComboBox[] ages = new JComboBox[25];
        final JLabel[] adultLabels = new JLabel[5];
        JLabel[] childrenLabels = new JLabel[5];



        for (int i = 0; i < 5; i++) {
            adultLabels[i] = new JLabel("Adults in room " + (i + 1) + ":");
            adultLabels[i].setFont(f);
            childrenLabels[i] = new JLabel("Children in room " + (i + 1) + ":");
            childrenLabels[i].setFont(f);
            adults[i] = new JComboBox(str);
            adults[i].setBackground(Color.WHITE);
            adults[i].setFont(f);
            children[i] = new JComboBox(str1);
            children[i].setBackground(Color.WHITE);
            children[i].setFont(f);

        }
        r.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {
                c.anchor = GridBagConstraints.FIRST_LINE_START;
                c.fill = GridBagConstraints.NONE;
                c.gridheight = 1;
                c.gridwidth = 1;
                c.gridx = GridBagConstraints.VERTICAL;
                c.insets = new Insets(10, 10, 0, 0);
                c.gridx = GridBagConstraints.HORIZONTAL;
                remove(r);
                remove(rooms);


                for (int i = 0; i <= r.getSelectedIndex(); i++) {
                    c.gridx = GridBagConstraints.VERTICAL;
                    gbl.setConstraints(adultLabels[i], c);
                    add(adultLabels[i]);
                    gbl.setConstraints(adults[i], c);
                    add(adults[i]);
                    gbl.setConstraints(childrenLabels[i], c);
                    add(childrenLabels[i]);
                    gbl.setConstraints(children[i], c);
                    add(children[i]);
                    final int finalI = i;
                    children[i].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {

                            for(int j = 0; j < children[finalI].getSelectedIndex(); j++) {
                                ages[j] = new JComboBox(str2);
                                ages[j].setBackground(Color.WHITE);
                                ages[j].setFont(f);
                                gbl.setConstraints(ages[j], c);
                                add(ages[j]);
                                getContentPane().revalidate();
                                getContentPane().repaint();
                            }
                        }
                    });
                    getContentPane().revalidate();
                    getContentPane().repaint();
                }
            }
        });

        gbl.setConstraints(rooms, c);
        add(rooms);
        gbl.setConstraints(r, c);
        add(r);
    }
    }

public class MainClass {
    public static void main(String args[]) {
        JFrame f = new Selection();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setExtendedState(Frame.MAXIMIZED_BOTH);
        f.setLocationRelativeTo(null);
    }
}

0 个答案:

没有答案