Java JFrame程序中的按钮错误

Java JFrame程序中的按钮错误,java,swing,compilation,jframe,joptionpane,Java,Swing,Compilation,Jframe,Joptionpane,所以我有一个Java项目,基本上应该是一个临时数据库。它用一个GUI存储学生,该GUI会询问他们的姓名、3个考试分数,并根据这些信息将其存档,然后如果我需要再次访问这些学生,JFrame会有一些按钮,允许我循环浏览我已经添加的学生。出于某些原因,我不断收到以下编译错误: aveScoreButton.addActionListener(new AveScoreListener()); previousButton.addActionListener(new PreviousLi

所以我有一个Java项目,基本上应该是一个临时数据库。它用一个GUI存储学生,该GUI会询问他们的姓名、3个考试分数,并根据这些信息将其存档,然后如果我需要再次访问这些学生,JFrame会有一些按钮,允许我循环浏览我已经添加的学生。出于某些原因,我不断收到以下编译错误:

    aveScoreButton.addActionListener(new AveScoreListener());

    previousButton.addActionListener(new PreviousListener());

    nextButton.addActionListener(new NextListener());

    firstButton.addActionListener(new FirstListener());

    lastButton.addActionListener(new LastListener());
我在节目后面的部分给这些听众打电话: //对单击“平均分数”按钮作出响应

    private class AveScoreListener implements ActionListener{

        public void actionPerformed(ActionEvent e){

            if (model.size() == 0){

                JOptionPane.showMessageDialog(TestScoresView.this, "No Student Available");

                return;

            }

            int ave = model.getClassAverage();

            JOptionPane.showMessageDialog(TestScoresView.this, "The Average Score is " + ave);
        }

    }


    // Responds to a click on the < Button

    private class PreviousListener implements ActionListener{

        public void actionPerformed(ActionEvent e){

            model.previous();

            displayInfo();

        }
    }


    // Responds to a click on the > Button

    private class NextListener implements ActionListener{

        public void actionPerformed(ActionEvent e){

            model.next();

            displayInfo();

        }
    }


    // Responds to a click on the << Button

    private class FirstListener implements ActionListener{

        public void actionPerformed(ActionEvent e){

            model.first();

            displayInfo();

        }
    }


    // Responds to a click on the >> Button

    private class LastListener implements ActionListener{

        public void actionPerformed(ActionEvent e){

            model.last();

            displayInfo();

       }
   }  
编译器错误:

C:\Users-->\Documents\Java\Student\src\Student\TestScoresView.Java:183:错误:找不到符号

addActionListener(新的AveScoreListener()); 符号:类侦听器 位置:类TestScoresView

C:\Users-->\Documents\Java\Student\src\Student\TestScoresView.Java:185:错误:找不到符号

addActionListener(新的PreviousListener()); 符号:类PreviousListener 位置:类TestScoresView

C:\Users-->\Documents\Java\Student\src\Student\TestScoresView.Java:187:错误:找不到符号

addActionListener(新的NextListener()); 符号:class NextListener 位置:类TestScoresView

C:\Users\Eric\Documents\Java\Student\src\Student\TestScoresView.Java:189:错误:找不到符号

addActionListener(新的FirstListener()); 符号:class FirstListener 位置:类TestScoresView

C:\Users-->\Documents\Java\Student\src\Student\TestScoresView.Java:191:错误:找不到符号

addActionListener(新的LastListener()); 符号:类LastListener 位置:类TestScoresView

5个错误

C:\Users-->\Documents\Java\Student\nbproject\build impl.xml:915:执行此行时发生以下错误:
C:\Users----\Documents\Java\Student\nbproject\build impl.xml:307:编译失败;有关详细信息,请参见编译器错误输出

其原因是括号错误

  • 移除HighScoreListener类中if循环的额外括号-
    if(model.size()==0){
    {-(大约行号:355。额外括号用粗体标记)
  • 拆下文件中的最后一个支架-大约行编号:447

  • 它可能是类
    private
    ,并且超出了按钮的范围。可能是您试图从
    静态
    上下文创建按钮。您能否提供有关类的结构、它们的位置、相互之间的相对位置以及编译器错误消息的更多信息?如果您提供完整的Exampl,可能会有所帮助看起来您的模型类需要一看。我已将所有部分添加到代码中。@user2063151:欢迎。别忘了:如果这是正确的解决方案,您可以接受这个答案。
    Student:
    package student;
    
    // Case Study 9.1: Student classes
    
    public class Student {
    
    private String name;
    private int[] tests;
    
    // Default: Name is "" and 3 scores are 0
    
    public Student(){
        this("");
    }
    
    // Name is nm and 3 scores are 0
    public Student(String nm){
        this(nm, 3);
    }
    
    // Name is nm and n scores are 0
    public Student(String nm, int n){
        name = nm;
        tests = new int[n];
        for (int i = 0; i < tests.length; i++)
            tests[i] = 0;
    }
    // Name is nm and scores are in t
    public Student(String nm, int[] t){
        name = nm;
        tests = new int[t.length];
        for (int i = 0; i < tests.length; i++)
            tests[i] = t[i];
    }
    
    // Builds a copy of s
    public Student(Student s){
        this(s.name, s.tests);
    }
    
    public int getNumberOfTests(){
        return tests.length;
    }
    
    public void setName (String nm){
        name = nm;
    }
    
    public String getName (){
        return name;
    }
    
    public void setScore (int i, int score){
        tests[i - 1] = score;
    }
    
    public int getScore (int i){
        return tests[i - 1];
    }
    
    public int getAverage (){
        int sum = 0;
        for (int score : tests)
            sum += score;
        return sum / tests.length;
    }
    
    public int getHighScore(){
        int highScore = 0;
        for (int score : tests)
            highScore = Math.max (highScore, score);
        return highScore;
    }
    
    public String toString(){
        String str = "Name:     " + name + "\n";
        for (int i = 0; i < tests.length; i++)
        str += "test " + (i + 1) + ": " + tests[i] + "\n";
        str += "Average: " + getAverage();
        return str;
    }
    
    // Returns null if there are no errors else returns
    // an appropriate error mesage.
    public String validateData(){
        if (name.equals ("")) return "SORRY: name required";
        for (int score : tests){
            if (score < 0 || score > 100){
                String str = "SORRY: must have "+ 0
                        + " <= test score <= " + 100;
                return str;
            }
        }
        return null;
    }
    
    package student;
    
    // Case Study 9.1: TestScoresModel class
    
    public class TestScoresModel{
    
    private Student[] students;             // Array of Students
    private int indexSelectedStudent;       // Position of current student
    private int studentCount;               // Current number of students
    
    public TestScoresModel(){
    
        // Initializes the data
        indexSelectedStudent = -1;
        studentCount = 0;
        students = new Student[10];
    }
    
    // Mutator methods for adding and replacing students
    
    public String add(Student s){
        if (studentCount == students.length)
            return "SORRY: student list is full";
        else{
            students[studentCount] = s;
            indexSelectedStudent = studentCount;
            studentCount++;
            return null;
        }
    }
    public String replace(Student s){
        if (indexSelectedStudent == -1)
            return "Must add a student first";
        else{
            students[indexSelectedStudent] = s;
            return null;
        }
    }
    
    // Navigation Methods
    
    public Student first(){
        Student s = null;
        if (studentCount == 0)
            indexSelectedStudent = -1;
        else{
            indexSelectedStudent = -1;
            s = students[indexSelectedStudent];
        }
        return s;
    }
    
    public Student previous(){
        Student s = null;
        if (studentCount == 0)
            indexSelectedStudent = -1;
        else{
            indexSelectedStudent
                    = Math.max (0, indexSelectedStudent -1);
            s = students[indexSelectedStudent];
        }
        return s;
    }
    
    public Student next(){
        Student s = null;
        if (studentCount == 0)
            indexSelectedStudent = -1;
        else{
            indexSelectedStudent
                    = Math.min (studentCount - 1, indexSelectedStudent + 1);
            s = students[indexSelectedStudent];
        }
        return s;
    }
    
    public Student last(){
        Student s = null;
        if (studentCount == 0)
            indexSelectedStudent = -1;
        else{
            indexSelectedStudent = studentCount -1;
            s = students[indexSelectedStudent];
        }
        return s;
    }
    
    // Accessors to observe data
    
    public Student currentStudent(){
        if (indexSelectedStudent == -1)
            return null;
        else
            return students[indexSelectedStudent];
    }
    
    public int size(){
        return studentCount;
    }
    
    public int currentPosition(){
        return indexSelectedStudent;
    }
    
    public int getClassAverage(){
        if (studentCount == 0)
            return 0;
        int sum = 0;
        for (int i = 0; i < studentCount; i++)
            sum += students[i].getAverage();
        return sum / studentCount;
    }
    
    public Student getHighScore(){
        if (studentCount == 0)
            return null;
        else{
            Student s = students[0];
            for (int i = 1; i < studentCount; i++)
                if (s.getHighScore() < students[i].getHighScore())
                    s = students[i];
            return s;
    
        }
    }
    
    public String toString(){
        String result = "";
                for (int i = 0; i < studentCount; i ++)
                    result = result + students[i] + "\n";
        return result;
    }
    
    // Solution to Project 9.9
    
    package student;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class TestScoresView extends JFrame{
    
    
    // >>>>>>>>> The Model <<<<<<<<<
    
    
    // Declare the Model
    private TestScoresModel model;
    
    // >>>>>>>>> The View <<<<<<<<<
    
    // Declare and instantiate the window objects.
    
    private JButton     addButton       = new JButton("Add");
    
    private JButton     modifyButton    = new JButton("Modify");
    
    private JButton     firstButton     = new JButton("<<");
    
    private JButton     previousButton  = new JButton("<");
    
    private JButton     nextButton      = new JButton(">");
    
    private JButton     lastButton      = new JButton(">>");
    
    private JButton     highScoreButton = new JButton("Highest Score");
    
    private JButton     aveScoreButton  = new JButton("Class Average");
    
    private JLabel      nameLabel       = new JLabel("Name");
    
    private JLabel      test1Label      = new JLabel("Test 1");
    
    private JLabel      test2Label      = new JLabel("Test 2");
    
    private JLabel      test3Label      = new JLabel("Test 3");
    
    private JLabel      averageLabel    = new JLabel("Average");
    
    private JLabel      countLabel      = new JLabel("Count");
    
    private JLabel      indexLabel      = new JLabel("Index");
    
    private JTextField  nameField       = new JTextField("");
    
    private JTextField  test1Field      = new JTextField("0");
    
    private JTextField  test2Field      = new JTextField("0");
    
    private JTextField  test3Field      = new JTextField("0");
    
    private JTextField  averageField    = new JTextField("0");
    
    private JTextField  countField      = new JTextField("0");
    
    private JTextField  indexField      = new JTextField("-1");
    
    //Constructor
    
    public TestScoresView(TestScoresModel m){
    
        model = m;
    
        // Set attributes of fields
    
        averageField.setEditable(false);
    
        countField.setEditable(false);
    
        indexField.setEditable(false);
    
        averageField.setBackground(Color.white);
    
        countField.setBackground(Color.white);
    
        indexField.setBackground(Color.white);
    
        // Setup panels to organize widgets and
    
        // add them to the window
    
        JPanel northPanel = new JPanel();
    
        JPanel centerPanel = new JPanel(new GridLayout(5, 4, 10, 5));
    
        JPanel southPanel = new JPanel();
    
        Container container = getContentPane();
    
        container.add(northPanel, BorderLayout.NORTH);
    
        container.add(centerPanel, BorderLayout.CENTER);
    
        container.add(southPanel, BorderLayout.SOUTH);
    
        // Data Access Buttons
    
        northPanel.add(addButton);
    
        northPanel.add(modifyButton);
    
        northPanel.add(highScoreButton);
    
        northPanel.add(aveScoreButton);
    
        // Row 1
    
        centerPanel.add(nameLabel);
    
        centerPanel.add(nameField);
    
        centerPanel.add(countLabel);
    
        centerPanel.add(countField);
    
        // Row 2
    
        centerPanel.add(test1Label);
    
        centerPanel.add(test1Field);
    
        centerPanel.add(indexLabel);
    
        centerPanel.add(indexField);
    
        // Row 3
    
        centerPanel.add(test2Label);
    
        centerPanel.add(test2Field);
    
        centerPanel.add(new JLabel(""));
    
        centerPanel.add(new JLabel(""));
    
        // Row 4
    
        centerPanel.add(test3Label);
    
        centerPanel.add(test3Field);
    
        centerPanel.add(new JLabel(""));
    
        centerPanel.add(new JLabel(""));
    
        // Row 5
    
        centerPanel.add(averageLabel);
    
        centerPanel.add(averageField);
    
        centerPanel.add(new JLabel(""));
    
        centerPanel.add(new JLabel(""));
    
        // Navigation buttons
    
        southPanel.add(firstButton);
    
        southPanel.add(previousButton);
    
        southPanel.add(nextButton);
    
        southPanel.add(lastButton);
    
        // Attach listeners to buttons
    
        addButton.addActionListener(new AddListener());
    
        modifyButton.addActionListener(new ModifyListener());
    
        highScoreButton.addActionListener(new HighScoreListener());
    
        aveScoreButton.addActionListener(new AveScoreListener());
    
        previousButton.addActionListener(new PreviousListener());
    
        nextButton.addActionListener(new NextListener());
    
        firstButton.addActionListener(new FirstListener());
    
        lastButton.addActionListener(new LastListener());
    
        // Other attachments will go here (excercise)
    
        // Set window attributes
    
        setTitle("Student Test Scores");
    
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        pack();
    
        setVisible(true);
    
    }
    
    // Updates fields with info from the model
    
    private void displayInfo(){
    
        Student s = model.currentStudent();
    
        if (s == null){
    
            nameField.setText("");
    
            test1Field.setText("0");
    
            test2Field.setText("0");
    
            test3Field.setText("0");
    
            averageField.setText("0");
    
            countField.setText("0");
    
            test3Field.setText("-1");
        } else {
    
            nameField.setText(s.getName());
    
            test1Field.setText("" + s.getScore(1));
    
            test2Field.setText("" + s.getScore(2));
    
            test3Field.setText("" + s.getScore(3));
    
            averageField.setText("" + s.getAverage());
    
            countField.setText("" + model.size());
    
            indexField.setText("" + model.currentPosition());
    
        }
    
    }
    
    
    // Creates and returns new Student from field info
    
    private Student getInfoFromScreen(){
    
        Student s = new Student(nameField.getText());
    
        s.setScore(1, Integer.parseInt(test1Field.getText()));
    
        s.setScore(2, Integer.parseInt(test2Field.getText()));
    
        s.setScore(3, Integer.parseInt(test3Field.getText()));
    
        return s;
    }
    
    
    // >>>>>>>>> The Controller <<<<<<<<<<
    
    
    // Responds to a click on the Add button
    
    private class AddListener implements ActionListener{
    
        public void actionPerformed(ActionEvent e){
    
            // Get inputs, validate, and display error and quit if invalid
    
            Student s = getInfoFromScreen();
    
            String message = s.validateData();
    
            if (message !=null){
    
                JOptionPane.showMessageDialog(TestScoresView.this, message);
    
                return;
    
            }
    
            // Attempt to add student and display error or update fields
    
            message = model.add(s);
    
            if (message !=null) { 
    
                JOptionPane.showMessageDialog(TestScoresView.this, message);
            }
    
            else {
                displayInfo();
            }
    
        }
    }
    
    
    // Responds to a click on the Modify Button
    
    private class ModifyListener implements ActionListener{
    
        public void actionPerformed(ActionEvent e){
    
            if (model.size() == 0){
    
                JOptionPane.showMessageDialog(TestScoresView.this, "No Student Available");
    
                return;
    
            }
    
            // Get inputs, validate, and display error and quit if invalid
    
            Student s = getInfoFromScreen();
    
            String message = s.validateData();
    
            if (message !=null){
    
                JOptionPane.showMessageDialog(TestScoresView.this, message);
    
                return;
    
            }
    
            // Attempt to add student and display error or update fields
    
            message = model.replace(s);
    
            if (message !=null) {
    
                JOptionPane.showMessageDialog(TestScoresView.this, message);
            }
    
            else {
                displayInfo();
            }
    
        }
    }
    
    // Responds to a click on the Highest Score button
    
    private class HighScoreListener implements ActionListener{
    
        public void actionPerformed(ActionEvent e){
    
            if (model.size() == 0){{
    
                JOptionPane.showMessageDialog(TestScoresView.this, "No Student is Available");
    
                return;
    
            }
    
            Student s = model.getHighScore();
    
            JOptionPane.showMessageDialog(TestScoresView.this, s.toString());
    
         }
     }
    
     // Responds to a click on the Average Score Button
    
        private class AveScoreListener implements ActionListener{
    
            public void actionPerformed(ActionEvent e){
    
                if (model.size() == 0){
    
                    JOptionPane.showMessageDialog(TestScoresView.this, "No Student Available");
    
                    return;
    
                }
    
                int ave = model.getClassAverage();
    
                JOptionPane.showMessageDialog(TestScoresView.this, "The Average Score is " + ave);
            }
    
        }
    
    
        // Responds to a click on the < Button
    
        private class PreviousListener implements ActionListener{
    
            public void actionPerformed(ActionEvent e){
    
                model.previous();
    
                displayInfo();
    
            }
        }
    
    
        // Responds to a click on the > Button
    
        private class NextListener implements ActionListener{
    
            public void actionPerformed(ActionEvent e){
    
                model.next();
    
                displayInfo();
    
            }
        }
    
    
        // Responds to a click on the << Button
    
        private class FirstListener implements ActionListener{
    
            public void actionPerformed(ActionEvent e){
    
                model.first();
    
                displayInfo();
    
            }
        }
    
    
        // Responds to a click on the >> Button
    
        private class LastListener implements ActionListener{
    
            public void actionPerformed(ActionEvent e){
    
                model.last();
    
                displayInfo();
    
            }
        }
    
    package student;
    
    public class TestScoresApp {
    
    public static void main(String[] args){
        TestScoresModel model = new TestScoresModel();
        new TestScoresView(model);
    }
    
    }