我有具有几个字段由空格这样分离的输入文件:
10 416-555-6666伯吉斯15
15 905-777-8888托马斯10
20 905-111-2222莫里斯5
从对象获取数据在一个ArrayList到GUI
我这是关系到具有ID,电话,姓名,工作多年的输入文件一个单独的类文件“录音”。
在我的主要方法中,我创建了一个名为employeeList的'Record'对象的ArrayList。 ArrayList由一个while循环填充,该循环将读取的输入文件分解为由Record构造函数定义的类别。
我的下一个任务是创建一个可以在输入文件中显示和循环的GUI,但是我在设计'记录'对象显示在文本字段中的方式时遇到问题。我已经把它贴在下面的图像显示什么,我试图让:
example drawing
接下来的员工按钮会循环对ArrayList中的下一个项目,成为下一个记录对象。我在下面附上了我的代码,并希望对下一步做什么有所建议(制作一个显示项目ArrayList的GUI)。
package javaapplication4;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class App {
private JFrame f;
private JPanel p;
private JButton b1;
private JLabel lab;
private JTextArea empID;
private JTextArea telephone;
private JTextArea name;
private JTextArea yearsWorked;
public App() {
gui();
}
public static void main(String[] args) throws FileNotFoundException {
File inputFile = new File("Emp.txt");
Scanner in = new Scanner(inputFile);
ArrayList<Record> employeeList = new ArrayList<Record>();
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]);
employeeList.add(new Record(empId, telephone, name, years_of_Work));
}
new App();
}
public void gui() {
f = new JFrame("UpdateEmp");
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
b1 = new JButton("Test");
lab = new JLabel();
empID = JTextField();
telephone = JTextField();
name = JTextField();
yearsWorked = new JTextField();
p.add(b1);
p.add(lab);
f.add(p);
}
}
该应用程序需要显示employeeList。所以你应该将它传递给它的构造函数,并将其存储到一个字段中。 –
有很多方法可以做到这一点,取决于您是否希望快速或精心设计。 – ChiefTwoPencils