我正在创建一个简单的GUI窗口来管理教师列表, 我遇到的错误是:
--------------------Configuration: FacultyListFrame1 - JDK version 1.7.0_02 <Default> - <Default>--------------------
C:\Users\lm_b116\Documents\JCreator Pro\MyProjects\Myfaculty\FacultyListFrame1.java:175: error: no suitable constructor found for Faculty(String,Name,MyDate,boolean)
Facultylistframe[noOfFaculty++] = new Faculty(ssnS,new Name(firstName,lastName), new MyDate(month, day, year),selectedStatus);
^
constructor Faculty.Faculty(int,Name,MyDate,double,boolean) is not applicable
(actual and formal argument lists differ in length)
constructor Faculty.Faculty(int,Name,MyDate) is not applicable
(actual and formal argument lists differ in length)
Note: C:\Users\lm_b116\Documents\JCreator Pro\MyProjects\Myfaculty\FacultyListFrame1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
我真的很感激任何帮助,
FacultyListFrame1的代码是:
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.awt.*;
public class FacultyListFrame1 extends JFrame {
private JButton store;
private JTextArea outputTextArea;
private JTextField fname, lname, dmonth, dday, dyear, ssalary,nssn,fstatus;
Faculty Facultylistframe[];
JComboBox statusSelection;
int noOfFaculty;
String[] status={"Fulltime","Parttime"};
public FacultyListFrame1(int num) {
super("Faculty List");
setLayout(new FlowLayout());
Facultylistframe = new Faculty[num];
noOfFaculty = 0;
JLabel jl1 = new JLabel("First Name:");
add(jl1);
fname = new JTextField(22);
add(fname);
JLabel jl2 = new JLabel("Last Name");
add(jl2);
lname = new JTextField(22);
add(lname);
JLabel jl7 = new JLabel("SSN:");
add(jl7);
nssn = new JTextField(20);
add(nssn);
JLabel jl8 = new JLabel("Salary:");
add(jl8);
ssalary = new JTextField(20);
add(ssalary);
JLabel jl9 = new JLabel("Status:");
add(jl9);
statusSelection= new JComboBox(status);
statusSelection.setMaximumRowCount(2);
add(statusSelection);
JLabel jl4 = new JLabel("Birthdate: MM");
add(jl4);
dmonth = new JTextField(3);
add(dmonth);
JLabel jl5 = new JLabel("DD");
add(jl5);
dday = new JTextField(3);
add(dday);
JLabel jl6 = new JLabel("Year");
add(jl6);
dyear = new JTextField(5);
add(dyear);
store = new JButton("Store Data");
store.addActionListener(new buttonEvent());
add(store);
outputTextArea = new JTextArea();
add(outputTextArea);
}
public static void main(String[] args) {
final int SIZEOFARRAY = 10;
FacultyListFrame1 plFrame = new FacultyListFrame1(SIZEOFARRAY);
plFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
plFrame.setSize(380, 500);
plFrame.setLocation(500, 200);
plFrame.setVisible(true);
}
private class buttonEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() != store) return;
{
String firstName = fname.getText();
if (firstName.length() == 0) {
JOptionPane.showMessageDialog(null, "No first name",
"Warning", JOptionPane.WARNING_MESSAGE);
return;
}
String lastName = lname.getText();
if (lastName.length() == 0) {
JOptionPane.showMessageDialog(null, "No last name",
"Warning", JOptionPane.WARNING_MESSAGE);
return;
}
String ssnS = nssn.getText();
int ssn;
if (ssnS.length() == 0) ssn = -1;
else
try {
ssn = Integer.parseInt(ssnS);
} catch (NumberFormatException ne) { ssn = -1; }
if (ssn < 0) {
JOptionPane.showMessageDialog(null, "Invalid SSN",
"Warning", JOptionPane.WARNING_MESSAGE);
return;
}
String salaryS = ssalary.getText();
int salary;
if (salaryS.length() == 0) ssn = -1;
else
try {
salary = Integer.parseInt(ssnS);
} catch (NumberFormatException ne) { ssn = -1; }
if (salary < 0) {
JOptionPane.showMessageDialog(null, "Invalid Salary",
"Warning", JOptionPane.WARNING_MESSAGE);
return;
}
String monthS = dmonth.getText();
int month, day, year;
try {
month = Integer.parseInt(monthS);
} catch (NumberFormatException ne) { month = 0; }
String dayS = dday.getText();
try {
day = Integer.parseInt(dayS);
} catch (NumberFormatException ne) { day = 0; }
String yearS = dyear.getText();
try {
year = Integer.parseInt(yearS);
} catch (NumberFormatException ne) { year = -1; }
int ret = MyDate.checkDate(month, day, year);
if (ret != 0) {
String t="";
switch (ret) {
case 1: t = "Month"; break;
case 2: t = "Day"; break;
case 3: t = "Year"; break;
}
JOptionPane.showMessageDialog(null, t + " is invalid",
"Warning", JOptionPane.WARNING_MESSAGE);
return;
}
fname.setText("");
lname.setText("");
nssn.setText("");
dmonth.setText("");
dday.setText("");
dyear.setText("");
ssalary.setText("");
int j=statusSelection.getSelectedIndex();
boolean selectedStatus=true;
if(j>0) selectedStatus=false;
if (noOfFaculty < Facultylistframe.length)
Facultylistframe[noOfFaculty++] = new Faculty(ssnS,new Name(firstName,lastName), new MyDate(month, day, year),selectedStatus);
else
JOptionPane.showMessageDialog(null, "Faculty List is full", "Warning", JOptionPane.WARNING_MESSAGE);
outputTextArea.setText("Last-Name\t First-Name\t SSN\t Salary\t Birth-Date\n");
for (int i=0; i<Facultylistframe.length; i++) {
if (Facultylistframe[i]!=null)
outputTextArea.append(Facultylistframe[i].toString() + "\n");
}
}
}
}
}
............................................... ........................................ 遗憾
这是Faculty的代码
public class Faculty extends Person{
private double salary;
private boolean fullTime;
public Faculty (int ssn, Name name, MyDate birth)
{
super(ssn, name, birth);
}
public Faculty (int ssn, Name name, MyDate birth, double salary, boolean f)
{
super(ssn, name, birth);
this.salary = salary;
fullTime = f;
}
public void setSalary (double salary)
{
this.salary = salary;
}
public double getSalary()
{
return salary;
}
public void setFullTime (boolean f)
{
fullTime = f;
}
public boolean getFullTime()
{
return fullTime;
}
public String toString()
{
String faculty = new String ("");
if (fullTime == false)
faculty = super.toString() + "\t" + salary + "\t part time ";
else
faculty = super.toString() + "\t" + salary + "\t full time ";
return faculty;
}
}
答案 0 :(得分:1)
您正在尝试传递4个参数,但根据错误消息,Faculty
的构造函数需要3个或5个参数。
要么不将selectedStatus
变量传递给构造函数,要匹配Faculty(int,Name,MyDate)
,要么在double
之前包含selectedStatus
值以匹配Faculty(int,Name,MyDate,double,boolean)
。
另一种选择:创建4参数构造函数以匹配您尝试调用的内容:Faculty(int,Name,MyDate,boolean)
。