【问题标题】:How to add additional JButton to JPanel when specific JComboBox item is selected选择特定 JComboBox 项时如何向 JPanel 添加额外的 JButton
【发布时间】:2019-01-23 07:50:31
【问题描述】:

我一直在面临的这个小问题,就是使我的JCombobox成为动态。月内的天数并添加到面板中

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

主要问题在 actionListener 中,例如,如果选择了 2 月,它将显示 28 个按钮,如果选择了 1 月,它将显示 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();

            }
        }
    });

希望这可以帮助任何有需要的人。 :)

【问题讨论】:

  • JPanel.add 的文档:此方法更改与布局相关的信息,因此使组件层次结构无效。如果容器已经显示,则层次结构必须在之后验证才能显示添加的组件。

标签: java swing jpanel jbutton jcombobox


【解决方案1】:

您需要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
    }
});

【讨论】:

  • 我完全忘记了 revalidate(); .谢谢。但是当我添加重新验证时,还有另一个问题。例如,当我选择 february 时,它成功添加了 28 个按钮,但是当我选择 March 时,它会在首先添加的 28 个按钮之上再添加 30 个按钮。
  • 是的,因为您总是在面板中添加新按钮。您可以检查组件子项的数量等。当然,您还需要在代码中添加一些逻辑。但是,我认为你原来的帖子问题已经解决了:)
  • 好的,谢谢。如果面板包含组件,我将通过添加一些逻辑来删除所有按钮来研究它,否则它将根据当月的天数添加新的 jbutton
  • 如果此答案解决了您的问题,请将其标记为已批准,以便所有人都知道这是解决方案;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-14
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
相关资源
最近更新 更多