-2

对于我的 UI,我让用户从三个下拉框中做出三个选择。我一直无法弄清楚是否有一种方法可以三合一JPanel。到目前为止,我只有三个不同的JPanels. 如何将所有三个投递箱添加到一个JPanel

这是我的代码

JPanel panel = new JPanel();
panel.add(new JLabel("Please select a country:"));
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement("Great Britian");
model.addElement("United States");
JComboBox comboBox = new JComboBox(model);
panel.add(comboBox);

JOptionPane.showConfirmDialog(null, panel, "Country", 
   JOptionPane.OK_OPTION, 
   JOptionPane.QUESTION_MESSAGE);

JPanel panel1 = new JPanel();
panel1.add(new JLabel("Please select a restaurant category"));
DefaultComboBoxModel model1 = new DefaultComboBoxModel();
model1.addElement("Diner");
model1.addElement("Evening Only");
model1.addElement("All day");
JComboBox comboBox1 = new JComboBox(model1);
panel1.add(comboBox1);

JOptionPane.showConfirmDialog(null, panel1, 
   "Restaurant", 
   JOptionPane.OK_OPTION, 
   JOptionPane.QUESTION_MESSAGE);
4

2 回答 2

2

您可以根据需要添加任意数量的组件JPanel

JPanel panel = new JPanel();

panel.add(cbox1);
panel.add(cbox2);
panel.add(cbox3);

你为什么要把所有东西都放进去JOPtionPane?您应该使用JFrame

public class BoxFrame extends JFrame {
    private JComboBox jcbox1 = new JComboBox(model1);
    private JComboBox jcbox2 = new JComboBox(model2);
    private JComboBox jcbox3 = new JComboBox(model3);
    private JLabel jlb1 = newJLabel("Question1");
    private JLabel jlb2 = newJLabel("Question2");
    private JLabel jlb3 = newJLabel("Question3");
    private JButton jbtSubmit = newJButton("Submit");

    public BoxFrame(){
        JPanel panel = new JPanel(new GridLayout(6, 1));
        panel.add(jlb1);
        panel.add(jcbox1);
        panel.add(jlb2);
        panel.add(jcbox2);
        panel.add(jlb3);
        panel.add(jcbox3);

        setLayout(new BorderLayout(5,5))
        add(panel, BorderLayout.CENTER);
        add(jbtSubmit,BorderLayout.SOUTH);

        jbtSumbit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                String cb1 = jcbox1.getItemSelected();
                String cb2 = jcbox2.getItemSelected();
                String cb3 = jcbox13.getItemSelected();

                // do something with Strings
            }
        });
    }

    public static void main(String[] args){
        BoxFrame frame = new BoxFrame();
        frame.pack();
        frame.setTitle("ComboBoxes);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationReltaiveTo(null);
        frame.setVisible();
    }

}
于 2013-11-09T00:55:11.887 回答
1

原谅一些脏代码。作为它的星期五

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")

    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Combo1");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Combo2");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Combo3");

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jPanel1.add(jLabel1);
        jPanel1.add(jComboBox1);
        jPanel1.add(jLabel2);
        jPanel1.add(jComboBox2);
        jPanel1.add(jLabel3);
        jPanel1.add(jComboBox3);

        add(jPanel1);

        pack();
    }


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;

}
于 2013-11-09T01:07:16.117 回答