我有一项任务,我必须设计一个小程序,循环显示文本文件中的员工信息。我认为我从文本文件中读取输入的代码是正确的,但是我无法通过GUI显示信息。我认为主要问题是我最初将其设计为swing应用程序,但我无法将其转换为Applet。任何建议都会有所帮助。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class UpdateEmp extends JApplet {
static final ArrayList<Record> recordList = new ArrayList<>();
static int currentRecord = 0;
private JTextArea empIdField;
private JTextArea telephoneField;
private JTextArea nameField;
private JTextArea yowField;
public class display extends JPanel {
public display() {
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
getContentPane().setLayout(layout);
empIdField = new JTextArea();
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 0;
add(empIdField, layoutConstraints);
telephoneField = new JTextArea();
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 1;
add(telephoneField, layoutConstraints);
nameField = new JTextArea();
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 2;
add(nameField, layoutConstraints);
yowField = new JTextArea();
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 3;
add(yowField, layoutConstraints);
}
}
public void init() {
System.out.println("Initializing");
display d = new display();
getContentPane().add(d);
}
public void start() {
}
public void stop() {
}
public void destory() {
}
public void RecordArray() throws FileNotFoundException {
File inputFile = new File("Emp.txt");
Scanner in = new Scanner(inputFile);
while(in.hasNextLine()) {
String line = in.nextLine();
String[] peopleInfo = line.split("\\s");
int empId = Integer.parseInt(peopleInfo[0]);
String telephone = peopleInfo[1];
String name = peopleInfo[2];
int years_of_Work = Integer.parseInt(peopleInfo[3]);
recordList.add(new Record(empId, telephone, name, years_of_Work));
}
}
private void showRecord() {
assert currentRecord >= 0 && currentRecord < recordList.size();
Record record = recordList.get(currentRecord);
empIdField.setText(Integer.toString(record.getEmpId()));
telephoneField.setText(record.getTelephone());
nameField.setText(record.getName());
yowField.setText(Integer.toString(record.getYears()));
}
public static void main(String[] args) throws FileNotFoundException {
//In replacement of app.ShowGui();
JFrame frame = new JFrame("UpdateEMP");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UpdateEmp app = new UpdateEmp();
app.RecordArray();
app.init();
app.showRecord();
System.out.println(recordList.get(currentRecord).getEmpId());
}
}