![](/img/trans.png)
[英]JUnit testing a method with JOptionPane.showMessageDialog
[英]how to pass a method call to JOptionPane.showMessageDialog parameter
我有一个名为 Person 的 class ,其中有一种方法可以显示一个人的详细信息
public class Person
{
private int age = 12;
private double salary=12000;
public void show() {
System.out.println("Age : " + age);
System.out.println("Salary: "+ salary);
}
在另一个class Test
中,我想使用来自Person Class
的show()方法通过showMessageDialog
显示人员的详细信息
import javax.swing.JOptionPane;
public class Test
{
public static void main(String[] args){
Person s = new Person();
JOptionPane.showMessageDialog(null, s.show(), "Person Info", JOptionPane.PLAIN_MESSAGE);
}
最终目标是在 Joptionpane 消息框上显示人员信息我想知道是否可以在不更改 show() 方法的情况下实现**
但是代码编译失败
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method showMessageDialog(Component, Object, String, int) in the type JOptionPane is not applicable for the arguments (null, void, String, int)
任何人有任何想法将不胜感激。 非常感谢
Person.show()
正在返回void
。 所以,你得到这个错误。
showMessageDialog(Component, **Object**, String, int)
(null, **void**, String, int)
您可以从show()
返回字符串
public String show() {
return "Age : " + age + " Salary: "+ salary;
}
或者定义一个toString()
方法。
public String toString() {
return "Age : " + age + " Salary: "+ salary;
}
然后通过以下方式显示
JOptionPane.showMessageDialog(null, s.toString(), "Person Info", JOptionPane.PLAIN_MESSAGE);
或者你可以从 show 方法中显示 Dialog。
public void show() {
JOptionPane.showMessageDialog(null, "Age : " + age + " Salary: "+ salary, "Person Info", JOptionPane.PLAIN_MESSAGE);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.