0

Java を始めたばかりで、トップ 10 リストを作成しようとしています。不正な式の開始と ';' を取得し続けます。行 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 に答える