2013-08-25 24 views
0

我得到的代碼和「行動非法啓動」「預期」編譯器錯誤

行讀取一行4個錯誤「公益行動無效的actionPerformed(ActionEvent的事件){」被「非法的開始行動「兩次,」預計「兩次。

我將這段代碼從Head First Java書中拷出來,爲什麼heck不會編譯它?

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() 

} 
+2

在Java中(從1.7開始)嵌套方法無效。 –

+0

移動行動在外走路 – dbarnes

回答

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 
+0

感謝您的建議傢伙。我現在明白了。爲什麼我的問題被降低了兩次?我做錯什麼了嗎? –