Java 错误:没有找到适合 showMessageDialog 的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21412173/
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
Error: no suitable method found for showMessageDialog
提问by mxgenius
I'm trying to create a temp conversion from C to F, based on a JOptionPane.showInputDialog
then will display the conversion to the user in a JOptionPane.showMessageDialog
. But I keep getting a no suitable method found error.
我正在尝试创建从 C 到 F 的临时转换,JOptionPane.showInputDialog
然后将在JOptionPane.showMessageDialog
. 但我不断收到没有合适的方法发现错误。
Here's the code:
这是代码:
package assignment2_2;
import javax.swing.JOptionPane;
public class Assignment2_2 {
public static void main(String[] args) {
// this program will convert celsius to fahrenheit
String celsiusInput = JOptionPane.showInputDialog(null,
"Enter Temperature in Celsius: " , "Temperature Converter",
JOptionPane.QUESTION_MESSAGE);
double celsius = Double.parseDouble(celsiusInput);
double fahrenheit = (9.0/5)*(celsius + 32);
JOptionPane.showMessageDialog(null, + celsius,
" when converted to Fahrenheit is: ", + fahrenheit,
JOptionPane.INFORMATION_MESSAGE);
}
}
here's the error message:
这是错误消息:
error: no suitable method found for showMessageDialog(<null>,double,String,double,int)
JOptionPane.showMessageDialog(null, + celsius, " when converted to Fahrenheit is: ", + fahrenheit, JOptionPane.INFORMATION_MESSAGE);
method JOptionPane.showMessageDialog(Component,Object,String,int,Icon) is not applicable
(actual argument double cannot be converted to int by method invocation conversion)
method JOptionPane.showMessageDialog(Component,Object,String,int) is not applicable
(actual and formal argument lists differ in length)
method JOptionPane.showMessageDialog(Component,Object) is not applicable
(actual and formal argument lists differ in length)
1 error
采纳答案by Mark
Your position of commas are the problem. It is essentially adding new parameters that shouldn't exist.
你的逗号位置是问题所在。它本质上是添加不应该存在的新参数。
Change it to this:
改成这样:
JOptionPane.showMessageDialog(null, "" + celsius + " when converted to Fahrenheit is: " + fahrenheit, JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "" + celsius + " when converted to Fahrenheit is: " + fahrenheit, JOptionPane.INFORMATION_MESSAGE);
回答by mdewitt
You need to be careful where you put your commas to separate your variables in the method call. When you are adding to a String, you do not need to put commas in between the + signs. Each comma you add tells the compiler that what follows is the next parameter.
您需要小心在方法调用中放置逗号分隔变量的位置。添加到字符串时,不需要在 + 号之间放置逗号。您添加的每个逗号都告诉编译器接下来是下一个参数。
In the following:
在下面的:
JOptionPane.showMessageDialog(null, + celsius,
" when converted to Fahrenheit is: ", + fahrenheit,
JOptionPane.INFORMATION_MESSAGE);
You can tell from the warning that the compiler is thinking that you are trying to call
您可以从警告中看出编译器认为您正在尝试调用
showMessageDialog(<null>,double,String,double,int)
Because it thinks you are inputting with the spaces it is adding celsius and fahrenheit as doubles instead of adding them to the String.
因为它认为您输入的是空格,所以它会将摄氏度和华氏度添加为双精度数,而不是将它们添加到字符串中。
Once you fix that, you will also need to add a title to the dialog in order to match one of the method calls, or you can remove the JOptionPane.INFORMATION_MESSAGE
variable because that is the default and just use the JOptionPane.showMessageDialog(Component,Object)
修复该问题后,您还需要向对话框添加标题以匹配方法调用之一,或者您可以删除JOptionPane.INFORMATION_MESSAGE
变量,因为这是默认值,只需使用JOptionPane.showMessageDialog(Component,Object)
This is how you want your method call to look:
这是您希望方法调用的外观:
JOptionPane.showMessageDialog(null, celsius + " when converted to Fahrenheit is: " +
fahrenheit);
回答by Paul Samsotha
Here are your possible methods and parameters
这是您可能的方法和参数
public static void showMessageDialog(Component parentComponent,
Object message,
String title,
int messageType)
throws HeadlessException
public static void showMessageDialog(Component parentComponent,
Object message)
throws HeadlessException
public static void showMessageDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon)
throws HeadlessException
You are currenly do this, where I see 4 commas ad 5 arguments.
您目前正在这样做,我看到 4 个逗号和 5 个参数。
showMessageDialog(null, + celsius,
" when converted to Fahrenheit is: ", + fahrenheit,
JOptionPane.INFORMATION_MESSAGE);
Look at your comma separations, and make sure the method and your parameters match one of the above.
查看您的逗号分隔符,并确保该方法和您的参数匹配上述之一。