0

我正在尝试将 JComboBox 添加到我的学生计划中,其中包含以下选项:大一、大二、大三或大四。When the Senior option is chosen, it should output something along the lines of, "Student has been in school for four years" or Junior, "Student has been in school for three years". 等等......我搜索了论坛并进行了一些谷歌搜索,但我无法与我在这里尝试做的类似的例子。我将在下面发布代码。任何帮助将不胜感激。谢谢!

学生班级:

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

    // Constructs fields
    public Student(String name, String address, String major, double balance) {
        this.name = name;
        this.address = address;
        this.major = major;
        this.balance = balance;
    }

    public String setName() {
        return name;
    }

    public String setAddress() {
        return address;
    }

    public double setBalance() {
        return balance;
    }

    public String setMajor() {
        return major;

    }

    public String setStudentInformation() {
        return "\n\tName: " + name + "\n\tAddress: " + address + "\n\tMajor: " + major + "\n\tBalance: " + balance;
    }

    public String toString() {
        return setStudentInformation();
    }
}

经理类:

public class Manager {
    private static Manager thisManager;

    public Student[] students = new Student[50];
    public int studentCounter = 0;

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

    // Private Constructor (Singleton instance method)
    public Manager() {

    }

    // Instance method (Singleton)
    public static Manager instance() {
        if (thisManager == null) {
            return thisManager = new Manager();
        } else {
            return thisManager;
        }
    }

    public Student createStudent(String name, String address, String major,
            double amount) {
        Student c1 = new Student(name, address, major, amount);
        storeStudent(c1);
        return c1;
    }

    private void storeStudent(Student c1) {
        students[studentCounter++] = c1;
    }

    public GradStudent createGradStudent(String name, String address,
            String major, double amount) {
        GradStudent e1 = new GradStudent(name, address, major, amount);
        storeGradStudent(e1);
        return e1;
    }

    private void storeGradStudent(GradStudent g1) {
        gradStudents[gradCounter++] = g1;
    }

    public String listStudents() {
        String temp = "\n\nStudent List\n";

        for (int i = 0; i < studentCounter; i++) {
            temp += "Student: " + students[i].setName() + "\n";
        }

        return temp;
    }

    public String listGrads() {
        String temp = "\n\nGraduate Student List\n";

        for (int i = 0; i < gradCounter; i++) {
            temp += "Graduate Student: " + gradStudents[i].setName() + "\n";
        }

        return temp;
    }

    public String aveBalance() {
        String temp = "\n\nStudent Average Balance: ";
        double sum = 0;

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

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

        return temp + sum / (studentCounter + gradCounter) + "\n";
    }

    public String listCsci() {
        String temp = "\n\nComputer Science Student List\n";

        for (int i = 0; i < studentCounter; i++) {
            if (students[i].setMajor().equals("Computer Science")) {
                temp += "Student: " + students[i].setName() + "\n";
            }
        }

        for (int i = 0; i < gradCounter; i++) {
            if (gradStudents[i].setMajor().equals("Computer Science")) {
                temp += "Graduate Student: " + gradStudents[i].setName() + "\n";
            }
        }

        return temp;
    }
}

图形界面类:

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.util.Enumeration;

public class GUI extends JFrame implements ActionListener {
    // RadioButtons & ButtonGroup
    private JRadioButton jrbStudent = new JRadioButton("Student");
    private JRadioButton jrbGraduate = new JRadioButton("Graduate Student");
    private ButtonGroup group = new ButtonGroup();

    // JTextFields
    private JTextField name = new JTextField(20);
    private JTextField address = new JTextField(20);
    private JTextField balance = new JTextField(20);
    private JTextField major = new JTextField(20);

    // Submit Button
    private JButton jbtSubmit = new JButton("Submit");

    // echoStudent output area
    private JTextArea echoStudent = new JTextArea(5, 20);

    // Specific Buttons
    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");

    // ScrollPane
    private JScrollPane scrollPane = new JScrollPane(echoStudent);

    // Fill and create JComboBox
    private String[] studentYear = { "Freshman", "Sophomore", "Junior", "Senior" };
    private JComboBox<String> jcbo = new JComboBox<String>(studentYear);



