在public void actionPerformed(ActionEvent ae)非法开始表达

时间:2014-06-22 06:20:34

标签: java compiler-errors

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

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

如何摆脱这些错误?

1 个答案:

答案 0 :(得分:1)

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

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