Java learning: AWT component and event processing notes (1) -- text box, javaawt

Source: Internet
Author: User

Java learning: AWT component and event processing notes (1) -- text box, javaawt

The TextField class in the java. awt package is used to create a text box. That is, an object created by the TextField class is a text box.

1. Main Methods of the TextField class
(1) TextField ()
The created text box can be edited. You can enter several characters in the text box. It can be seen that the character sequence length is a machine Character length.
(2) TextField (int x)
The created text box can be edited. You can enter several characters in the text box. It can be seen that the character sequence length is x machine characters.
(3) TextField (String s)
The initial string of the created text box is s. The text box can be edited. You can enter several characters in the text box.
(4) public void setText (String s)
This method is called by the text box object. The text in the text box is cleared and the text in the text box is set to the text specified by parameter s.
(5) public String getText ()
You can call this method to obtain the text in the text box.
(6) public void setEchoChar (char c)
The text box object can call this method to set echo characters. When you input text in the text box, the text box only displays the characters specified by parameter c.
(7) public void setEditable (boolean B)
You can call this method to edit a text box.
(8) public void addActionListener (ActionListener l)
You can call this method to add an action monitor to the text box.
(9) public void removeActionListener (ActionListener l)
You can call this method to remove the Action Monitor in the text box.

Practice: design a window containing two text boxes
The Code is as follows:

Import java. awt. *; class WindowText extends Frame {TextField text1, text2; WindowText (String s) {super (s); setLayout (new FlowLayout ()); /* set the FlowLayout layout of the text box (arranged from left to right, full with line breaks) */text1 = new TextField ("Enter Password:", 10); text1.setEditable (false ); text2 = new TextField (10); text2.setEchoChar ('*'); add (text1); add (text2); setBounds (100,100,400,150); setVisible (true ); validate () ;}} public class Example2 {public static void main (String [] args) {WindowText win = new WindowText ("this is a window with a text box added ");}}

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.