如何在 Java 中显示类似 MessageBox 的信息窗口?
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以想到以下原因:您可能没有“导入”包含 JOptionPane 的包。尝试:
在源文件之上。或者,使用
提问者编辑后:
其他原因是方法的位置,如果您处于静态上下文中,则无法使用
this
。I can think of the following cause: you might not be "importing" the package containing JOptionPane. Try:
On top of your source file. Or, use
After questioner edit:
Other cause is the location of the method, if you are in an static context, you can't use
this
.showMessageDialog 方法不带 3 个参数。试试这个:
有 3 个名为 showMessageDialog 的方法,其中一个有 2 个参数(组件和消息)、4 个参数(组件、消息、标题、消息类型)和 5 个参数(组件、消息、标题、消息类型、图标)。
The showMessageDialog method does not take 3 parameters. Try this:
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).
这工作正常:
This works fine: