所以我有这个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();
}
}
然而,ActionListeners在底部找不到这些,导致找不到符号错误。任何人都可以帮我这个吗?
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 location:类TestScoresView
C:\ Users ---- \ Documents \ Java \ Student \ src \ student \ TestScoresView.java:185:错误:找不到符号
previousButton.addActionListener(new PreviousListener()); symbol:类PreviousListener location:类TestScoresView
C:\ Users ---- \ Documents \ Java \ Student \ src \ student \ TestScoresView.java:187:错误:找不到符号
nextButton.addActionListener(new NextListener()); 符号:类NextListener location:类TestScoresView
C:\ Users \ Eric \ Documents \ Java \ Student \ src \ student \ TestScoresView.java:189:错误:找不到符号
firstButton.addActionListener(new FirstListener()); 符号:类FirstListener location:类TestScoresView
C:\ Users ---- \ Documents \ Java \ Student \ src \ student \ TestScoresView.java:191:错误:找不到符号
lastButton.addActionListener(new LastListener()); 符号:类LastListener location:类TestScoresView
5个错误
C:\ Users ---- \ Documents \ Java \ Student \ nbproject \ build-impl.xml:915:执行此行时发生以下错误: C:\ Users ---- \ Documents \ Java \ Student \ nbproject \ build-impl.xml:307:编译失败;有关详细信息,请参阅编译器错误输出
答案 0 :(得分:2)
因为括号错误
if (model.size() == 0){
{ - (约行号:355。以粗体标记的额外括号)< / LI>