我們如何從文本字段中檢索值以及actionPerformed()
?我需要將該值轉換爲String
以供進一步處理。我創建了一個單擊按鈕的文本框,我需要將輸入的值存儲到String
中,您可以提供一個代碼片段嗎?如何在Java Swing中從JTextField中檢索值?
24
A
回答
44
testField.getText()
示例代碼的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();
}
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);
}
}
現在
其工作
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());
}
});
相關問題
- 1. 如何從jTextField中檢索信息
- 2. 如何從Java中的JTextField將字符串檢索到Action Listener?
- 3. 從數據庫中檢索最大值並顯示在java eclipse中的JTextfield中
- 4. java swing JTextField set PlaceHolder
- 5. Java Swing - 如何將光標定位在PlainDocument的JTextField中
- 6. 在Java Swing中獲取已更改的JTextField的值
- 7. 的Java Swing JTextField中的插圖
- 8. JTextField中的佔位符 - Java Swing
- 9. Java swing從JTextField獲取輸入
- 10. 如何在Java中檢索數組值?
- 11. 檢索JTextField的文本值
- 12. JTextField - Java swing - 無遊標?
- 13. JTextField橫向滾動java swing
- 14. (Java Swing)JTextField更新「太遲」
- 15. Java Swing - 動態創建JTextField
- 16. 滾動數字java swing JTextField
- 17. 如何在檢索一個值之後從列中檢索值
- 18. 從Java Swing組件檢索對象
- 19. Java Swing將JTextField值移動到第二個按鈕中
- 20. 在java中檢索hashmap值
- 21. 如何在Codeigniter中從form_dropdown()檢索值
- 22. 從Java中的MongoDB中檢索long值
- 23. java swing從不同線程的JTextField獲取值
- 24. 檢索java swing JFrame圖標
- 25. 在追加的Java Swing新線的JTextField
- 26. 更新Swing JTextField
- 27. 如何從Java中的arraylist中檢索值單擊超鏈接
- 28. 如何使用Java中的外鍵從表中檢索值?
- 29. 如何從Java 8中的HashMap中檢索值和鍵?
- 30. 如何從MySQL檢索圖像到Java Swing?
您是否試過閱讀javadoc? – 2011-04-22 04:37:46
它是重複的。也許不是一個單一的問題。但這不是一個新問題。 [獲取文本](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
它很容易從JTextfield獲得價值..嘗試閱讀java文檔........它會幫助你開發程序.. – Chetan 2015-01-30 08:57:55