2013-11-04 84 views
0

我測試直接實現ActionListener搖擺actionPerformed方法

下面的應用程序可以編譯和運行應用程序:

public class App implements ActionListener { 

JButton button; 
int count = 0; 

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

public void go() 
{ 
    button = new JButton("Click me!"); 
    JFrame frame = new JFrame(); 
    frame.getContentPane().add(button); 
    frame.setSize(500,500); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    button.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent event) 
     { 
      count++; 
      button.setText("I've been clicked "+count+" times"); 
     } 
    }); 

} 

} 

但是Eclipse想在App類的

public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 

} 

方法以及。這是因爲「去」方法有時可能不會被調用使actionPerformed不被調用,然後反對如何實現工作? 在此先感謝您的幫助。

+1

如果你不打算執行'actionPerformed'方法,那麼你不需要'農具ActionListener' – MadProgrammer

+0

刪除'從你的應用程序類實現ActionListener'宣言。 – Sambuca

回答