Javafx-ui Control-Label __java

Source: Internet
Author: User
Tags documentation
  2 label (label)


This chapter describes how to use a label (label), which is located in the Javafx.scene.control package of the JavaFX API to display a text element. Next, we'll show you how to wrap text elements around a restricted space, add an icon, or use visual effects.

Figure 2-1 shows the three common uses of labels. The left side of the label is a text with an icon, the middle of the display of the rotation effect, the right side of the use of automatic line wrapping settings.

Figure 2-1 Label Example


This picture shows three labels, and they are placed on the same line. The label on the left has an icon that looks like a magnifying glass and a "Search" text. The middle label has a "Values" text and rotates 270 degrees clockwise. The label on the right contains the following text: "A label that is needs to be wrapped." Because the text is set to wrap automatically, so it takes two lines to fit.

Create a label

The JavaFX API provides three constructors for the label, see Code fragment example 2-1.

Example 2-1 creating a label

An empty label label
Label1 = new label ();
The label of the specified text is
label2 = new label ("Search");
Specifies the label image image of the text and icon
= new Image (GetClass (). getResourceAsStream ("labels.jpg"));
Label label3 = new label ("Search", new ImageView (image));

After creating a label, you can use the following methods to set or modify text and graphic content

SetText (String text)--Specify text content

Setgraphic (Node graphic)--Specify the graphics icon

The Settextfill () method can set the fill color of the text. Look at code fragment example 2-2, which creates a text label, adds an icon to it, and specifies the text fill color.

Example 2-2 Add an icon and text to fill a label

Label Label1 = new label ("Search");
Image image = New Image (GetClass (). getResourceAsStream ("labels.jpg"));
Label1.setgraphic (new ImageView (image));
Label1.settextfill (Color.web ("#0076a3"));

Run this code, and the result is as shown in Figure 2-2.

Figure 2-2 Label with an icon

When you set both text and graphic content for a label, you can use the Setgraphictextgap () method to set the spacing between them.

Alternatively, you can use the Settextalignment () method to change its alignment [translator Note: This method only works on multiple lines of text (auto wrap or \ n-wrap), one-line text does not work]. You can also define the position of the graphic relative to the text by using the Setcontentdisplay (Contentdisplay value) method, and the Contentdisplay optional value is: Left and right, centre center, top, Living under the bottom.


Set Font

Compared to the Search tab in Figures 2-1 and 2-2, figure 2-1 has a larger font size. This is because code fragment example 2-2 does not have any fonts set, and it uses the default font.

You can use the SetFont () method to specify a different font to distinguish it from a non-default font. The font setting for Label1 text in code fragment example 2-3 is set to Arial 30,label2 set to Cambria 32. Example 2-3 font settings

Use the constructor of the font class to construct the Font object
label1.setfont (New Font ("Arial");
Use the static method of the font class to construct the Font object
Label2.setfont (Font.font ("Cambria", 32));


Text Wrap

When you need to place a label in a smaller space, the wrapping can display more content while adapting to the size of the container. Calling the Setwraptext (true) method wraps the line automatically, as in code fragment example 2-4.

Example 2-4 enable text wrapping

Label label3 = new Label ("A Label that is needs to is wrapped");
Label3.setwraptext (TRUE);

Run this code, and the result is as shown in Figure 2-3.

Figure 2-3 Text label for wrapping automatically

If the display area provided to the label has not only a width limit, but also a height limit, you can specify the display behavior of the label when all text content cannot be displayed. Use the Settextoverrun (Overrunstyle value) method, where Overrunstyle is used to indicate how to handle text that cannot be fully rendered. The default display of text that is not full will be ... Be prompted. For more information, see the API documentation for Overrunstyle.


Visual Effects

Although a label is a static content that cannot be edited, you can still use visual effects or transform effects on it. The Label2 in code fragment example 2-5 rotates 270 degrees and moves its vertical position.

Example 2-5 rotate a label

Label Label2 = new label ("Values");
Label2.setfont (New Font ("Cambria");
Label2.setrotate (270);
Label2.settranslatey (50);

Rotation and displacement are typical transformation effects of the JavaFX API. In addition, you can set a zoom effect to zoom in when the user hovers the mouse cursor over the label.

A magnification effect is set for LABEL3 in code fragment example 2-6. The Mouse entry event (mouse_entered) is triggered on the label, and the magnification is set at 1.5 by the Setscalex and Setscaley methods. When the user moves the mouse cursor out of the label, the mouse left event (mouse_exited) is triggered, and the scaling factor is restored to 1.0, which is the original size.

Example 2-6 scaling effect

Label3.setonmouseentered ((mouseevent e)-> {
    Label3.setscalex (1.5);
    Label3.setscaley (1.5);
});

Label3.setonmouseexited ((mouseevent e)-> {
    Label3.setscalex (1);
    Label3.setscaley (1);
});

Figure 2-4 shows the two states of the label3.

Figure 2-4 Enlarge the label

Related API documentation

Label

Labeled

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.