简体   繁体   English

Java while loop with break not working as expected for JOptionPane 错误消息

[英]Java while loop with break not working as expected for JOptionPane Error Message

I am creating a Java program emulating Windows 95, including its various components.我正在创建一个模拟 Windows 95 的 Java 程序,包括其各种组件。

In Windows 95 in "My Computer", clicking the "Removable Disk" icon would cause a dialog box containing an error message to appear, and clicking "Retry" button will cause it to continuously reappear, until the user hits "Cancel" button or X.在“我的电脑”中的 Windows 95 中,单击“可移动磁盘”图标会导致出现包含错误消息的对话框,单击“重试”按钮将导致它不断重新出现,直到用户点击“取消”按钮或X。

I would like to achieve the effect as depicted in the following image, in which clicking "Retry" would make the dialog box continuously appear, until the user clicks "Cancel" or X to close it.我想实现如下图所示的效果,其中点击“重试”会使对话框不断出现,直到用户点击“取消”或X关闭它。
在此处输入图像描述

But the following code snippet I have written just would not work as expected.但是我编写的以下代码片段无法按预期工作。 Specifically the while loop with break is not working as intended.特别是带有 break 的 while 循环没有按预期工作。 The whole program would just freeze unexpectedly.整个程序会意外冻结。 There is no error in syntax found by Eclipse IDE. Eclipse IDE发现语法没有错误。 On a side note, removableDiskA is a JLabel ImageIcon object representing the icon of Removable Disk (A:);附带说明一下,removableDiskA 是一个 JLabel ImageIcon object 代表可移动磁盘 (A:) 的图标; I used.=JOptionPane,YES_OPTION because I do not know the code for X for closing the dialog box.我使用了.=JOptionPane,YES_OPTION 因为我不知道关闭对话框的 X 代码。 but I have to include both "Cancel" and X但我必须同时包含“取消”和 X

removableDiskA.addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
            if (e.getClickCount() == 2) {
              UIManager.put("OptionPane.yesButtonText", "Retry");
              UIManager.put("OptionPane.noButtonText", "Cancel");
              int responseRemovableDiskA = JOptionPane.showConfirmDialog(null, "A: \\ is not accessible. \n\nThis device is not ready.",
                "Removable Disk (A:)", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
              while (responseRemovableDiskA == JOptionPane.YES_OPTION) {
                if (responseRemovableDiskA != JOptionPane.YES_OPTION) break;
              }
              JOptionPane.showConfirmDialog(null, "A: \\ is not accessible. \n\nThis device is not ready.",
                "Removable Disk (A:)", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
            }
          }

Your while loop condition and your break condition are both exclusive.您的while循环条件和break条件都是互斥的。 This creates an infinite loop, hanging the application.这会创建一个无限循环,挂起应用程序。

To achieve what you want, call back the showConfirmDialog if the selected option is not what you expect.要实现您想要的,如果所选选项不是您所期望的,请回调showConfirmDialog

int responseRemovableDiskA = JOptionPane.YES_OPTION;
do {
    responseRemovableDiskA = JOptionPane.showConfirmDialog(null, 
                "A: \\ is not accessible. \n\nThis device is not ready.",
                "Removable Disk (A:)", 
                JOptionPane.YES_NO_OPTION, 
                JOptionPane.ERROR_MESSAGE);
} while (responseRemovableDiskA == JOptionPane.YES_OPTION);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM