问题
I want to create a showOptionDialog using JOptionPane that has two buttons: Metric and Imperial. If say, Metric is clicked on, the Metric GUI will load. Conversely, if Imperial is clicked on, then the Imperial GUI will load.
How do I do this? Many thanks.
回答1:
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
}
回答2:
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");
}
回答3:
Check out http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html it pretty much has everything you would need.
"As i understand it, JOptionPane is great for what it can do, but you can't really change the functionality beyond that (not easily). JDialog is better to inherit from if you want to create your own custom Dialogs"
来源:https://stackoverflow.com/questions/13479731/joptionpane-showoptiondialog