如何在JTextFeild
内获取用户输入,并在另一帧中将输入设置/添加到表中?学生对象存储在ArrayList
中。数组列表将添加到表视图,并显示在另一个框架中。 StudentCntl
,StudentListUI
,StudentUI
如下所示。 StudentListUI(左)StudentUI(右)
public class StudentCntl {
private static final int STARTING_INDEX_OF_DISPLAY = 0;
StudentCntl studentCntl;
StudentList studentList;
StudentUI studentUI;
StudentListUI studentListUI;
StudentTableModel theStudentTable;
public StudentCntl() {
studentList = new StudentList();
theStudentTable = new StudentTableModel(studentList.getStudentList());
studentUI = new StudentUI(this, STARTING_INDEX_OF_DISPLAY);
studentUI.setVisible(true);
studentListUI = new StudentListUI(this, studentUI);
studentListUI.setVisible(true);
}
public StudentTableModel getStudentTableModel() {
return theStudentTable;
}
Student getStudent(int index) {
Student student = studentList.getStudentList().get(index);
return student;
}
public class StudentUI extends JFrame {
private int indexOfElementToDisplay;
private final JTextField firstNameDisplayValue = new JTextField(15);
private final JTextField lastNameDisplayValue = new JTextField(15);
private final JTextField gpaDisplayValue = new JTextField(15);
private JPanel studentPanel;
private JPanel buttonPanel;
String[] info = {firstNameDisplayValue.getText(),lastNameDisplayValue.getText(),gpaDisplayValue.getText()};
private final StudentCntl studentCntl;
public StudentUI(StudentCntl studentCntl, int startingIndexOfDisplay) {
this.studentCntl = studentCntl;
indexOfElementToDisplay = startingIndexOfDisplay;
initComponents();
setFieldView();
}
private void initComponents() {
setTitle("Student Viewer");
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
studentPanel = new JPanel(new GridLayout(5, 1));
studentPanel.add(new JLabel("First Name"));
studentPanel.add(firstNameDisplayValue);
studentPanel.add(new JLabel("Last Name"));
studentPanel.add(lastNameDisplayValue);
studentPanel.add(new JLabel("GPA"));
studentPanel.add(gpaDisplayValue);
buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton nextButton = new JButton("Next");
JButton previousButton = new JButton("Previous");
JButton deleteButton = new JButton("Delete");
JButton newButton = new JButton("New Entry");
JButton saveButton = new JButton("Save");
nextButton.addActionListener(e -> showNext(indexOfElementToDisplay));
buttonPanel.add(nextButton);
previousButton.addActionListener(e -> showPrevious(indexOfElementToDisplay));
buttonPanel.add(previousButton);
deleteButton.addActionListener(e -> showDelete(indexOfElementToDisplay));
buttonPanel.add(deleteButton);
saveButton.addActionListener(e -> addToList(info));
buttonPanel.add(saveButton);
newButton.addActionListener(e -> showNew());
buttonPanel.add(newButton);
setContentPane(new JPanel(new BorderLayout()));
getContentPane().add(studentPanel, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
private void setFieldView() {
firstNameDisplayValue.setText(studentCntl.getStudent(indexOfElementToDisplay).getFirstName());
lastNameDisplayValue.setText(studentCntl.getStudent(indexOfElementToDisplay).getLastName());
gpaDisplayValue.setText(Double.toString(studentCntl.getStudent(indexOfElementToDisplay).getGpa()));
}
private void NewEntryView() {
firstNameDisplayValue.setText(" ");
lastNameDisplayValue.setText(" ");
gpaDisplayValue.setText(" ");
}
private void addToList(String[] info){
studentCntl.studentList.getStudentList().add(new Student(info));
}
private void showNew() {
NewEntryView();
}
void refreshDisplayWithNewValues(int index) {
setFieldView();
this.repaint();
}