JOptionPane showOptionDialog

て烟熏妆下的殇ゞ 提交于 2019-12-07 10:58:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!