在Java Swing中显示警报对话框

在Java Swing中显示警报对话框,java,swing,Java,Swing,我想在出现异常时发出警报,如在代码中: try { //the code here } catch(Exception e) { //show an alert dialog here } 我需要一个示例或代码片段。您可以将JOptionPane.showMessageDialog与警告消息一起使用: JOptionPane.showMessageDialog(yourFrame, "WARNING.", "Warning", JOptionPane.W

我想在出现异常时发出警报,如在代码中:

try
{
    //the code here
}
catch(Exception e)
{
    //show an alert dialog here
}

我需要一个示例或代码片段。

您可以将
JOptionPane.showMessageDialog
警告消息一起使用:

JOptionPane.showMessageDialog(yourFrame,
    "WARNING.",
    "Warning",
    JOptionPane.WARNING_MESSAGE);
有关如何创建对话框的更多信息。

尝试以下方法:

JOptionPane.showMessageDialog(null, "My Goodness, this is so concise");
如果静态导入JOptionPane.showMessageDialog,这将进一步减少到

showMessageDialog(null, "This language just gets better and better!");

请参见教程我认为答案的第二部分是不必要的,您可以使用几乎所有的方法来实现这一点,但事实上您不应该这样做(例如,请参见此处:,最后一段)。如果您目前无法访问frame,则可以使用null作为第一个参数