gpt4 book ai didi

java - public void actionPerformed(ActionEvent ae) 中表达式的非法开始

转载 作者:行者123 更新时间:2023-12-02 11:09:25 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How do I fix an "Illegal start of expression" error in Java?

(2 个回答)


7年前关闭。




我在编译期间遇到了一些错误。
在程序结束时,我提到了编译期间遇到的所有错误。
检查此程序并提供解决方案以消除错误。

import javax.swing.*;    
import java.awt.*;
import java.awt.event.*;

public class PuzzleGame extends JFrame implements ActionListener {

JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;

public PuzzleGame(){
super("Puzzle Game");
setSize(300,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

b1 = new JButton(" 1 ");
b2 = new JButton(" 2 ");
b3 = new JButton(" 3 ");
b4 = new JButton(" 4 ");
b5 = new JButton(" 5 ");
b6 = new JButton(" 6 ");
b7 = new JButton(" 7 ");
b8 = new JButton(" 8 ");
b9 = new JButton(" ");

setLayout(new GridLayout(3,3));
add(b5);add(b6);add(b7);
add(b2);add(b3);add(b1);
add(b4);add(b8);add(b9);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
b1.setVisible(false);
b9.setLabel(" 1 ");
}
if(ae.getSource()==b8)
{
b8.setVisible(false);
b9.setLabel(" 8 ");
}
}
}

public static void main(String[] str)
{
new PuzzleGame();
}
}

在编译时,我有这些错误:
E:\>javac PuzzleGame.java
PuzzleGame.java:41: error: illegal start of expression
public void actionPerformed(ActionEvent ae)
^
PuzzleGame.java:41: error: illegal start of expression
public void actionPerformed(ActionEvent ae)
^
PuzzleGame.java:41: error: ';' expected
public void actionPerformed(ActionEvent ae)
^
PuzzleGame.java:41: error: ';' expected
public void actionPerformed(ActionEvent ae)
^
4 errors

如何摆脱这些错误?

最佳答案

您在构造函数中添加无效的方法

public PuzzleGame(){ 
// some code
public void actionPerformed(ActionEvent ae)
{

关于java - public void actionPerformed(ActionEvent ae) 中表达式的非法开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24348669/

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