我正在开始Java,我正在尝试创建十大名单。我一直在非法开始表达和';'预计第78,110和118行。
78:public insert(String name,Integer score) 110:public boolean isOnList(String first,String second) 118:public String toString()
如果我使这个类不是Action Event Listener,那么这部分代码会编译,但如果它是一个Event Listener,我会收到这些错误。任何和所有帮助使这些代码工作将不胜感激。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JList;
import java.util.*;
import java.util.Scanner;
import java.util.LinkedList;
public class TopTenList extends JFrame
{
private TopTenList tt;
private JTextArea listView;
private JTextField name;
private JTextField score;
private LinkedList<String> scores;
private JButton enterButton;
// This is the code for the GUI Window
public TopTenList()
{
listView = new JTextArea();
name = new JTextField();
score = new JTextField();
// Put the textArea in the center of the frame
add(listView);
listView.setEditable(false);
listView.setBackground(Color.WHITE);
//Create panel and label for the Name and score text fields
JPanel namePanel = new JPanel(new GridLayout(2,2));
namePanel.add(new JLabel ("Enter User Name: "));
namePanel.add(name);
namePanel.add(new JLabel ("Enter New Score: "));
namePanel.add(score);
add(namePanel, BorderLayout.NORTH);
//Create Enter score button
enterButton = new JButton ("Enter");
add(enterButton, BorderLayout.SOUTH);
//Add action listener to the button
enterButton.addActionListener(new enterButtonListener());
// Set up the frame
setTitle("Top Ten Scoreholders"); // Window Title
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Behavior on close
pack();
setVisible(true); // Display the window
}
// Create the Linked List
public void TopTenList()
{
scores = new LinkedList<String>();
}
// Populate the list
private class enterButtonListener implements ActionLister
{
public void actionPerformed(ActionEvent e)
{
public insert(String name, Integer score)
{
String newScore = name + " "+score.toString();
if(scores.isEmpty())
{
scores.add(newScore);
return;
}
for (int i=0; i<=scores.size(); i++)
{
if(i==scores.size())
{
scores.add(newScore);
break;
}
if (isOnList(newScore, scores.get(i)))
{
scores.add(i,newScore);
break;
}
}
// Shrink the list to the top ten scores
while (scores.size()>10)
{
scores.remove(10);
}
}
// method to evaluate placement on score list
public boolean isOnList (String first, String second)
{
Integer firstScore = Integer.parseInt(first.substring(first.lastIndexOf(' ')+1));
Integer secondScore = Integer.parseInt(second.substring(second.lastIndexOf(' ')+1));
return firstScore > secondScore;
}
// make the list for display
public String toString()
{
String scoreList = "";
for (int i = 0; i <scores.size(); i++)
{
scoreList = scoreList + scores.get(i)+"\n";
}
return scoreList;
}
}
}
}
答案 0 :(得分:2)
public void actionPerformed(ActionEvent e)
{
public insert(String name, Integer score)
{...}
public boolean isOnList (String first, String second)
{...}
public String toString()
{...}
}
这种语法在java中没有任何意义。你不能用这种方式在另一个函数中定义一个函数。您想要将代码重新排列为:
public void actionPerformed(ActionEvent e)
{ // handle the ActionEvent here }
public insert(String name, Integer score)
{...}
public boolean isOnList (String first, String second)
{...}
public String toString()
{...}