Java Learning: Notes for AWT components and event handling (1)--text box

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.