Java Swing 的错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11563555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:35:37  来源:igfitidea点击:

Error messages with Java Swing

javaswingnetbeans

提问by Poliquin

I have a query on handling error conditions with Java Swing.

我有一个关于使用 Java Swing 处理错误条件的查询。

I am using Netbeans to develop a simple Java Swing app. It is to load in a text file, then run calculation based on the numbers found in the text file. The main Swing class holds the JFrames and JPanels.

我正在使用 Netbeans 开发一个简单的 Java Swing 应用程序。它是加载一个文本文件,然后根据文本文件中找到的数字运行计算。主要的 Swing 类包含 JFrames 和 JPanels。

I have the file loading code as a separate class file. It returns the number of lines read and a List of numbers back to the main Swing app.

我将文件加载代码作为单独的类文件。它将读取的行数和数字列表返回到主 Swing 应用程序。

I realised that if the file reading fails (i.e. try -> catch (Exception ex)), the entire app will crash. What's the best way to handle errors resulting from my scenario above? That is to say, the file loading code crashes and I don't want the entire program to crash. I want the program to say the file is corrupted and wait for user to load new file.

我意识到如果文件读取失败(即 try -> catch (Exception ex)),整个应用程序将崩溃。处理上述场景导致的错误的最佳方法是什么?也就是说,文件加载代码崩溃了,我不希望整个程序崩溃。我希望程序说文件已损坏并等待用户加载新文件。

Any thoughts?

有什么想法吗?

Yakult

养乐多

回答by ewok

when you catch the exception, run:

当您捕获异常时,运行:

JOptionPane.showMessageDialog("File is corrupted. Please select a new file.");

Then display the file dialog again.

然后再次显示文件对话框。

It's probably best to do this as a loop that continues while the the file is not valid. If there is a corruption, then rather than throwing an exception, set a boolean flag and loop as long as the flag is set. That way, when a good file is found, the while loop will terminate.

最好将此作为在文件无效时继续的循环来执行。如果存在损坏,那么与其抛出异常,不如设置一个布尔标志并在设置标志时循环。这样,当找到一个好的文件时,while 循环将终止。

Example:

例子:

public static void main(String[] args){
    boolean goodFile = false;

    while (!goodFile){
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog();

        goodFile = processFile(chooser.getSelectedFile());
    }
}

private boolean processFile(File file){
    //do you stuff with the file

    //return true if the processing works properly, and false otherwise    
}

回答by Samitha

yeah the problem is with your IO reading concept the while loop is reading to the end of the file and so on.. to prevent that u can use a buffered reader and use this code

是的,问题出在你的 IO 读取概念上,while 循环正在读取到文件的末尾等等......以防止你可以使用缓冲阅读器并使用此代码

String line = null
while((line = reader.readLine())!= null) {
// do stuf
}

if you are having this problem with processing the read line all you need is to create a exception class of your own by extending Exception class and throw that exception in your catch block after setting the message to your custom exception class you can set that message in to

如果您在处理读取行时遇到此问题,您只需要通过扩展 Exception 类创建自己的异常类,并在将消息设置为自定义异常类后在 catch 块中抛出该异常,您可以在到

 JOptionPane.showMessageDialog(null,"message here"); //showMessageDialog is a static method

ok

回答by GingerHead

You just catch the exception and put condition in the catch block. If the file contains other content that your application is intended to handle then you could call your method which will re-handle another file.

您只需捕获异常并将条件放入 catch 块中。如果文件包含您的应用程序打算处理的其他内容,那么您可以调用将重新处理另一个文件的方法。

The main handling of your new process of the new file manipulation will start from your catch block. So in this way you are using java thrown exception to restart your application in a brand new way other than relaunching your app from the zero level.

新文件操作的新进程的主要处理将从您的 catch 块开始。因此,通过这种方式,您将使用 java 抛出的异常以一种全新的方式重新启动您的应用程序,而不是从零级别重新启动您的应用程序。