gpt4 book ai didi

java - 学生/研究生项目中的经理类(class)问题

转载 作者:行者123 更新时间:2023-12-02 06:04:30 26 4
gpt4 key购买 nike

该项目的目标是创建四个类:Student 类、GradStudent 类、Manager 类和 GUI 类。 GUI 中有两个单选按钮:一个用于学生,一个用于研究生。根据选择的对象,Manager 类应该负责使用两个数组创建和存储 Student 或 GradStudent 对象。我已对所有类(class)进行了编程,但是当我选择任一单选按钮时,我在 Manager.getLastGradStudent 中收到错误,这反过来又在我的 JRBListener 类中出现错误。我将在下面发布 GUI 和 Manager 类。任何帮助将不胜感激!

经理级别:

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

private GradStudent[] gradStudents = new GradStudent[50];
private 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";
}
}

GUI 类:

import java.awt.BorderLayout;
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 Manager m1 = new Manager();

public GUI() {

// Creates panel P1 and adds the components
JPanel p1 = new JPanel(new GridLayout(7, 1));
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(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);
}

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

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

}

}

我收到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at Manager.getLastGradStudent(Manager.java:28)
at GUI$JRBListener.actionPerformed(GUI.java:93)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

代码中的问题是您没有在 JRBListener.actionPerformed() 方法中设置任何定义 if-scope 的括号。如果未设置括号,则仅将 if 条件后的下一行视为在范围内。因此,在您的情况下,无论是否添加学生,程序都会尝试声明“学生已添加!”消息 :-) 仅当您实际上添加了学生时,这才有意义:

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

关于java - 学生/研究生项目中的经理类(class)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22412350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com