Java's tag development

Source: Internet
Author: User
Tags empty gettext trim

The

Label works exactly: Place a label on the form. This is very important for textfields and text areas without tags, and if we simply want to put the text in the information on the form can also use the same. We can use the paint () inside the drawstring () to place a literal at a certain location, as demonstrated in the first routine in this chapter. When we use the tag allows us to add other text components through layout management. (We'll go into the discussion later in this chapter.)
Using the builder we can create a label that includes the initialization text (this is our typical practice), a tag that includes one line of center, left, and right (the static result is defined in the class tag). If we forget that we can read values using GetText () and getalignment (), we can also use SetText () and setalignment () to change and adjust. The following example shows the characteristics of the label:
 

: Label1.java//Using labels import java.awt.*;

Import java.applet.*;
  public class Label1 extends Applet {TextField T1 = new TextField ("T1", 10);
  Label LABL1 = new label ("TextField T1");
  Label LABL2 = new Label ("");
  Label LABL3 = new Label ("", label.right);
  Button B1 = New button ("Test 1");
  Button B2 = New button ("Test 2");
    public void Init () {Add (LABL1); add (t1); Add (B1);
    Add (LABL2); Add (B2);
  Add (LABL3); Public boolean action (Event evt, Object Arg) {if (Evt.target.equals (B1)) Labl2.settext ("Text set into Label
    ");
      else if (evt.target.equals (B2)) {if Labl3.gettext (). Trim (). Length () = 0) labl3.settext ("Labl3");
      if (labl3.getalignment () = = Label.left) labl3.setalignment (label.center);
      else if (labl3.getalignment () ==label.center) labl3.setalignment (label.right);
  else if (labl3.getalignment () = = Label.right) labl3.setalignment (label.left);  else return super.action (EVT, ARG);
  return true; }
} ///:~

The first is the most typical use of labels: mark a text field or text area. In the second part of the routine, when we press the "Test 1" button to insert a string of empty spaces into the field by SetText (). Because the number of empty spaces does not equal the same number of characters (in a font of equal proportional spacing), when inserting text into the label we will see that the text will be omitted. In the third part of the example, the blank space is reserved when we first press "Test 2" to find that the label is empty (trim () deletes the space at the end of each string) and inserts a short label at the beginning of the left column. We press the button to adjust the rest of the work so we can see the effect.
We might think we could create an empty tag and then use SetText () to place the text in it. However, we cannot add text to an empty tag-presumably because the empty label has no width-so it is useless to create an empty label without text. In the example above, the "blank" tag is filled with empty spaces, so it's enough to accommodate the text added later.
Similarly, setalignment () has no effect on the typical text labels we create with the builder. The width of the label is the width of the text, so you can't make any adjustments to it. However, if we start a long label and then turn it into a short one, we can see the effect of the adjustment.
These cause events, together with their minimized dimensions, are found by the default layout manager used by the program. Part of the layout manager is included later in this chapter.

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.