0

所以我有这个 Java 项目,它基本上应该是一个临时数据库。它使用 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();

       }
   }  

然而,ActionListener 在底部找不到这些,并导致找不到符号错误。谁能帮我这个?

PS。抱歉,如果我弄错了这样的术语,我对 Java 比较陌生,不知道所有东西都叫什么。请在您认为合适的任何地方纠正我!

//------------- 整个代码

这是一个 4 部分计划

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

}

编译器错误:

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

aveScoreButton.addActionListener(new AveScoreListener()); 符号:AveScoreListener 类 位置:TestScoresView 类

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

previousButton.addActionListener(new PreviousListener()); 符号:类 PreviousListener 位置:类 TestScoresView

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

nextButton.addActionListener(new NextListener()); 符号:NextListener 类 位置:TestScoresView 类

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

firstButton.addActionListener(new FirstListener()); 符号:FirstListener 类 位置:TestScoresView 类

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

lastButton.addActionListener(new LastListener()); 符号:LastListener 类 位置:TestScoresView 类

5 个错误

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

4

1 回答 1

2

这是因为括号错误

  1. 删除类 HighScoreListener - if (model.size() == 0){{ - 中 if 循环的额外括号(大约行号:355。额外括号以粗体标记)
  2. 删除文件中的最后一个括号 - 大约行号:447
于 2013-02-12T06:08:29.137 回答