如何在Java中显示类似MessageBox的信息窗口?

Ser*_*pia 1 java messagebox

我正在学习Java,我不知道如何做到这一点.

我在Netbeans的表单上拖了一个按钮,双击它并创建了这个事件:

@Action
public void HelloClickMethod() 
{
    JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title."); 
}
Run Code Online (Sandbox Code Playgroud)

这是IDE提出的例外.

找不到标志.符号:showMessageDialog()

编辑1> 现在我将其更改为:

@Action
public void HelloClickMethod()
{
    JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.",JOptionPane.ERROR_MESSAGE);
}
Run Code Online (Sandbox Code Playgroud)

但是IDE说我在'this'这个词中有错误."找不到标志".我不明白.为什么它如此困难,为什么这些错误如此深奥.:P

Anz*_*rio 5

我可以想到以下原因:您可能没有"导入"包含JOptionPane的包.尝试:

 import javax.swing.*;
Run Code Online (Sandbox Code Playgroud)

在您的源文件之上.或者,使用

javax.swing.JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.", JOptionPane.ERROR_MESSAGE);
Run Code Online (Sandbox Code Playgroud)

提问者编辑后:

其他原因是方法的位置,如果您处于静态上下文中,则无法使用this.