【问题标题】:add dynamic checkbox in java windowbuilder在java windowbuilder中添加动态复选框
【发布时间】:2018-10-09 14:28:32
【问题描述】:

我想在组合框中选择一个项目后添加一个动态复选框。 我正在研究 Eclipse 并在窗口构建器上设计我的框架。

    final JComboBox<String> comboBox = new JComboBox<String>();
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String typeName = comboBox.getSelectedItem().toString();
            for (int i = 0; i < SqlQuery.getCoursesName(typeName).size(); i++) {
                JCheckBox c = new JCheckBox(SqlQuery.getCoursesName(typeName).get(i));
                c.setVisible(true);
                coursePanel.add(c);

                frame.repaint();
                frame.validate();

                System.out.println(c.getText());
            }
        }
    });
    comboBox.setBounds(208, 221, 91, 20);
    frame.getContentPane().add(comboBox);

编辑:这是我的完整代码。

public class registerForm { 
private JFrame frame;
private JTextField txtFirstName;
private JTextField txtLastName;
private JTextField txtPassword;
private JTextField txtEmail;

List<Integer> coursesId; // ן¿½ן¿½ן¿½ן¿½ן¿½ ן¿½ן¿½ ן¿½ן¿½ן¿½ן¿½ן¿½ ן¿½ן¿½ ן¿½ן¿½ן¿½ן¿½ן¿½ן¿½

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                registerForm window = new registerForm();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public registerForm() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 442);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel lblNewLabel = new JLabel("\u05D4\u05E8\u05E9\u05DE\u05D4");
    lblNewLabel.setBounds(165, 11, 91, 29);
    lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
    frame.getContentPane().add(lblNewLabel);

    JLabel label = new JLabel("\u05E9\u05DD \u05E4\u05E8\u05D8\u05D9:");
    label.setBounds(363, 55, 61, 14);
    label.setFont(new Font("Tahoma", Font.PLAIN, 14));
    frame.getContentPane().add(label);

    txtFirstName = new JTextField();
    txtFirstName.setBounds(75, 51, 221, 20);
    frame.getContentPane().add(txtFirstName);
    txtFirstName.setColumns(10);

    JLabel label_1 = new JLabel("\u05E9\u05DD \u05DE\u05E9\u05E4\u05D7\u05D4:");
    label_1.setBounds(344, 80, 80, 14);
    label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
    frame.getContentPane().add(label_1);

    txtLastName = new JTextField();
    txtLastName.setBounds(75, 82, 221, 20);
    txtLastName.setColumns(10);
    frame.getContentPane().add(txtLastName);

    txtPassword = new JTextField();
    txtPassword.setBounds(75, 140, 221, 20);
    txtPassword.setColumns(10);
    frame.getContentPane().add(txtPassword);

    JLabel label_2 = new JLabel("\u05DE\u05D9\u05D9\u05DC:");
    label_2.setBounds(392, 110, 32, 14);
    label_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
    frame.getContentPane().add(label_2);

    txtEmail = new JTextField();
    txtEmail.setBounds(75, 109, 221, 20);
    txtEmail.setColumns(10);
    frame.getContentPane().add(txtEmail);

    JLabel label_3 = new JLabel("\u05E1\u05D9\u05E1\u05DE\u05D0:");
    label_3.setBounds(373, 141, 51, 14);
    label_3.setFont(new Font("Tahoma", Font.PLAIN, 14));
    frame.getContentPane().add(label_3);

    final JDateChooser dateChooser = new JDateChooser();
    dateChooser.setBounds(75, 171, 221, 39);
    frame.getContentPane().add(dateChooser);

    JLabel label_4 = new JLabel("\u05EA\u05D0\u05E8\u05D9\u05DA \u05DC\u05D9\u05D3\u05D4:");
    label_4.setBounds(344, 167, 90, 14);
    label_4.setFont(new Font("Tahoma", Font.PLAIN, 14));
    frame.getContentPane().add(label_4);

    JButton btnSend = new JButton("\u05E9\u05DC\u05D7");
    btnSend.setBounds(258, 334, 61, 58);
    btnSend.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            // date
            Date date = new Date(dateChooser.getDate().getTime());
        }
    });
    frame.getContentPane().add(btnSend);

    JButton button = new JButton("\u05E0\u05E7\u05D4");
    button.setBounds(175, 334, 61, 58);
    frame.getContentPane().add(button);

    JLabel label_5 = new JLabel("\u05DE\u05D2\u05DE\u05D4:");
    label_5.setFont(new Font("Tahoma", Font.PLAIN, 14));
    label_5.setBounds(382, 218, 42, 14);
    frame.getContentPane().add(label_5);

    final JPanel coursePanel = new JPanel();
    coursePanel.setBounds(10, 249, 286, 74);
    frame.getContentPane().add(coursePanel);
    coursePanel.setLayout(null);

    final JComboBox<String> comboBox = new JComboBox<String>();
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String typeName = comboBox.getSelectedItem().toString();
            for (int i = 0; i < SqlQuery.getCoursesName(typeName).size(); i++) {
                JCheckBox c = new JCheckBox(SqlQuery.getCoursesName(typeName).get(i));
                int selectedIndex = comboBox.getSelectedIndex();
                boolean isInPanel = c.getParent() == coursePanel;
                if (selectedIndex == 1 && !isInPanel) {
                    coursePanel.add(c);
                    coursePanel.repaint(); //Repaint the proper panel that has this component.
                    coursePanel.revalidate();
                } else if (isInPanel && selectedIndex != 1) {
                    coursePanel.remove(c);
                    coursePanel.repaint(); //Repaint the proper panel that has this component.
                    coursePanel.revalidate();
                }
                coursePanel.repaint();
                coursePanel.validate();

                System.out.println(c.getText());
            }
        }
    });
    comboBox.setBounds(208, 221, 91, 20);
    frame.getContentPane().add(comboBox);
    // fill comboBox
    List<String> lst = SqlQuery.getTypes();
    for (int i = 0; i < lst.size(); i++)
        comboBox.addItem(lst.get(i));

    JLabel label_6 = new JLabel("\u05E9\u05DC\u05D9\u05D8\u05D4 \u05D1\u05E7\u05D5\u05E8\u05E1\u05D9\u05DD");
    label_6.setFont(new Font("Tahoma", Font.PLAIN, 14));
    label_6.setBounds(321, 245, 103, 14);
    frame.getContentPane().add(label_6);



}

}

