问题最终不符合我的想法。最终评论给出正确答案......
我正在尝试输入一个程序来输入员工信息,并在被问到时输出信息。
通过在自定义showOptionDialog按钮上点击“New Employee”,系统将提示用户输入有关该员工的信息。
通过点击“检索信息”,用户可以通过输入员工的姓名或号码来检索信息。 (但是,我的代码中尚未实现此部分)
import javax.swing.*;
import java.awt.*;
public class InputList {
public static void main(String[] args) {
JFrame frame = new JFrame("EmployeeDatabase");
Object[] options = {
"New Employee",
"Retrieve Information"
};
int selectedValue = JOptionPane.showOptionDialog(
null,
"Select an option",
"Employee Database",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
options[0]
);
if(selectedValue == 0) {
String inputStringName = JOptionPane.showInputDialog(
"Employee Name",
"Last Name, First Name"
);
String inputStringIdentificationNumber = JOptionPane.showInputDialog(
"Employee Number",
"---"
);
String inputStringAge = JOptionPane.showInputDialog(
"Employee Age",
"--"
);
String inputStringWorkLevel = JOptionPane.showInputDialog(
"Reported Employee Skill Level",
"1 - Best : 20 - Worst"
);
if(inputStringName == null) {
return;
}
String employeeName = inputStringName;
String employeeIDNumber = inputStringIdentificationNumber;
String employeeAge = inputStringAge;
String employeeWorth = inputStringWorkLevel;
JOptionPane.showMessageDialog(
null,
employeeIDNumber
+ "\n"
+ employeeName
+ "\n"
+ employeeAge
+ " years old"
+ "\nRating: "
+ employeeWorth,
"Employee Information",
JOptionPane.PLAIN_MESSAGE
);
} else if(selectedValue == 1) {
JOptionPane.showMessageDialog(
null,
"Program Incomplete",
JOptionPane.PLAIN_MESSAGE
);
}
}
}
所以,我首先设置了自定义的“新员工”和“检索信息”按钮。
我尝试创建一个showOptionDialog,但每次都这样,我收到此错误消息:
InputList.java:75: error: no suitable method found for showMessageDialog(<null>,String,int)
JOptionPane.showMessageDialog(
^
method JOptionPane.showMessageDialog(Component,Object) is not applicable
(actual and formal argument lists differ in length)
method JOptionPane.showMessageDialog(Component,Object,String,int) is not applicable
(actual and formal argument lists differ in length)
method JOptionPane.showMessageDialog(Component,Object,String,int,Icon) is not applicable
(actual and formal argument lists differ in length)
1 error
我试图用frame替换第一个null,但我似乎无法理解如何将JFrame与JDialog和JOptionPane一起使用,正如Java API所使用的那样......任何帮助都表示赞赏。
由于评论无法正常运作:
Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
这是我试图使用的模板
and this is the information that's messing me up:
JOptionPane构造函数不包含此参数。代替, 在创建包含的JDialog时指定父框架 JOptionPane,您使用JDialog setLocationRelativeTo方法 设置对话框位置。