我想使用JOptionPane创建一个showOptionDialog,它有两个按钮:公制和英制。如果单击了Metric,则将加载Metric GUI。相反,如果单击Imperial,则将加载Imperial GUI。
我该怎么做呢?非常感谢。
发布于 2012-11-21 02:39:12
int choice = JOptionPane.showOptionDialog(null, //Component parentComponent
"Metric or Imperial?", //Object message,
"Choose an option", //String title
JOptionPane.YES_NO_OPTION, //int optionType
JOptionPane.INFORMATION_MESSAGE, //int messageType
null, //Icon icon,
{"Metric","Imperial"}, //Object[] options,
"Metric");//Object initialValue
if(choice == 0 ){
//Metric was chosen
}else{
//Imperial was chosen
}发布于 2012-11-21 02:49:13
Object[] options = {"Metric","Imperial"};
int n = JOptionPane.showOptionDialog(null,
"A Message",
"A Title",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.DEFAULT_OPTION,
null,
options,
options[1]);
System.out.println(n);
JFrame metric = new JFrame("Metric");
metric.setBounds(0, 0, 320, 240);
JFrame imperial = new JFrame("Imperial");
imperial.setBounds(0, 0, 320, 240);
if(n==0){
metric.setVisible(true);
}else if(n==1){
imperial.setVisible(true);
}else{
System.out.println("no option choosen");
}发布于 2012-11-21 02:39:02
查看http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html,它几乎包含了您需要的所有内容。
“据我所知,JOptionPane的功能非常强大,但你不能真正改变它的功能(不容易)。如果你想创建自己的自定义对话框,最好继承JDialog。”
https://stackoverflow.com/questions/13479731
复制相似问题