如何在 Java 中显示类似 MessageBox 的信息窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2076231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get a MessageBox like information window to appear in Java?
提问by Sergio Tapia
I'm learning Java and I have no idea how to do this.
我正在学习 Java,但我不知道该怎么做。
I dragged a button on the form in Netbeans, double clicked it and it created this event:
我在 Netbeans 的表单上拖动了一个按钮,双击它并创建了这个事件:
@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.
这是 IDE 提出的例外。
Cannot find symbol. Symbol: showMessageDialog()
找不到标志。符号:showMessageDialog()
Edit 1>Now I changed it to this:
编辑 1>现在我把它改成这样:
@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
但是,IDE 说我在“this”这个词中有错误。“找不到标志”。我不明白。为什么它如此困难,为什么错误如此深奥。:P
采纳答案by Anzurio
I can think of the following cause: you might not be "importing" the package containing JOptionPane. Try:
我可以想到以下原因:您可能没有“导入”包含 JOptionPane 的包。尝试:
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
.
另一个原因是方法的位置,如果您在静态上下文中,则不能使用this
.
回答by Vincent Ramdhanie
The showMessageDialog method does not take 3 parameters. Try this:
showMessageDialog 方法不带 3 个参数。尝试这个:
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).
showMessageDialog 有 3 个方法,一个有 2 个参数(组件和消息),4 个参数(组件、消息、标题、消息类型)和 5 个参数(组件、消息、标题、消息类型、图标)。
回答by Kaito
This works fine:
这工作正常:
JOptionPane.showMessageDialog(null,"ErrorMSG", "Title!", JOptionPane.WARNING_MESSAGE)