0

我在一行代码中遇到 4 个错误

读取“public action void actionPerformed(ActionEvent event){”的行两次获得“Illegal start of action”和两次“; expected”。

我从 Head First Java 书中复制了这段代码,为什么它不能编译?

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

public class SimpleGui1 implements ActionListener{
    Jbutton button;

    public static void main (String [] args) {
        SimpleGui1 gui = new SimpleGui1();
        gui.go();
    }

    public void go(){
        JFrame frame = new JFrame();
        button = new JButton("Click");

        button.addActionListener(this);

        frame.getContentPane().add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);


        public void actionPerformed(ActionEvent event){
            button.setText("I've been clicked.");
        } //close actionPerformed
    } //close go()

}
4

1 回答 1

3

您不能在另一个方法中定义一个方法。移出方块actionPerformed_go

 public void go(){
        JFrame frame = new JFrame();
        button = new JButton("Click");

        button.addActionListener(this);

        frame.getContentPane().add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);


} //close go()



public void actionPerformed(ActionEvent event){
            button.setText("I've been clicked.");
} //close actionPerformed
于 2013-08-25T21:10:50.243 回答