JTextField類是一個允許編輯單行文本的組件。
類聲明
以下是javax.swing.JTextField類的聲明 -
public class JTextField
extends JTextComponent
implements SwingConstants
字段
以下是javax.swing.JList類的字段 -
static String notifyAction- 要發送已接受字段內容的通知的操作的名稱。
類構造函數
| 編號 | 構造函數 | 描述 |
|---|---|---|
| 1 | JTextField() |
構造一個新的TextField。 |
| 2 | JTextField(Document doc, String text, int columns) |
構造一個新的JTextField,它使用給定的文本存儲模型和給定的列數。 |
| 3 | JTextField(int columns) |
構造具有指定列數的新空TextField。 |
| 4 | JTextField(String text) |
構造使用指定文本初始化的新TextField。 |
| 5 | JTextField(String text, int columns) |
構造一個使用指定文本和列初始化的新TextField。 |
類構造函數
| 編號 | 類方法 | 描述 |
|---|---|---|
| 1 | protected void actionPropertyChanged(Action action, String propertyName) |
更新文本字段的狀態以回應關聯操作中的屬性更改。 |
| 2 | void addActionListener(ActionListener l) |
添加指定的操作偵聽器以從此文本字段接收操作事件。 |
| 3 | protected void configurePropertiesFromAction(Action a) |
設置此文本字段上的屬性以匹配指定Action中的屬性。 |
| 4 | protected PropertyChangeListener createActionPropertyChangeListener(Action a) |
創建並返回一個PropertyChangeListener,它負責偵聽指定Action的更改並更新相應的屬性。 |
| 5 | protected Document createDefaultModel() |
如果沒有明確給出,則創建要在構造中使用的模型的默認實現。 |
| 6 | protected void fireActionPerformed() |
通知所有已註冊對此事件類型的通知感興趣的監聽器。 |
| 7 | AccessibleContext getAccessibleContext() |
獲取與此JTextField關聯的AccessibleContext。 |
| 8 | Action getAction() |
返回此ActionEvent源的當前設置Action,如果未設置Action,則返回null。 |
| 9 | ActionListener[] getActionListeners() |
返回使用addActionListener()添加到此JTextField的所有ActionListener的數組。 |
| 10 | Action[] getActions() |
獲取編輯器的命令列表。 |
| 11 | int getColumns() |
返回此TextField中的列數。 |
| 12 | protected int getColumnWidth() |
返回列寬。 |
| 13 | int getHorizontalAlignment() |
返回文本的水準對齊方式。 |
| 14 | BoundedRangeModel getHorizontalVisibility() |
獲取文本字段的可見性。 |
| 15 | Dimension getPreferredSize() |
返回此TextField所需的首選大小。 |
| 16 | int getScrollOffset() |
獲取滾動偏移量(以像素為單位)。 |
| 17 | String getUIClassID() |
獲取UI的類ID。 |
| 18 | boolean isValidateRoot() |
通過驗證textfield來處理來自textfield本身內部的重新驗證調用,除非textfield包含在JViewport中,否則返回false。 |
| 19 | protected String paramString() |
返回此JTextField的字串表示形式。 |
| 20 | void postActionEvent() |
處理在此文本字段上發生的操作事件,方法是將它們分派給任何已註冊的ActionListener對象。 |
| 21 | void removeActionListener(ActionListener l) |
刪除指定的操作偵聽器,以便它不再從此文本字段接收操作事件。 |
| 22 | void scrollRectToVisible(Rectangle r) |
向左或向右滾動字段。 |
| 23 | void setAction(Action a) |
設置ActionEvent源的Action。 |
| 24 | void setActionCommand(String command) |
設置用於操作事件的命令字符串。 |
| 25 | void setColumns(int columns) |
設置此TextField中的列數,使佈局無效。 |
| 26 | void setDocument(Document doc) |
將編輯器與文本文檔關聯。 |
| 27 | void setFont(Font f) |
設置當前字體。 |
| 28 | void setHorizontalAlignment(int alignment) |
設置文本的水準對齊方式。 |
| 29 | void setScrollOffset(int scrollOffset) |
設置滾動偏移(以像素為單位)。 |
方法繼承
該類繼承以下類中的方法 -
javax.swing.text.JTextComponentjavax.swing.JComponentjava.awt.Containerjava.awt.Componentjava.lang.Object
JTextField示例
使用編輯器創建以下Java程式 -
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextFieldExample {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public JTextFieldExample(){
prepareGUI();
}
public static void main(String[] args){
JTextFieldExample swingControlDemo = new JTextFieldExample();
swingControlDemo.showTextFieldDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing JTextField示例");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showTextFieldDemo(){
headerLabel.setText("Control in action: JTextField");
JLabel namelabel= new JLabel("用戶名: ", JLabel.RIGHT);
JLabel passwordLabel = new JLabel("密碼: ", JLabel.CENTER);
final JTextField userText = new JTextField(6);
final JPasswordField passwordText = new JPasswordField(6);
JButton loginButton = new JButton("登錄");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "用戶名: " + userText.getText();
data += ", 密碼: " + new String(passwordText.getPassword());
statusLabel.setText(data);
}
});
controlPanel.add(namelabel);
controlPanel.add(userText);
controlPanel.add(passwordLabel);
controlPanel.add(passwordText);
controlPanel.add(loginButton);
mainFrame.setVisible(true);
}
}
執行上面示例代碼,得到以下結果:

