2012-05-31 2 views
0

JTextField에 actionListener를 추가하는 데 문제가 있습니다. 사용자가 입력 한 텍스트를 문자열로 가져와서 작업 할 수 있어야합니다.GridBagLayout의 JTextField ActionListener

누구나 내가 잘못하고있는 일이나 내가 어떻게해야 하는지를 알 수 있습니까?

public void actionPerformed(ActionEvent e) 
     { 
      //Execute when button is pressed 
      JFrame frame = new JFrame("Value Bet"); 
      frame.setVisible(true); 
      frame.setSize(500,300); 
      GridBagLayout layout = new GridBagLayout(); 
      frame.setLayout(layout); 
      GridBagConstraints c = new GridBagConstraints(); 

      JLabel label; 
      JTextField tf; 

      if (shouldFill) { 
      //natural height, maximum width 
      c.fill = GridBagConstraints.HORIZONTAL; 
      } 
      if (shouldWeightX) { 
      c.weightx = 0.5; 
      } 

      ... 

      tf = new JTextField(); 
      c.fill = GridBagConstraints.HORIZONTAL; 
      c.gridx = 1; 
      c.gridy = 2; 
      frame.add(tf, c); 
      tf.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent e) 
       { 
        String chance1 = tf.getText(); 
       } 
      }); 

... 

답변

1

가 왜 ActionListener 대신 KeyListener 사용하고 있습니다 : 여기

코드인가?

당신은 KeyListener를 사용하거나해야합니다

tf.getDocument().addDocumentListener(documentListener); 

DocumentListener

+0

문서 수신기를 사용하면 사용자 입력을 변수에도 저장할 수 있습니까? 그리고 어떻게? –

1
public void actionPerformed(ActionEvent e) 
{ 
    // Look Ma, a one-liner! 
    String chance1 = JOptionPane.showInputDialog(someComponent, "Value Bet"); 
} 

option pane one-liner

가 견고, 모양을 조정할 오버로드 된 메소드로 더 좀 봐 null 검사를 추가 고려 텍스트 필드 (BNI) 대신 회 전자를 사용합니다.