当我单击“确定”和单击“取消”时,您可以选择另一个选项吗?如果我有这样的JOptionPane,如果我可以这样做,我如何实现它?
JOptionPane.showConfirmDialog(frame,"Message",JOptionPane.OK_CANCEL_OPTION);发布于 2014-01-26 08:19:32
您可以作为一个JOptionPane.showConfirmDialog()从int获取返回值,并将其与JOptionPane中可用的常量进行比较,以决定下一步要做什么。
int action = JOptionPane.showConfirmDialog(...);
if(action == JOptionPane.CANCEL_OPTION){ // something } SSCCE:
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class JExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
int action = JOptionPane.showConfirmDialog(null,
"Click Something, Moron!",
"Y U NO Click!",
JOptionPane.YES_NO_CANCEL_OPTION);
if(action == JOptionPane.YES_OPTION){
System.out.println("YES!");
}else if(action == JOptionPane.NO_OPTION){
System.out.println("NO!");
}else{
System.out.println("CANCEL!");
}
}
});
}
}发布于 2014-01-26 08:19:40
你读了JavaDocs
public static int showConfirmDialog(Component parentComponent, Object message) throws HeadlessException弹出一个对话框,选项是“是”、“否”和“取消”;标题是“选择选项”。 参数:parentComponent-确定显示对话框的框架;如果为null,或者如果parentComponent没有帧,则使用默认框架。message-要显示的对象 返回: 表示用户选择的选项的整数
当这对您没有帮助时,您可以查看教程,例如如何制作对话框。
发布于 2017-08-25 08:50:52
你也可以这样做。
if(JOptionPane.showConfirmDialog(null, "Bla bla", "Bla bla", JOptionPane) == 0){
System.out.println("YES");
//The value zero represents index of first option which will be the YES option
}
else if(JOptionPane.showConfirmDialog(null, "Bla bla", "Bla bla", JOptionPane) == 1){
System.out.println("NO");
//The value one represents index of second option which will be the NO option
}您可以移除索引号,并将其替换为您所拥有的格式,使其如下所示:
if(JOptionPane.showConfirmDialog(null, "Bla bla", "Bla bla", JOptionPane) == JOptionPane.YES_OPTION){
System.out.println("YES");
option
}
else if(JOptionPane.showConfirmDialog(null, "Bla bla", "Bla bla", JOptionPane) == JOptionPane.NO_OPTION){
System.out.println("NO");
option
}您可以访问此YouTube链接以获得更多帮助JOptionPane教程。
https://stackoverflow.com/questions/21361221
复制相似问题