2011-04-22 52 views
24

我們如何從文本字段中檢索值以及actionPerformed()?我需要將該值轉換爲String以供進一步處理。我創建了一個單擊按鈕的文本框,我需要將輸入的值存儲到String中,您可以提供一個代碼片段嗎?如何在Java Swing中從JTextField中檢索值?

+12

您是否試過閱讀javadoc? – 2011-04-22 04:37:46

+2

它是重複的。也許不是一個單一的問題。但這不是一個新問題。 [獲取文本](http://stackoverflow.com/questions/3885577/retrieve-jtextfield-text-value)&[添加動作偵聽器](http://stackoverflow.com/questions/4062195/adding-an-action-監聽到一個-JComboBox中)。另外我相信,如果你想谷歌,甚至有一個例子,你所描述的。 – Boro 2011-04-22 06:21:10

+0

它很容易從JTextfield獲得價值..嘗試閱讀java文檔........它會幫助你開發程序.. – Chetan 2015-01-30 08:57:55

回答

44
testField.getText() 

JTextField

示例代碼的Java文檔可以是:

button.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent ae){ 
     String textFieldValue = testField.getText(); 
     // .... do some operation on value ... 
    } 
}) 
4

我們如何檢索文本字段的值?

mytestField.getText(); 

ActionListner例如:

mytextField.addActionListener(this); 

public void actionPerformed(ActionEvent evt) { 
    String text = textField.getText(); 
    textArea.append(text + newline); 
    textField.selectAll(); 
} 
+0

你可以顯示'newline'聲明的位置嗎? – trashgod 2011-04-22 06:35:39

+1

@trashgod:它只是''\ n「'字符串 – 2011-04-22 06:54:46

+4

所以也許像'String newline = System.getProperty(」line.separator「);'會是正確的嗎? – trashgod 2011-04-22 07:03:54

2
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Swingtest extends JFrame implements ActionListener 
{ 
    JTextField txtdata; 
    JButton calbtn = new JButton("Calculate"); 

    public Swingtest() 
    { 
     JPanel myPanel = new JPanel(); 
     add(myPanel); 
     myPanel.setLayout(new GridLayout(3, 2)); 
     myPanel.add(calbtn); 
     calbtn.addActionListener(this); 
     txtdata = new JTextField(); 
     myPanel.add(txtdata); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     if (e.getSource() == calbtn) { 
      String data = txtdata.getText(); //perform your operation 
      System.out.println(data); 
     } 
    } 

    public static void main(String args[]) 
    { 
     Swingtest g = new Swingtest(); 
     g.setLocation(10, 10); 
     g.setSize(300, 300); 
     g.setVisible(true); 
    } 
} 
現在

其工作

+0

重新格式化的代碼;如果不正確請回復。不幸的是,這不能編譯。 – trashgod 2011-04-22 06:28:15

+0

請不要大喊;它讓你聽起來很生氣。即使忽略缺少的導入,它看起來像缺少構造函數。我能幫你解決嗎? – trashgod 2011-04-22 06:57:56

+0

你明白如何從文本框中獲得價值所以它好 – jayesh 2011-04-22 07:04:32

1

只需使用event.getSource() FRIM actionPerformed

投它內的組件

防爆,如果你需要的ComboBox

JComboBox comboBox = (JComboBox) event.getSource(); 
JTextField txtField = (JTextField) event.getSource(); 

使用適當的API來獲取值,

防爆。

Object selected = comboBox.getSelectedItem(); etc. 
7
* First we declare JTextField like this 

JTextField testField = new JTextField(10); 

* We can get textfield value in String like this on any button click event. 

button.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent ae){ 
     String getValue = testField.getText() 

    } 
}) 
0

我發現有用的是這樣的條件如下。

String tempEmail = ""; 
JTextField tf1 = new JTextField(); 

tf1.addKeyListener(new KeyAdapter(){ 
    public void keyTyped(KeyEvent evt){ 
     tempEmail = ((JTextField)evt.getSource()).getText() + String.valueOf(evt.getKeyChar()); 
    } 
});