【问题标题】:Java illegal start of expression within the action event listener动作事件侦听器中的 Java 非法表达式开始
【发布时间】:2014-04-28 23:26:17
【问题描述】:

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

78:公共插入(字符串名称,整数分数) 110:公共布尔isOnList(字符串第一,字符串第二) 118:公共字符串 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;
        }
   }
   }

    }

【问题讨论】:

  • 您正在定义方法和方法体中的构造函数之类的东西。这是不允许的。请重新阅读教程。
  • 代码太多了...你能把它缩小到只显示错误的几行吗?

标签: java actionlistener actionevent


【解决方案1】:
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()
{...}

【讨论】:

  • 谢谢 - 这是我错过的部分,我一次尝试做太多事情。由于我真正想要执行的操作是插入,我需要弄清楚如何重写它以在 LinkedList 的侦听器中工作
  • 帮助的 TY - 现在将编译代码。但是,当它运行时会出现 AWT-EVENTQueue-Q java.lang.NullPointerException 并且该行仍然指的是插入方法。关于如何解决这个问题的任何想法?
  • 这里没有足够的信息继续下去。我的第一个想法是 insert(name,score) 可能会以 null 分数调用,并且您会在 insert 函数的第一行中的语句 score.toString() 上得到 NullPointerException,但没有看到调用代码我可以不能确定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2016-09-28
  • 2021-12-01
  • 2018-01-23
  • 1970-01-01
  • 2021-06-05
相关资源
最近更新 更多