Java 将数组打印到JTextArea时出现问题

Java 将数组打印到JTextArea时出现问题,java,arrays,swing,actionlistener,tostring,Java,Arrays,Swing,Actionlistener,Tostring,这个项目的目标是创建四个类:一个学生类、一个研究生类、一个经理类和一个GUI类。在GUI中有两个单选按钮:一个用于学生,一个用于研究生。根据选择哪一个,Manager类应该负责使用两个数组创建和存储Student或Gradstuent对象。我还向GUI添加了一个按钮,该按钮用于将学生数组打印到JTextArea,但这正是我遇到问题的地方。我得到一个错误“无法对基元类型int调用toString()。我理解为什么会发生这种情况,但我不知道如何解决它。任何帮助都将不胜感激。谢谢 GUI类 impor

这个项目的目标是创建四个类:一个学生类、一个研究生类、一个经理类和一个GUI类。在GUI中有两个单选按钮:一个用于学生,一个用于研究生。根据选择哪一个,Manager类应该负责使用两个数组创建和存储Student或Gradstuent对象。我还向GUI添加了一个按钮,该按钮用于将学生数组打印到JTextArea,但这正是我遇到问题的地方。我得到一个错误“无法对基元类型int调用toString()。我理解为什么会发生这种情况,但我不知道如何解决它。任何帮助都将不胜感激。谢谢

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

    // 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 StudentListener extends Manager implements ActionListener {
        public void actionPerformed(ActionEvent c) {

            echoStudent.setText(counter1.toString());

        }

    }

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

    }

}
public class Manager {
    public Student[] students = new Student[50];
    public 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";
    }


}
无法对基元类型int调用toString()

使用

无法对基元类型int调用toString()

使用

无法对基元类型int调用toString()

使用

无法对基元类型int调用toString()

使用


您还可以尝试将
int
转换为
String

 String.valueOf(counter1);
或者您也可以尝试
Integer

 new Integer(counter1).toString();
或使用

 Integer.valueOf(counter1).toString();

--编辑--

根据您最后的评论,这里是打印
JTextArea

class StudentListener extends Manager implements ActionListener {
    public void actionPerformed(ActionEvent c) {

        StringBuilder builder=new StringBuilder();
        for(int i=0;i<counter1;i++){
            builder.append(m1.students[i].name);
        }
        echoStudent.setText(builder.toString());
    }
} 
class StudentListener扩展管理器实现ActionListener{
已执行的公共无效操作(操作事件c){
StringBuilder=新的StringBuilder();

对于(int i=0;i您也可以尝试将
int
转换为
String

 String.valueOf(counter1);
或者您也可以尝试
Integer

 new Integer(counter1).toString();
或使用

 Integer.valueOf(counter1).toString();

--编辑--

根据您最后的评论,这里是打印
JTextArea

class StudentListener extends Manager implements ActionListener {
    public void actionPerformed(ActionEvent c) {

        StringBuilder builder=new StringBuilder();
        for(int i=0;i<counter1;i++){
            builder.append(m1.students[i].name);
        }
        echoStudent.setText(builder.toString());
    }
} 
class StudentListener扩展管理器实现ActionListener{
已执行的公共无效操作(操作事件c){
StringBuilder=新的StringBuilder();

对于(int i=0;i您也可以尝试将
int
转换为
String

 String.valueOf(counter1);
或者您也可以尝试
Integer

 new Integer(counter1).toString();
或使用

 Integer.valueOf(counter1).toString();

--编辑--

根据您最后的评论,这里是打印
JTextArea

class StudentListener extends Manager implements ActionListener {
    public void actionPerformed(ActionEvent c) {

        StringBuilder builder=new StringBuilder();
        for(int i=0;i<counter1;i++){
            builder.append(m1.students[i].name);
        }
        echoStudent.setText(builder.toString());
    }
} 
class StudentListener扩展管理器实现ActionListener{
已执行的公共无效操作(操作事件c){
StringBuilder=新的StringBuilder();

对于(int i=0;i您也可以尝试将
int
转换为
String

 String.valueOf(counter1);
或者您也可以尝试
Integer

 new Integer(counter1).toString();
或使用

 Integer.valueOf(counter1).toString();

--编辑--

根据您最后的评论,这里是打印
JTextArea

class StudentListener extends Manager implements ActionListener {
    public void actionPerformed(ActionEvent c) {

        StringBuilder builder=new StringBuilder();
        for(int i=0;i<counter1;i++){
            builder.append(m1.students[i].name);
        }
        echoStudent.setText(builder.toString());
    }
} 
class StudentListener扩展管理器实现ActionListener{
已执行的公共无效操作(操作事件c){
StringBuilder=新的StringBuilder();


对于(int i=0;我是否在StudentListener类中添加此项?您可以在第
echoStudent.setText(counter1.toString())行中更改它;
我是否在StudentListener类中添加此项?您可以在第
echoStudent.setText(counter1.toString())行中更改它
我在StudentListener类中添加这个吗?您可以在第
行echoStudent.setText(counter1.toString())中更改它;
我在StudentListener类中添加这个吗?您可以在第
行echoStudent.setText(counter1.toString())中更改它;
它们基本上是一样的。
字符串.valueOf(int)
简单地包装
Integer.toString(int)
。将这一行
echoStudent.setText(counter1.toString());
替换为
echoStudent.setText([上面提到的任何一个]);
@JonK是的,你是绝对正确的。有不同的方法。I thing
Integer.valueOf(counter1)
一个比另一个好,我想你知道为什么吗?你知道为什么当点击按钮时它会在JTextArea中打印“0”吗?显然
计数器1
的值是零。你在哪里再次设置它的值?它们几乎是一个,无论如何都是一样的。
字符串。value of(int)
简单地包装
Integer.toString(int)
。将这一行
echoStudent.setText(counter1.toString());
替换为
echoStudent.setText([上面提到的任何一个]);
@JonK是的,你是绝对正确的。有不同的方法可以做到这一点。I thing
Integer.valueOf(counter1)
一个好,另一个好,我想你知道为什么吗?知道为什么它会打印“0”单击按钮时返回JTextArea?显然,
counter1
的值为零。您在哪里再次设置它的值?它们几乎是一个,而且是一样的。
String.valueOf(int)
只需包装
Integer.toString(int)
。替换这一行
echoStudent.setText(counter1.toString())
使用
echoStudent.setText([上面提到的任何一个]);
@JonK是的,你是绝对正确的。这有不同的方法。我使用
Integer.valueOf(counter1)
一个比另一个好,我想你知道为什么吗?知道为什么它打印“0”吗单击按钮时返回JTextArea?显然,
counter1
的值为零。您在哪里再次设置它的值?它们几乎是一个,而且是一样的。
String.valueOf(int)
只需包装
Integer.toString(int)
。替换这一行
echoStudent.setText(counter1.toString())
使用
echoStudent.setText([上面提到的任何一个]);
@JonK是的,你是绝对正确的。这有不同的方法。我使用
Integer.valueOf(counter1)
一个比另一个好,我想你知道为什么吗?知道为什么它打印“0”吗单击按钮时返回JTextArea?显然,
counter1
的值为零。您在哪里再次设置其值?