首页 > 解决方案 > 添加新的组合框 Java

问题描述

我在 java 上的 Swing 接口有问题。说明:我有一个包含 1、2、3、4、5 项的组合框。When an exact item is selected I need to create some more comboboxes the number of which depends on the selected item. 因此,如果选择了数字 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);
    }
}

标签: javaswing

解决方案


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

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

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);
    }
}

推荐阅读