gpt4 book ai didi

Java Action 事件监听器中表达式的非法开始

转载 作者:行者123 更新时间:2023-12-02 05:53:15 24 4
gpt4 key购买 nike

我正在开始 Java,我正在尝试创建一个前十名列表。我不断收到非法的表达开始和“;”预计第 78、110 和 118 行。

78:公共(public)插入(字符串名称,整数分数)110: public boolean isOnList(第一个字符串,第二个字符串)118: 公共(public)字符串 toString()

如果我将其设置为不是 Action 事件监听器的类,则这部分代码将编译,但如果它是事件监听器,则会出现这些错误。任何使此代码正常工作的帮助将不胜感激。

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

}

最佳答案

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()
{...}

关于Java Action 事件监听器中表达式的非法开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23352900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com