ArrayList打印出来自gui的代码

时间:2014-04-10 10:38:41

标签: java swing arraylist jpanel actionlistener

我遇到了一些严重的麻烦。

我必须编写的程序是一个基本上要求的GUI:

  • 课程(例如:cpsc130)
  • 名称(例如:计算机编程2)
  • 您收到的课程学分(前3)
  • 您的成绩(前A,B)

我对ActionListener不是很了解,因为我不知道我在做什么。我必须为ArrayList提供信息

  • 一个ArrayList<Course>实例变量),例如courseList,用于存储您添加的课程;
  • 四个内部ActionListener类,每个类实现其方法 为actionPerformed。 Specically,
  • AddCourseListener:读取输入并创建课程对象,添加课程 对象进入courseList,并将其附加到输出区域;
  • CalGPAListener:从课程列表中读取所有课程学分和成绩,添加 他们并计算整体GPA。你假设A是4,B是3,C是2, D为1,E为0.GPA = Sum(creditpoint)= totalCredits。例如(图 1),你已经完成了3门课程:130(3学分,A学分),131(3学分,年级) B)和370(4学分,B等级),那么你的GPA =(3 * 4 + 3 * 3 + 4 * 3)/(3 + 3 + 4) = 3.3。
  • ResetInputListener:重置所有输入字段;
  • ResetOutputListener:重置输出区域。

这些都是我需要的ActionListener。到目前为止,我将向您展示我的代码。编译时会显示所有ButtonTextArea s,ActionListener是我唯一需要帮助的内容。

import javax.swing.*;
import java.awt.event.*;
import java.util.*;


public class DegreeWorksFrame extends JFrame  {
    private JLabel courseCodeLabel;
    private JTextField courseCodeField;

    private JLabel courseNameLabel;
    private JTextField courseNameField;

    private JLabel courseCreditLabel;
    private JTextField courseCreditField;

    private JLabel courseGradeLabel;
    private JTextField courseGradeField;

    private JTextArea resultArea;

    private double sum =0;    
    private double totalCredits=0;

    private String code = "";
    private String name = "";
    private String credit = "";
    private String grade = "";

    private String heading = ("Code\tName\tCredit\tGrade" + "\n");
    private ArrayList<Course> courseList;

    private JButton AddCourse;
    private JButton CalculateGPA;
    private JButton ResetInput;
    private JButton ResetOutput;

    private static final int AREA_ROWS = 15;
    private static final int AREA_COLUMNS = 35;

    private final int FRAME_HEIGHT =400;
    private final int FRAME_WIDTH = 500;

    final int FIELD_WIDTH = 30;

    public DegreeWorksFrame() {
        resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
        resultArea.setText(heading);
        resultArea.setEditable(false);

        createTextField1();
        createTextField2();
        createTextField3();
        createTextField4();

        createButtonAddCourse();
        createButtonCalculateGPA();
        createButtonResetInput();
        createButtonResetOutput();

        createPanel();

        setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }

    private void createTextField1() {
        courseCodeLabel = new JLabel("Enter Course Code: ");
        courseCodeField = new JTextField(FIELD_WIDTH);
        courseCodeField.setText(code);
    }

    private void createTextField2() {
        courseNameLabel = new JLabel("Enter Course Name: ");

        courseNameField = new JTextField(FIELD_WIDTH);
        courseNameField.setText(name);
    }

    private void createTextField3() {
        courseCreditLabel = new JLabel("Enter Course Credit: ");

        courseCreditField = new JTextField(FIELD_WIDTH);
        courseCreditField.setText(credit);
    }

    private void createTextField4() {
        courseGradeLabel = new JLabel("Enter Course Grade: ");

        courseGradeField = new JTextField(FIELD_WIDTH);
        courseGradeField.setText(grade);
    }


    private void createButtonAddCourse() {
        AddCourse = new JButton ("Add Courses: ");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }

    class addCourseListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            courseList = new ArrayList();
            String receiveList = userList.get
        }
    }

    private void createButtonCalculateGPA() {
        CalculateGPA = new JButton ("Calculate GPA");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }

    class calculateGPAListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {

        }
    }

    private void createButtonResetInput() {
        ResetInput = new JButton ("Reset Input");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }


    class resetInputListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {

        }
    }

    private void createButtonResetOutput() {
        ResetOutput = new JButton ("Reset Output");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }

    class resetOutputListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {

        }
    }

    private void createPanel() {
        JPanel panel = new JPanel();
        panel.add(courseCodeLabel);
        panel.add(courseCodeField);
        panel.add(courseNameLabel);
        panel.add(courseNameField);
        panel.add(courseCreditLabel);
        panel.add(courseCreditField);
        panel.add(courseGradeLabel);
        panel.add(courseGradeField);

        //buttons
        panel.add(AddCourse);
        panel.add(CalculateGPA);
        panel.add(ResetInput);
        panel.add(ResetOutput);

        //Scroll bar
        JScrollPane scrollPane = new JScrollPane(resultArea);
        panel.add(scrollPane);
        add(panel);
    }
}

1 个答案:

答案 0 :(得分:2)

旁注:请注意,您要在AddCourse按钮上添加多个不正确的监听器。您需要将侦听器添加到正确的按钮。

正如this answer中所述,实现侦听器的一个好方法是使用Anonymous Inner类(有关原因的更好解释,请参阅链接的答案)。

例如:

private void createButtonAddCourse() {
    AddCourse = new JButton ("Add Courses: ");
    AddCourse.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Create a new Course object and add it to the ArrayList here
        }
    });
}

...

private void createButtonCalculateGPA() {
    CalculateGPA = new JButton ("Calculate GPA");
    CalculateGPA.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Iterate over the ArrayList and calculate the overall GPA for each course
        }
    });
}

建议读数