如何在一帧中获取用户输入并将其添加到数组列表以在另一帧上显示

时间:2018-10-23 15:26:19

标签: java swing object arraylist jframe

如何在JTextFeild内获取用户输入,并在另一帧中将输入设置/添加到表中?学生对象存储在ArrayList中。数组列表将添加到表视图,并显示在另一个框架中。 StudentCntlStudentListUIStudentUI如下所示。 StudentListUI(左)StudentUI(右)

enter image description here

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();


}

0 个答案:

没有答案