选择特定的 JComboBox 项时如何向 JPanel 添加其他 JButton

How to add additional JButton to JPanel when specific JComboBox item is selected

我一直面临这个小问题,以使我的jcombobox尽可能动态。到该月的天数并添加到面板中

我面临的问题是它不会自动更改面板的显示,但是当我试图查看我的控制台日志中是否有代码 运行s 时。 运行很顺利。我尽力寻找解决方案,但无济于事。

主要问题出在 actionListener 中,例如,如果选择二月,它将显示 28 个按钮,如果选择一月,它将显示 31 天等等,但是当我 运行 代码时,我的system.out.println 声明 运行s 但我的 Gui 没有显示按钮。

private static JButton method_Btn(int i){
    JButton btn = new JButton(Integer.toString(i));
    return btn;
}

public static void day(){
    JFrame frame = new JFrame();
    JPanel topPanel = new JPanel();
    JPanel centerPanel = new JPanel();
    JButton days = new JButton();
    JLabel days_label = new JLabel();


    //-- Top Panel
    String month[] = {"--Select Month--" , "January", "February"};
    JComboBox month_combo = new JComboBox(month);
    topPanel.add(month_combo, BorderLayout.PAGE_START);

    //-- Center Panel
    centerPanel.setLayout(new FlowLayout());
    centerPanel.add(days_label);



    //------- Change when jcombo is selected
    month_combo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(month_combo.getSelectedItem().equals("January")){
                for(int i = 0;i < 31;i++){
                    centerPanel.add(method_Btn(i));
                }
            }

            if(month_combo.getSelectedItem().equals("February")){
                for(int i = 0;i < 28;i++){
                    centerPanel.add(method_Btn(i));
                }
            }
        }
    });

    frame.add(topPanel, BorderLayout.PAGE_START);
    frame.add(centerPanel , BorderLayout.CENTER);

    frame.setSize(400,400);
    frame.setVisible(true);
}

public static void main(String[] args){
    day();

}

补充说明, 我开始意识到我面临的另一个问题是它会叠加在第二次选择月份后创建的按钮数量。我如何解决它是我添加 centerPanel.removeAll();和 centerPanel.repaint();

month_combo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            int count = 0;
            //---- gettind days of month selected in comboBox


                if (month_combo.getSelectedItem().equals("February")) {
                    centerPanel.removeAll();
                    centerPanel.repaint();
                    for (int i = 1; i <= 28; i++) {
                        centerPanel.add(method_Btn(i));
                        System.out.println("days in feb " + i);
                    }
                    centerPanel.revalidate();
                }



            if (month_combo.getSelectedItem().equals("March")) {

                centerPanel.removeAll();
                centerPanel.repaint();
                for (int i = 1; i <= 31; i++) {
                    centerPanel.add(method_Btn(i));
                }
                centerPanel.revalidate();

            }
        }
    });

希望这对有需要的人有所帮助。 :)

您需要revalidate()您添加的组件如下:

centerPanel.revalidate();

您需要更改以下代码:

month_combo.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        if(month_combo.getSelectedItem().equals("January")){
            for(int i = 0;i < 31;i++){
                centerPanel.add(method_Btn(i));
            }
        }

        if(month_combo.getSelectedItem().equals("February")){
            for(int i = 0;i < 28;i++){
                centerPanel.add(method_Btn(i));
            }
        }

        centerPanel.revalidate(); // Need to add this for revalidation for the component
    }
});