我在一行代码上遇到4个错误
读取“public action void actionPerformed(ActionEvent event){”得到“非法行动开始”两次和“;预期”两次的行。
我从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()
}
答案 0 :(得分: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