0

我正在开始使用 Java,并且正在尝试创建前十名列表。我不断收到非法的表达开始和';' 预计在 78、110 和 118 行。

78: public insert(String name, Integer score) 110: public boolean isOnList (String first, String second) 118: public String toString()

如果我将此类设为不是动作事件侦听器的类,则这部分代码将编译,但如果它是事件侦听器,则会出现这些错误。非常感谢您为使此代码正常工作提供的任何和所有帮助。

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

    }
4

1 回答 1

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()
{...}
于 2014-04-28T23:32:52.213 回答