添加新的Java Java组合框

时间:2018-07-02 12:28:51

标签: java swing

我在Java上的Swing接口有问题。说明:我有一个带有1,2,3,4,5个项目的组合框。当选择一个确切的项目时,我需要创建更多的组合框,其数量取决于所选的项目。因此,如果选择了数字5,则框架中还必须出现5个组合框。我使用了ActionListener,但无法正常工作。但是,在Actionlistener外部使用相同的代码效果很好。有什么问题吗?

public class FrameClass extends JFrame {
    JPanel panel;
    JComboBox box;

    String[] s = {"1", "2", "3", "4", "5"};
    String[] s1 = {"0", "1", "2", "3", "4", "5"};
    public FrameClass() {
        panel = new JPanel();
        box = new JComboBox(s);
        JComboBox adults = new JComboBox(s);
        JComboBox children = new JComboBox(s1);

        panel.add(box, BorderLayout.CENTER);

        box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            for(int i = 0; i <= box.getSelectedIndex(); i++) {
                panel.add(adults, BorderLayout.WEST);
                panel.add(children, BorderLayout.WEST);
            }
        }
        });
        add(panel);
    }
}

public class MainClass {
    public static void main(String[] args) {
        JFrame frame = new FrameClass();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.getContentPane().setBackground(Color.WHITE);
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

您没有通知布局管理器面板中新元素的问题。

这是您的动作监听器的正确变体:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class FrameClass extends JFrame {
    JPanel panel;

    JComboBox<String> box;

    String[] s = {"1", "2", "3", "4", "5"};

    String[] s1 = {"0", "1", "2", "3", "4", "5"};

    public FrameClass() {
        panel = new JPanel();
        box = new JComboBox(s);
        JComboBox[] adults = new JComboBox[5];
        JComboBox[] children = new JComboBox[5];
        for (int i = 0; i < 5; i++) {
            adults[i] = new JComboBox<>(s);
            children[i] = new JComboBox<>(s1);
        }

        panel.add(box, BorderLayout.CENTER);
        JPanel nested = new JPanel();
        add(nested, BorderLayout.EAST);
        box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                nested.removeAll();
                nested.setLayout(new GridLayout(box.getSelectedIndex() + 1, 2));
                for (int i = 0; i <= box.getSelectedIndex(); i++) {
                    nested.add(adults[i]);
                    nested.add(children[i]);
                }
                getContentPane().revalidate();
                getContentPane().repaint();
                pack();
            }
        });
        add(panel);
    }

    public static void main(String[] args) {
        JFrame frame = new FrameClass();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.getContentPane().setBackground(Color.WHITE);
        frame.setLocationRelativeTo(null); // center the window
        frame.setVisible(true);
    }
}