簡體   English   中英

選擇特定的 JComboBox 項目時如何向 JPanel 添加額外的 JButton

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

我一直面臨這個關於使我的 JCombobox 盡可能動態的小問題。
例如,當combobox中的某一項被選中時,它會根據當月的天數動態改變按鈕的數量並添加到面板中

我面臨的問題是它不會自動更改面板的顯示,但是當我嘗試查看代碼是否在我的控制台日志中運行時。 它運行順利。 我已盡力尋找解決方案,但無濟於事。

主要問題是在 actionListener 中,例如,如果選擇二月,它將顯示 28 個按鈕,如果選擇一月,它將顯示 31 天等等,但是當我運行代碼時,我的 system.out.println 聲明它運行但是我的 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
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM