关于JOptionPane中的ComboBox使用对话框

时间:2016-10-23 03:52:27

标签: java netbeans combobox dialog joptionpane

我想使用joptionpane作为Combobox的对话框,我想接受日,月和年的值。我希望所有这些都在一个对话框中。我的意思是:

 String[] date= {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
    String[] month= {"1","2","3","4","5","6","7","8","9","10","11","12"};
    String[] year={"2016","2017","2018","2019","2020"};
    JComboBox jcd = new JComboBox(date);
    JComboBox jcm = new JComboBox(date);
    JComboBox jcy = new JComboBox(date);
      jcd.setEditable(true);
      jcm.setEditable(true);
      jcy.setEditable(true);
    JOptionPane.showMessageDialog( null, jcd, "Date", JOptionPane.QUESTION_MESSAGE);
    JOptionPane.showMessageDialog( null, jcm, "Month", JOptionPane.QUESTION_MESSAGE);
    JOptionPane.showMessageDialog( null, jcy, "Year", JOptionPane.QUESTION_MESSAGE);

    int resd=(int) jcd.getSelectedItem();
    int resm=(int) jcd.getSelectedItem();
    int resy=(int) jcd.getSelectedItem();

这里的问题是我一个接一个地输入3个对话框用于输入值,我想把它作为一个具有多个组合框的单个对话框。

1 个答案:

答案 0 :(得分:5)

这是让你入门的东西。这个想法是:
创建一个JOptionPane
添加并根据需要进行配置,
使用JDialog显示JOptionPane内容:

import java.io.IOException;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

class Test {


    public static void  main(String args[]) throws IOException  {


        String[] date= {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
        String[] month= {"1","2","3","4","5","6","7","8","9","10","11","12"};
        String[] year={"2016","2017","2018","2019","2020"};
        JComboBox jcd = new JComboBox(date);
        JComboBox jcm = new JComboBox(month);
        JComboBox jcy = new JComboBox(year);

        jcd.setEditable(true);
        jcm.setEditable(true);
        jcy.setEditable(true);

        //create a JOptionPane
        Object[] options = new Object[] {};
        JOptionPane jop = new JOptionPane("Please Select",
                                        JOptionPane.QUESTION_MESSAGE,
                                        JOptionPane.DEFAULT_OPTION,
                                        null,options, null);

        //add combos to JOptionPane
        jop.add(jcd);
        jop.add(jcm);
        jop.add(jcy);

        //create a JDialog and add JOptionPane to it 
        JDialog diag = new JDialog();
        diag.getContentPane().add(jop);
        diag.pack();
        diag.setVisible(true);
    }
}

enter image description here