编写一个方法,从两个不同的数组中提取信息并将其打印到JTextField中

时间:2014-03-31 22:31:14

标签: java arrays string methods jtextarea

这个项目的目标是创建四个类:Student类,GradStudent类,Manager类和GUI类。在GUI中有两个单选按钮:一个用于学生,一个用于GradStudent。根据选择的是哪一个,Manager类应该负责通过使用两个数组来创建和存储Student或GradStudent对象。我还在GUI中添加了一个按钮,用于打印所有以“计算机科学”为主要内容的学生。但是,我在如何编写此方法时遇到了麻烦。任何有关此问题的帮助将不胜感激。我将在下面发布我的课程。谢谢!

学生班级

public class Student {
    protected String name;
    protected String address;
    protected double balance;
    protected String major;

    // Constructs fields
    public Student(String name, String address, String balance, String major) {
        this.name = name;
        this.address = address;
        this.major = major;
        try {
            this.balance = Double.parseDouble(balance);
        }
        catch(NumberFormatException e) {
            this.balance = 0;
        }
    }

    public String setName(String name) {
        return name;
    }

    public String setAddress(String address) {
        return address;
    }

    public double setBalance(double balance) {
        return balance;
    }

    public String setMajor(String major) {
        return major;

    }

    public String toString() {
        return ("Name: " + this.name + " Address: " + this.address
                + " Balance: " + this.balance + " Major: " + this.major);
    }
}

GradStudent Class

public class GradStudent extends Student {

    public GradStudent(String name, String address, String balance, String major) {
        super(name, address, balance, major);
    }



    public String toString() {
        return ("Name: " + this.name + " Address: " + this.address
                + " Balance: " + this.balance + " Major: " + this.major);
    }
}

经理类

public class Manager {
    public Student[] students = new Student[50];
    public int counter1 = 0;

    public GradStudent[] gradStudents = new GradStudent[50];
    public int counter2 = 0;

    public Manager() {

    }

    public void addStudent(String name, String address, String balance, String major) {
        Student student1 = new Student(name, address, balance, major);
        students[counter1] = student1;
                counter1++;
    }
    public String getLastStudent() {
        return "Student added: " + students[counter1-1] +"\n";
    }

    public void addGradStudent(String name, String address, String balance, String major) {
        GradStudent student2 = new GradStudent(name, address, balance, major);
        gradStudents[counter2] = student2;
                counter2++;
    }
    public String getLastGradStudent() {
        return "Graduate Student added: " + gradStudents[counter2-1] +"\n";
    }

    public String getAverageBalance(){
        double sum = 0;
        for (int i = 0; i < counter1; i++) {
            sum += students[i].setBalance(counter1);
        }

        for(int i = 0; i < counter2; i++) {
            sum+= students[i].setBalance(counter2);
        }

        double average = sum / (counter1+counter2);
        return average+"";
        }

}

