首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JOptionPane是/否选项确认对话框问题

JOptionPane是/否选项确认对话框问题
EN

Stack Overflow用户
提问于 2012-01-01 00:14:36
回答 2查看 272K关注 0票数 70

我已经创建了一个JOptionPane,它只有两个按钮YES_NO_OPTION

JOptionPane.showConfirmDialog弹出后,我想单击YES BUTTON继续打开JFileChooser,如果我单击了NO BUTTON,它应该会取消操作。

这看起来很简单,但我不确定我的错误在哪里。

代码片段:

if (textArea.getLineCount() >= 1) {  //The condition to show the dialog if there is text inside the textArea

    int dialogButton = JOptionPane.YES_NO_OPTION;
    JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);

    if (dialogButton == JOptionPane.YES_OPTION) { //The ISSUE is here

    JFileChooser saveFile = new JFileChooser();
    int saveOption = saveFile.showSaveDialog(frame);
    if(saveOption == JFileChooser.APPROVE_OPTION) {

    try {
        BufferedWriter fileWriter = new BufferedWriter(new FileWriter(saveFile.getSelectedFile().getPath()));
        fileWriter.write(textArea.getText());
        fileWriter.close();
    } catch(Exception ex) {

    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-02-09 12:33:07

尝尝这个,

int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(this, "Your Message", "Title on Box", dialogButton);
if(dialogResult == 0) {
  System.out.println("Yes option");
} else {
  System.out.println("No Option");
} 
票数 43
EN

Stack Overflow用户

发布于 2014-04-07 10:29:00

int opcion = JOptionPane.showConfirmDialog(null, "Realmente deseas salir?", "Aviso", JOptionPane.YES_NO_OPTION);

if (opcion == 0) { //The ISSUE is here
   System.out.print("si");
} else {
   System.out.print("no");
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8689122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档