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

发布于 2024-08-18 13:45:03 字数 653 浏览 4 评论 0原文

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

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

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

这是 IDE 引发的异常。

找不到符号。符号:showMessageDialog()

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

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

但是 IDE 说我的“this”一词有错误。 “找不到符号”。我不明白。为什么它如此困难,为什么错误如此深奥。 :P

I'm learning Java and I have no idea how to do this.

I dragged a button on the form in Netbeans, double clicked it and it created this event:

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

This is the exception the IDE brings up.

Cannot find symbol. Symbol: showMessageDialog()

Edit 1>
Now I changed it to this:

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

However the IDE is saying I have an error in the word 'this'. "Cannot find symbol". I don't understand. Why is it so dificult and why are the errors so esoteric. :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两个我 2024-08-25 13:45:03

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

 import javax.swing.*;

在源文件之上。或者,使用

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

提问者编辑后:

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

I can think of the following cause: you might not be "importing" the package containing JOptionPane. Try:

 import javax.swing.*;

On top of your source file. Or, use

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

After questioner edit:

Other cause is the location of the method, if you are in an static context, you can't use this.

恍梦境° 2024-08-25 13:45:03

showMessageDialog 方法不带 3 个参数。试试这个:

  JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.", JOptionPane.ERROR_MESSAGE);

有 3 个名为 showMessageDialog 的方法,其中一个有 2 个参数(组件和消息)、4 个参数(组件、消息、标题、消息类型)和 5 个参数(组件、消息、标题、消息类型、图标)。

The showMessageDialog method does not take 3 parameters. Try this:

  JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.", JOptionPane.ERROR_MESSAGE);

There are 3 methods named showMessageDialog, one with 2 parameters (component and message), 4 parameters (component, message, title, message type) and 5 parameters (component, message, title, message type, icon).

绝影如岚 2024-08-25 13:45:03

这工作正常:

JOptionPane.showMessageDialog(null,"ErrorMSG", "Title!", JOptionPane.WARNING_MESSAGE)

This works fine:

JOptionPane.showMessageDialog(null,"ErrorMSG", "Title!", JOptionPane.WARNING_MESSAGE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文