GUI类

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class GUI extends JFrame {
    private JRadioButton jrbStudent = new JRadioButton("Student");
    private JRadioButton jrbGraduate = new JRadioButton("Graduate");
    private JTextField name = new JTextField(20);
    private JTextField address = new JTextField(20);
    private JTextField balance = new JTextField(20);
    private JTextField major = new JTextField(20);
    private JButton jbtSubmit = new JButton("Submit");
    private JTextArea echoStudent = new JTextArea();

    private JButton printStudentNames = new JButton("Print Student's Names");
    private JButton printGradStudentNames = new JButton(
            "Print Graduate Student's Names");
    private JButton calcBalance = new JButton(
            "Calculate Average Balance of All Students");
    private JButton compSciMajor = new JButton(
            "Displays Computer Science Major Students");

    private Manager m1 = new Manager();

    public GUI() {

        // Creates panel P1 and adds the components
        JPanel p1 = new JPanel(new GridLayout(9, 1, 10, 10));
        p1.add(new JLabel("Name: "));
        p1.add(name);
        p1.add(new JLabel("Address: "));
        p1.add(address);
        p1.add(new JLabel("Balance: "));
        p1.add(balance);
        p1.add(new JLabel("Major: "));
        p1.add(major);
        p1.add(jrbStudent);
        p1.add(jrbGraduate);
        p1.add(new JLabel("Submit Button: "));
        p1.add(jbtSubmit);
        p1.add(printStudentNames);
        p1.add(printGradStudentNames);
        p1.add(calcBalance);
        p1.add(compSciMajor);
        p1.add(new JLabel("Submitted Text: "));

        // Creates a radio-button group to group both buttons
        ButtonGroup group = new ButtonGroup();
        group.add(jrbStudent);
        group.add(jrbGraduate);

        // Adds the panel and text area to the frame
        add(p1);
        p1.add(echoStudent);
        echoStudent.setEditable(false);

        // Creates a listener and registers it with the submit button
        SubmitListener l1 = new SubmitListener();
        jbtSubmit.addActionListener(l1);

        // Creates a listener and registers it with the radio buttons
        JRBListener l2 = new JRBListener();
        jrbStudent.addActionListener(l2);
        jrbGraduate.addActionListener(l2);

        // Creates a listener and registers it with the PrintStudentNames button
        StudentListener l3 = new StudentListener();
        printStudentNames.addActionListener(l3);

        // Creates a listener and registers it with the PrintGradStudentNames button
        GradStudentListener l4 = new GradStudentListener();
        printGradStudentNames.addActionListener(l4);

        // Creates a listener and registers it with the calcBalance button
        CalcBalanceListener l5 = new CalcBalanceListener();
        calcBalance.addActionListener(l5);
    }

    // Class to handle the submit button
    class SubmitListener implements ActionListener {
        public void actionPerformed(ActionEvent a) {
            Student[] students = new Student[50];
            int arrayLocation = 0;

            Student student1 = new Student(name.getText(), address.getText(),
                    balance.getText(), major.getText());
            // Checks remaining array space
            if (arrayLocation < 50) {
                students[arrayLocation] = student1;
                ++arrayLocation;
            }
            // Echos back entered text while storing the previous text
            echoStudent.setText(echoStudent.getText() + "\n"
                    + student1.toString());
        }
    }

    // Class to handle the radio buttons
    class JRBListener implements ActionListener {
        public void actionPerformed(ActionEvent b) {
            if (b.getSource() == jrbStudent) {
                m1.addStudent(name.getText(), address.getText(),
                        balance.getText(), major.getText());
                echoStudent
                        .setText("Created Student: \n" + m1.getLastStudent());
            }

            if (jrbGraduate.isSelected()) {
                m1.addGradStudent(name.getText(), address.getText(),
                        balance.getText(), major.getText());
                echoStudent.setText("Created Graduate Student: \n"
                        + m1.getLastGradStudent());
            }
        }
    }
    // Class to handle the "Print Student's Names" button
    class StudentListener extends Manager implements ActionListener {
        public void actionPerformed(ActionEvent c) {

            echoStudent.setText(m1.students[counter1].name);

        }

    }
    // Class to handle the "Print Graduate Student's Names" button
    class GradStudentListener extends Manager implements ActionListener {
        public void actionPerformed(ActionEvent d) {

            echoStudent.setText(m1.gradStudents[counter2].name);

        }

    }
    // Class to handle the "Calculate Average Balance of All Students" button
    class CalcBalanceListener extends Manager implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            echoStudent.setText(getAverageBalance());
        }
    }
    // Class to handle the "Displays Computer Science Major Students" button
    class CompSciMajorListener extends Manager implements ActionListener {
        public void actionPerformed(ActionEvent f) {

        }
    }

    public static void main(String[] args) {
        GUI frame = new GUI();
        frame.setTitle("Information Interface");
        frame.setSize(1200, 900);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

1 个答案:

答案 0 :(得分:0)

对我而言,您应该执行以下操作: 在一个方法中,你将需要遍历学生阵列(你称之为学生),让每个学生,检查分配给它的专业(与“计算机科学”相比)并打印那些做CS的学生。

这将是一致的:

private static void checkMajor(Student [] studentList) {
    for(Student s : studentList){
        if(s.major.equals("Computer Science"){
            System.out.println(s);
        }
    }
}

或者您可以将方法的返回类型从void更改为student,如果要在GUI中显示或使用它执行某些操作(我的代码将其打印到命令行),则将println语句更改为返回s。 / p>