如何在Swing中打开警告/信息/错误对话框?

2022-08-31 22:21:10

如何在Swing中打开警告/信息/错误对话框?

我需要带有“确定”按钮和“红十字”图像的标准错误对话框。即模拟org.eclipse.jface.dialogs.MessageDialog.openError()


答案 1

请参见如何创建对话框

您可以使用:

JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");

您还可以将符号更改为错误消息或警告。例如,参见 JOptionPane Features


答案 2
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ErrorDialog {

  public static void main(String argv[]) {
    String message = "\"The Comedy of Errors\"\n"
        + "is considered by many scholars to be\n"
        + "the first play Shakespeare wrote";
    JOptionPane.showMessageDialog(new JFrame(), message, "Dialog",
        JOptionPane.ERROR_MESSAGE);
  }
}

推荐