gpt4 book ai didi

java - 关于使用对话框的 JOptionPane 中的 ComboBox

转载 作者:搜寻专家 更新时间:2023-11-01 02:03:26 26 4
gpt4 key购买 nike

我想获得一个使用 joptionpane 作为组合框的对话框,我想在其中接受日、月和年的值。我希望所有这些都在一个对话框中。我的是这样的:

 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 个用于输入值的对话框,我想将其作为具有多个组合框的单个对话框。

最佳答案

这里有一些可以帮助您入门的东西。这个想法是:
创建一个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

关于java - 关于使用对话框的 JOptionPane 中的 ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40199334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com