我在一行代码中遇到了 4 个错误
“public action void actionPerformed(ActionEvent event){”行获得“Illegal start of action”两次和“;预期”两次。
我从 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()
}
最佳答案
您不能在另一个方法中定义一个方法。将 actionPerformed
移到 go
block
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
关于java - "Illegal Start of action"和 "; expected"编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18433726/