2013-01-07 115 views
2

我有一個帶有Actionlistener的JTextField。現在,當我按下回車鍵時,我想讓它做某些事情。我正在使用共享的ActionListener,所以試圖在JTextField上執行getSource,但它不起作用!希望任何人都能幫忙。Enter key JTextField

JTextField txtProductAantal = new JTextField(String.valueOf(WinkelApplication.getBasket().getProductAmount(productdelete))); 
      txtProductAantal.setBounds(340, verticalPosition + i * productOffset, 40, 20); 
      txtProductAantal.addActionListener(this); 
      add(txtProductAantal); 

public void actionPerformed(ActionEvent event) { 

     if (event.getSource() == btnEmptyBasket) { 
      WinkelApplication.getBasket().empty(); 
      WinkelApplication.getInstance().showPanel(new view.CategoryList()); 
     } 

     if(event.getSource() == txtProductAantal){ 
      String productgetal = txtProductAantal.getText(); 
      txtProductAantal.setText(productgetal); 
      WinkelApplication.getInstance().showPanel(new view.Payment()); 
     } 
    } 
+2

1)爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 2)'txtProductAantal.setBounds(..)'使用佈局來避免接下來的17個問題。 –

回答

3
  • 必須創造一個比較

例如

public void actionPerformed(ActionEvent event) { 
     Object source = event.getSource(); 
     if (source == btnEmptyBasket) { 
      //........... 
     } else if (source == txtProductAantal) { 
      //........... 
     } else { 

     } 
    } 
  • 一個臨時Object或只有JTextFields(避免instanceofif - else語句),你可以鑄造對象到JTextField directl ÿ

JTextField source = (JTextField) event.getSource();

EDIT,

  • one from the next adviced 17 problems.

  • please to read suggestion by @Andrew Thompson, again

    • 1) For better help sooner, post an SSCCE.

    • 2) txtProductAantal.setBounds(..)

    • Use layouts to avoid the next 17 problems.

我的代碼工作,因爲我預料

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 

public class JTextFieldAndActionListener implements ActionListener { 

    private JFrame frm = new JFrame("JTextFieldAndActionListener"); 
    private JTextField one = new JTextField(10); 
    private JTextField two = new JTextField(); 
    private JTextField three = new JTextField(); 

    public JTextFieldAndActionListener() { 
     one.addActionListener(this); 
     two.addActionListener(this); 
     three.addActionListener(this); 
     frm.setLayout(new GridLayout()); 
     frm.add(one); 
     frm.add(two); 
     frm.add(three); 
     frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frm.setLocation(400, 300); 
     frm.pack(); 
     frm.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent event) { 
     Object source = event.getSource(); 
     if (source == one) { 
      System.out.println("firing from JTextField one"); 
     } else if (source == two) { 
      System.out.println("firing from JTextField two"); 
     } else if (source == three) { 
      System.out.println("firing from JTextField three"); 
     } else { 
      System.out.println("something went wrong"); 
     } 
    } 

    public static void main(String[] args) { 

     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JTextFieldAndActionListener ie = new JTextFieldAndActionListener(); 
      } 
     }); 
    } 
} 

print_out我的ENTER鍵

run: 
firing from JTextField one 
firing from JTextField two 
firing from JTextField three 
BUILD SUCCESSFUL (total time: 15 seconds) 
+0

仍然不起作用。我已經把一個系統放在文本框的if上,但是它只是對輸入按鈕沒有反應。 – DaViDa

+0

@DaViDa:它確實工作得很好。您可以通過查看[** link **](http://gagandeepbali.uk.to/gaganisonline/swing/downloads/Example.java)來自己測試[SSCCE](http://sscce.org/) 。 –

+1

我覺得別的是錯的,這就是爲什麼它不起作用,但感謝所有爲我展示這樣做的好方法! – DaViDa

相關問題