介绍

textField组件允许用户编辑单行文本。当用户在文本字段中键入键时,事件将发送到TextField。按键事件可以是按下按键,释放按键或键入按键。密钥事件将传递给已注册的KeyListener。如果在文本字段上启用了ActionEvent,则也可以使用ActionEvent,然后可以通过按回车键来触发ActionEvent。

类声明

以下是 java.awt.TextField 类的声明:

  1. public class TextField
  2. extends TextComponent

类的构造函数

S.N.Constructor & Description
1 TextField() Constructs a new text field.
2 TextField(int columns) Constructs a new empty text field with the specified number of columns.
3 TextField(String text) Constructs a new text field initialized with the specified text.
4 TextField(String text, int columns) Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of columns.

类方法

S.N.Method & Description
1 void addActionListener(ActionListener l) Adds the specified action listener to receive action events from this text field.
2 void addNotify() Creates the TextField's peer.
3 boolean echoCharIsSet() Indicates whether or not this text field has a character set for echoing.
4 AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this TextField.
5 ActionListener[] getActionListeners() Returns an array of all the action listeners registered on this textfield.
6 int getColumns() Gets the number of columns in this text field.
7 char getEchoChar() Gets the character that is to be used for echoing.
8 <T extends EventListener> T[] getListeners(Class<T> listenerType) Returns an array of all the objects currently registered as FooListeners upon this TextField.
9 Dimension getMinimumSize() Gets the minumum dimensions for this text field.
10 Dimension getMinimumSize(int columns) Gets the minumum dimensions for a text field with the specified number of columns.
11 Dimension getPreferredSize() Gets the preferred size of this text field.
12 Dimension getPreferredSize(int columns) Gets the preferred size of this text field with the specified number of columns.
13 Dimension minimumSize() Deprecated. As of JDK version 1.1, replaced by getMinimumSize().
14 Dimension minimumSize(int columns) Deprecated. As of JDK version 1.1, replaced by getMinimumSize(int).
15 protected String paramString() Returns a string representing the state of this TextField.
16 Dimension preferredSize() Deprecated. As of JDK version 1.1, replaced by getPreferredSize().
17 Dimension preferredSize(int columns) Deprecated. As of JDK version 1.1, replaced by getPreferredSize(int).
18 protected void processActionEvent(ActionEvent e) Processes action events occurring on this text field by dispatching them to any registered ActionListener objects.
19 protected void processEvent(AWTEvent e) Processes events on this text field.
20 void removeActionListener(ActionListener l) Removes the specified action listener so that it no longer receives action events from this text field.
21 void setColumns(int columns) Sets the number of columns in this text field.
22 void setEchoChar(char c) Sets the echo character for this text field.
23 void setEchoCharacter(char c) Deprecated. As of JDK version 1.1, replaced by setEchoChar(char).
24 void setText(String t) Sets the text that is presented by this text component to be the specified text.

方法继承

此类从以下类继承方法:

  • java.awt.TextComponent

  • java.awt.Component

  • java.lang.Object

TextField示例

使用您选择的任何编辑器在 D:/ > AWT > com > tutorialspoint > gui >创建以下Java程序 AwtControlDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtControlDemo {
  7.  
  8. private Frame mainFrame;
  9. private Label headerLabel;
  10. private Label statusLabel;
  11. private Panel controlPanel;
  12.  
  13. public AwtControlDemo(){
  14. prepareGUI();
  15. }
  16.  
  17. public static void main(String[] args){
  18. AwtControlDemo awtControlDemo = new AwtControlDemo();
  19. awtControlDemo.showTextFieldDemo();
  20. }
  21.  
  22. private void prepareGUI(){
  23. mainFrame = new Frame("Java AWT Examples");
  24. mainFrame.setSize(400,400);
  25. mainFrame.setLayout(new GridLayout(3, 1));
  26. mainFrame.addWindowListener(new WindowAdapter() {
  27. public void windowClosing(WindowEvent windowEvent){
  28. System.exit(0);
  29. }
  30. });
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showTextFieldDemo(){
  47. headerLabel.setText("Control in action: TextField");
  48.  
  49. Label namelabel= new Label("User ID: ", Label.RIGHT);
  50. Label passwordLabel = new Label("Password: ", Label.CENTER);
  51. final TextField userText = new TextField(6);
  52. final TextField passwordText = new TextField(6);
  53. passwordText.setEchoChar('*');
  54.  
  55. Button loginButton = new Button("Login");
  56.  
  57. loginButton.addActionListener(new ActionListener() {
  58. public void actionPerformed(ActionEvent e) {
  59. String data = "Username: " + userText.getText();
  60. data += ", Password: " + passwordText.getText();
  61. statusLabel.setText(data);
  62. }
  63. });
  64.  
  65. controlPanel.add(namelabel);
  66. controlPanel.add(userText);
  67. controlPanel.add(passwordLabel);
  68. controlPanel.add(passwordText);
  69. controlPanel.add(loginButton);
  70. mainFrame.setVisible(true);
  71. }
  72. }

使用命令提示符编译程序。转到 D:/ > AWT 并键入以下命令。

  1. D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java

如果没有错误,则表示编译成功。使用以下命令运行程序。

  1. D:\AWT>java com.tutorialspoint.gui.AwtControlDemo

验证以下输出

awt_textfield.jpg