2013-10-29 41 views
0

我用Netbeans在java中製作應用程序。在Jframe表格中,我使用四個按鈕。我需要知道哪些用戶點擊過。每個人都可以幫助我? 感謝單擊了哪個按鈕。 Java,Netbeans

public class Color extends javax.swing.JFrame implements ActionListener { 


public Color() { 
     initComponents(); 


     ///////////////////////////////// 

     //Register a listener for the buttons. 
     up_button.addActionListener(this); 
     down_button.addActionListener(this); 
     left_button.addActionListener(this); 
     right_button.addActionListener(this); 
     } 


private int k=1; 
    public void actionPerformed(ActionEvent e) { 

     k=k+1; 


     if (k==1) 
     { 
     image.setIcon(createImageIcon("color1" 
             + e.getActionCommand() 
             + ".PNG")); 
     } 
     else ... } 

     private void up_buttonActionPerformed(java.awt.event.ActionEvent evt)  {           
     // TODO add your handling code here: 

    } 

    private void down_buttonActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 

    } 

    public static void main(String args[]) { 

     /* Create and display the form */ 

     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new Color().setVisible(true); 
      } 
     }); 
    } 

回答

2

我需要知道哪些人點擊了用戶

只需實現actionPerformed方法,並調用getSource()ActionEvent可變知道已被點擊了哪個按鈕:

public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == up_button){ 
     //up_button clicked 
     }  
} 

您還可以直接將聽衆添加到您的按鈕中:

up_button.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
     //Button is pressed 
    } 
}); 
+0

非常感謝你。第一選擇幫助我! :) – user2933161

+0

@ user2933161不客氣=) –

+0

@ user2933161,那麼你應該接受答案,讓人們知道問題已經解決。 – camickr