    public GUI() {
        super("College Student Information Interface");

        // Creates Panels
        JPanel bottomPanel = new JPanel();
        JPanel topLeftPanel = new JPanel();
        JPanel topRightPanel = new JPanel();

        // Adds Labels, Fields, and Buttons to the topRightPanel
        topRightPanel.setLayout(new GridLayout(7, 2, 5, 5));
        topRightPanel.add(new JLabel("Name:"));
        topRightPanel.add(name);
        topRightPanel.add(new JLabel("Address:"));
        topRightPanel.add(address);
        topRightPanel.add(new JLabel("Major:"));
        topRightPanel.add(major);
        topRightPanel.add(new JLabel("Balance:"));
        topRightPanel.add(balance);
        topRightPanel.add(new JLabel("Submit:"));
        topRightPanel.add(jbtSubmit);
        topRightPanel.add(printStudentNames);
        topRightPanel.add(printGradStudentNames);
        topRightPanel.add(calcBalance);
        topRightPanel.add(compSciMajor);

        // Handles the radioButton group and adds ActionListeners
        jrbStudent.setSelected(true);
        group.add(jrbStudent);
        group.add(jrbGraduate);
        jrbStudent.addActionListener(this);
        jrbGraduate.addActionListener(this);

        // topLeftPanel includes student type with student/gradStudent radio
        // buttons & freshman/sophomore/junior/senior in a combo box
        topLeftPanel.setBorder(BorderFactory
                .createTitledBorder("Which Type of Student Are You?"));
        topLeftPanel.add(jrbStudent);
        topLeftPanel.add(jrbGraduate);
        topLeftPanel.add(jcbo);

        // Adds topLeftPanel and topRightPanel to bottomPanel
        bottomPanel.add(topLeftPanel);
        bottomPanel.add(topRightPanel);

        // Places bottomPanel and scrollPane
        getContentPane().add(BorderLayout.NORTH, bottomPanel);
        getContentPane().add(BorderLayout.CENTER, scrollPane);

        // Adds ActionListeners to the buttons
        jbtSubmit.addActionListener(this);
        printStudentNames.addActionListener(this);
        printGradStudentNames.addActionListener(this);
        calcBalance.addActionListener(this);
        compSciMajor.addActionListener(this);
        echoStudent.setEditable(false);

        // Interface Settings
        setSize(1200, 700);
        setVisible(true);

    }

    // ActionEvent methods to handle interface events
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == jbtSubmit) {
            if (jrbStudent.isSelected()) {
                Student s1 = Manager.instance().createStudent(name.getText(),
                        address.getText(), major.getText(),
                        Double.parseDouble(balance.getText()));
                echoStudent.append("\n\nCreated Student: " + s1.toString());
            }

            else if (jrbGraduate.isSelected()) {
                GradStudent g1 = Manager.instance().createGradStudent(
                        name.getText(), address.getText(), major.getText(),
                        Double.parseDouble(balance.getText()));
                echoStudent.append("\n\nCreated Graduate Student: "
                        + g1.toString());

            }
        } else if (event.getSource() == printStudentNames) {
            echoStudent.append(Manager.instance().listStudents());
        } else if (event.getSource() == printGradStudentNames) {
            echoStudent.append(Manager.instance().listGrads());
        } else if (event.getSource() == calcBalance) {
            echoStudent.append(Manager.instance().aveBalance());
        } else if (event.getSource() == compSciMajor) {
            echoStudent.append(Manager.instance().listCsci());
        } else if (event.getSource() == jcbo) {

        }
    }

    public static void main(String[] args) {
        new GUI();

    }

}
4

1 回答 1

1

你可以像这样实现它。首先在 GUI() 构造函数中将 actionListener 添加到 JComboBox jcbo。

jcbo.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
String selected=""+jcbo.getSelectedItem();
if(selected.equals("Senior"))
   System.out.println("Student has been in school for four years")
else if(selected.equals("Junior"))
   System.out.println("Student has been in school for three years");
    }
});

像这样你可以为所有选项做。代码本身是不言自明的。即使是这样。我给你一个简短的解释。当 JComboBox 项目被选中时,上面的代码将被执行。如果选中的是

  1. 高级:- Prints 学生已经在学校学习了四年。
  2. Junior :- Prints 学生已经上学三年了。

我想它会对你有所帮助。

于 2014-04-16T07:25:30.200 回答