希望你能理解我写的内容。单击组合框后,我想显示课程名称列表。 为什么框架不显示复选框? 谢谢。

【问题讨论】:

    标签: java eclipse checkbox dynamic windowsbuilder


    【解决方案1】:

    首先。 comboBox.setBounds() 是一个非常糟糕的做法。花点时间看看Layout Managers。由于您指的是 Swing 库,因此还要在您的帖子中添加“swing”标签。

    框架未显示组件,因为您重新绘制了错误的组件。您在 coursePanel 中添加复选框,这意味着您必须 repaint() & revalidate() coursePanel。所以coursePanel.repaint() 而不是frame.repaint() 会帮助你。

    也不需要checkBox.setVisible(true),因为默认情况下复选框是可见的。

    最后我添加了我将如何做的方式。

    package test;
    
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Test {
    
        private JFrame frame;
        private JCheckBox checkBox;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Test window = new Test();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public Test() {
            frame = new JFrame();
            frame.setPreferredSize(new Dimension(300, 300));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(createPanel());
            frame.pack();
        }
    
        private JPanel createPanel() {
            JPanel p = new JPanel(new FlowLayout());
            checkBox = new JCheckBox("I am a checkbox"); //Create the checkbox
            JComboBox<String> combo = new JComboBox<>();
            combo.addItem("Hello");
            combo.addItem("Stack");
            combo.addItem("OverFlow");
            combo.addActionListener(e -> {
                //Its better to handle indices when there is not dynmic data to your combobox
                int selectedIndex = combo.getSelectedIndex();
                boolean isInPanel = checkBox.getParent() == p;
                if (selectedIndex == 1 && !isInPanel) {
                    p.add(checkBox);
                    p.repaint(); //Repaint the proper panel that has this component.
                    p.revalidate();
                } else if (isInPanel && selectedIndex != 1) {
                    p.remove(checkBox);
                    p.repaint(); //Repaint the proper panel that has this component.
                    p.revalidate();
                }
    
            });
            p.add(combo);
            return p;
        }
    }
    

    在 cmets 之后您的问题的答案(使复选框可见):

    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class registerForm {
        private JFrame frame;
        private JTextField txtFirstName;
        private JTextField txtLastName;
        private JTextField txtPassword;
        private JTextField txtEmail;
        private JCheckBox checkBox;
    
        List<Integer> coursesId; // ן¿½ן¿½ן¿½ן¿½ן¿½ ן¿½ן¿½ ן¿½ן¿½ן¿½ן¿½ן¿½ ן¿½ן¿½ ן¿½ן¿½ן¿½ן¿½ן¿½ן¿½
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        registerForm window = new registerForm();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public registerForm() {
            initialize();
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();
            frame.setBounds(100, 100, 450, 442);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);
    
            JLabel lblNewLabel = new JLabel("\u05D4\u05E8\u05E9\u05DE\u05D4");
            lblNewLabel.setBounds(165, 11, 91, 29);
            lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
            frame.getContentPane().add(lblNewLabel);
    
            JLabel label = new JLabel("\u05E9\u05DD \u05E4\u05E8\u05D8\u05D9:");
            label.setBounds(363, 55, 61, 14);
            label.setFont(new Font("Tahoma", Font.PLAIN, 14));
            frame.getContentPane().add(label);
    
            txtFirstName = new JTextField();
            txtFirstName.setBounds(75, 51, 221, 20);
            frame.getContentPane().add(txtFirstName);
            txtFirstName.setColumns(10);
    
            JLabel label_1 = new JLabel("\u05E9\u05DD \u05DE\u05E9\u05E4\u05D7\u05D4:");
            label_1.setBounds(344, 80, 80, 14);
            label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
            frame.getContentPane().add(label_1);
    
            txtLastName = new JTextField();
            txtLastName.setBounds(75, 82, 221, 20);
            txtLastName.setColumns(10);
            frame.getContentPane().add(txtLastName);
    
            txtPassword = new JTextField();
            txtPassword.setBounds(75, 140, 221, 20);
            txtPassword.setColumns(10);
            frame.getContentPane().add(txtPassword);
    
            JLabel label_2 = new JLabel("\u05DE\u05D9\u05D9\u05DC:");
            label_2.setBounds(392, 110, 32, 14);
            label_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
            frame.getContentPane().add(label_2);
    
            txtEmail = new JTextField();
            txtEmail.setBounds(75, 109, 221, 20);
            txtEmail.setColumns(10);
            frame.getContentPane().add(txtEmail);
    
            JLabel label_3 = new JLabel("\u05E1\u05D9\u05E1\u05DE\u05D0:");
            label_3.setBounds(373, 141, 51, 14);
            label_3.setFont(new Font("Tahoma", Font.PLAIN, 14));
            frame.getContentPane().add(label_3);
    
    
            JLabel label_4 = new JLabel("\u05EA\u05D0\u05E8\u05D9\u05DA \u05DC\u05D9\u05D3\u05D4:");
            label_4.setBounds(344, 167, 90, 14);
            label_4.setFont(new Font("Tahoma", Font.PLAIN, 14));
            frame.getContentPane().add(label_4);
    
    
            JButton button = new JButton("\u05E0\u05E7\u05D4");
            button.setBounds(175, 334, 61, 58);
            frame.getContentPane().add(button);
    
            JLabel label_5 = new JLabel("\u05DE\u05D2\u05DE\u05D4:");
            label_5.setFont(new Font("Tahoma", Font.PLAIN, 14));
            label_5.setBounds(382, 218, 42, 14);
            frame.getContentPane().add(label_5);
    
            final JPanel coursePanel = new JPanel();
            coursePanel.setBounds(10, 249, 286, 74);
            frame.getContentPane().add(coursePanel);
            coursePanel.setLayout(null);
            List<String> lst = new ArrayList<>();
            lst.add("Hello");
            lst.add("Stack");
            lst.add("Over");
            lst.add("Flow");
            //Prepare the checkBox
            checkBox = new JCheckBox("nothing");
            final JComboBox<String> comboBox = new JComboBox<String>();
            comboBox.addActionListener(new ActionListener() {
    
    
                public void actionPerformed(ActionEvent e) {
                    String typeName = comboBox.getSelectedItem().toString();
                    for (int i = 0; i < lst.size(); i++) {
                        checkBox.setText(typeName);
                        //set the same bounds as comboBox but reduce its X by 80.
                        //Compare checkBoxbounds and combobox bounds
                        //Remind: setBounds is baaaaad practice.
                        checkBox.setBounds(128, 221, 91, 20);
                        //if you want to add it in coursePanel, change all frame.getContentPane() to coursePanel
                        int selectedIndex = comboBox.getSelectedIndex();
                        boolean isInPanel = checkBox.getParent() == frame.getContentPane(); 
                        if (selectedIndex == 1 && !isInPanel) {
    
                            frame.getContentPane().add(checkBox);
                        } else if (isInPanel && selectedIndex != 1) {
                            frame.getContentPane().remove(checkBox);
                        }
                        frame.getContentPane().repaint();
                        frame.getContentPane().validate();
    
                        System.out.println(checkBox.getText());
                    }
                }
            });
            comboBox.setBounds(208, 221, 91, 20);
            frame.getContentPane().add(comboBox);
            // fill comboBox
    
            for (int i = 0; i < lst.size(); i++)
                comboBox.addItem(lst.get(i));
    
            JLabel label_6 = new JLabel("\u05E9\u05DC\u05D9\u05D8\u05D4 \u05D1\u05E7\u05D5\u05E8\u05E1\u05D9\u05DD");
            label_6.setFont(new Font("Tahoma", Font.PLAIN, 14));
            label_6.setBounds(321, 245, 103, 14);
            frame.getContentPane().add(label_6);
    
        }
    }
    

    另一种选择是将复选框创建为动作侦听器之外的字段(就像我所做的那样)并使用窗口构建器设置其边界并使用 setVisible(false)。然后在你的监听器上,setVisible(true)

    【讨论】:

    • 嗨。谢谢你帮助我。我正在使用 swing,因为它用于拼贴项目。我做了你写的,但它仍然没有显示复选框。
    • 那么你可能需要给我们更多的代码。还有你在 coursePanel 中使用什么布局?
    • 我编辑了帖子并发送了我的完整代码。 @George Zougianos
    • 您使用的是空布局。这就是组件不显示的原因。您可以在添加组件之前使用 setBounds 到组件,也可以简单地使用布局管理器。我已经在回答中提到了这一点。从不推荐使用 setBounds。
    • 但我无法设置绑定到组合框,因为它来自数据库信息。我不知道我在 coursePanel 中的组合计数是多少
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多