The TextField class in the java.awt package is designed to create a text box, that is, an object created by the TextField class is a text box.
The main methods of the 1.TextField class
(1) TextField ()
The Created text box can be edited, and the user can enter several characters in the text box, and the visible character sequence length is one machine character long
(2) TextField (int x)
The Created text box can be edited, and the user can enter several characters in the text box, and the visible character sequence length is x machine characters long
(3) TextField (String s)
The initial string for the created text box is S, the text box is editable, and the user can enter several characters in the text box
(4) public void SetText (String s)
The text box object calls the method, emptying the text box with the previous text, and setting the text in the text box to the text specified by the parameter s
(5) Public String GetText ()
The text box object calls the method, and you can get the text in the text box
(6) public void Setechochar (char c)
The text box object calls this method to set the echo character, and the text box displays only the characters specified by the parameter C when the user makes text input in the text box
(7) public void Seteditable (Boolean b)
The text box object calls this method to set the editable nature of the text box
(8) public void addActionListener (ActionListener l)
The text box object calls this method to add an action monitor to the text box
(9) public void Removeactionlistener (ActionListener l)
The text box object calls this method to remove the action Monitor on the text box
Practical Practice: Design a window with two text boxes
The code is as follows:
Importjava.awt.*;classwindowtextextendsframe{TextField text1,text2; windowtext (String s) {Super(s); SetLayout (NewFlowLayout ());/*Set the text box FlowLayout layout (from left to right, full of line breaks)*/Text1=NewTextField ("Enter Password:", 10); Text1.seteditable (false); Text2=NewTextField (10); Text2.setechochar (‘*‘); Add (Text1); Add (TEXT2); SetBounds (100,100,400,150); SetVisible (true); Validate (); }} Public classexample2{ Public Static voidMain (string[] args) {windowtext win=Newwindowtext ("This is a window with a text box added") ; }}
Java Learning: Notes for AWT components and event handling (1)--text box