简体   繁体   English

当使用“ try,catch”块来处理异常时,如何在GUI上输出错误消息?

[英]when using a 'try, catch' block to handle exceptions, how do I output an error message on a GUI?

I'm writing a GUI program and I'm using a 'try, catch' block to do exception handling. 我正在编写一个GUI程序,并且正在使用“ try,catch”块进行异常处理。 I know that you can use a 'System.out.print()' inside the catch block to output an error message on the console, but how do you output an error message on the GUI? 我知道您可以在catch块中使用'System.out.print()'在控制台上输出错误消息,但是如何在GUI上输出错误消息呢?

I used 'JLabel' to create an error message, and I'm trying to add that label to my JPane by putting a line of code inside of the catch block but it's not working so I'm kind of stuck here, I can only output errors to the console which the end user would never see. 我使用“ JLabel”创建了一条错误消息,并且我试图通过在catch块中放置一行代码来将该标签添加到JPane中,但是它无法正常工作,所以我有点卡在这里,我只能将错误输出到最终用户永远不会看到的控制台。 Any help/advice is very greatly appreciated. 任何帮助/建议都将不胜感激。

    errorMessage = new JLabel("<html><b>An error has occured. Please remember that you cannot enter alphabetic characters in any of the data fields, "
            + "also you cannot leave any of the fields blank and the probability data must be a decimal number less than '1' and greater than '0'</html>");
    errorMessage.setBounds(10,150,410,180);
    errorMessage.setFont(defaultFont);


    JButton beginSim = new JButton("Begin simulation");
    beginSim.setFont(defaultFont);
    beginSim.setBounds(10, 178, 160, 25);
    inputPane.add(beginSim);
    beginSim.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
            try
            {
                PlaneSimulator newSimulation = new PlaneSimulator(Integer.parseInt(txtLandingTime.getText()), Integer.parseInt(txtTakeoffTime.getText()), 
                    Double.parseDouble(txtLandingProb.getText()), Double.parseDouble(txtTakeoffProb.getText()), Integer.parseInt(txtTotalTime.getText()),
                        Integer.parseInt(txtCrashTime.getText()));
            }
            catch (NumberFormatException e1)
            {
                inputPane.add(errorMessage);    
            }
        }
    });

This can by done by showing a dialog message. 这可以通过显示对话框消息来完成。 Take a look at the documentation to see how you can display a dialog . 查看文档 ,了解如何显示dialog

EDIT: 编辑:

Since you don't want any pop-ups, you could add a JTextArea at the bottom of the screen which only becomes visible when there are any errors, and you could show the error message there, 由于您不希望出现任何弹出窗口,因此可以在屏幕底部添加一个JTextArea,仅当出现任何错误时该JTextArea才可见,并且可以在此处显示错误消息,

The label is being added to the panel, but you won't see any changes to the panel unless you alter its attributes like its width and height somehow (resizing/minimizing/maximizing the window that the panel is in, for example). 标签已添加到面板中,但是除非以某种方式更改其宽度和高度之类的属性(例如,调整/最小化/最大化面板所在的窗口),否则您将看不到面板的任何更改。

The easiest way to solve the issue is to call revalidate on the panel, which will validate the change made to the panel and make it visible to the user. 解决此问题的最简单方法是在面板上调用revalidate ,这将验证对面板所做的更改并使它对用户可见。

catch (NumberFormatException e1)
{
    inputPane.add(errorMessage);
    inputPane.revalidate();
}

In some cases you may need to repaint the panel as well. 在某些情况下,您可能还需要repaint面板。

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

相关问题 如何在GUI上使用try / catch块来测试Deck类上的方法 - how do I use a try/catch block on a GUI to test out methods on a Deck Class 使用try / catch块捕获来自文件输入的异常 - Using a try/catch block to catch exceptions with input from a file 如何修复do-while循环中的逻辑,然后应用try-catch块来捕获并显示来自另一个类的错误消息? - How to fix the logic in my do-while loop, and then applying the try-catch block to catch and display an error message from another class? 在 Java 中使用 Exceptions 专门创建自己的异常如何正确使用 try、catch、finally 命令? - In Java using Exceptions specifically creating own exception how do I properly use the try, catch, finally command? Java-如何正确处理try catch块 - Java - How to properly handle a try catch block 我如何在try and catch块内进行输入验证? - how do i do input validation inside a try and catch block? 程序在不使用try and catch时报告异常 - Program reports exceptions when not using try and catch 如何测试 Mockito 中的 try and catch 块? - How do I test a try and catch block in Mockito? 如何将我的主要方法放入try catch块? - How do I place my main method into a try catch block? 如何从try / catch块中获取变量? - How do I get a variable out of a try/catch block?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM