Java Reference
In-Depth Information
34 Ellipse ellipse = new Ellipse( 50 , 50 , 50 , 25 );
35 ellipse.setStroke(Color.GREEN);
36 ellipse.setFill(Color.WHITE);
37 StackPane stackPane = new StackPane();
38 stackPane.getChildren().addAll(ellipse, new Label( "JavaFX" ));
39 Label lb5 = new Label( "A pane inside a label" , stackPane);
40 lb5.setContentDisplay(ContentDisplay.BOTTOM);
41
42 HBox pane = new HBox( 20 );
43 pane.getChildren().addAll(lb1, lb2, lb3, lb4, lb5);
44
45 // Create a scene and place it in the stage
46 Scene scene = new Scene(pane, 450 , 150 );
47 primaryStage.setTitle( "LabelWithGraphic" ); // Set the stage title
48 primaryStage.setScene(scene); // Place the scene in the stage
49 primaryStage.show(); // Display the stage
50 }
60 }
create a label
add labels to pane
F IGURE 16.4
The program displays labels with texts and nodes.
The program creates a label with a text and an image (line 19). The text is US\n50 States
so it is displayed in two lines. Line 21 specifies that the image is placed at the bottom of the
text.
The program creates a label with a text and a circle (line 24). The circle is placed on top of
the text (line 25). The program creates a label with a text and a rectangle (line 28). The rectan-
gle is placed on the right of the text (line 29). The program creates a label with a text and an
ellipse (line 31). The ellipse is placed on the left of the text (line 32).
The program creates an ellipse (line 34), places it along with a label to a stack pane
(line 38), and creates a label with a text and the stack pane as the node (line 39). As seen from
this example, you can place any node in a label.
The program creates an HBox (line 42) and places all five labels into the HBox (line 43).
16.1 How do you create a label with a node without a text?
16.2 How do you place a text on the right of the node in a label?
16.3 Can you display multiple lines of text in a label?
16.4 Can the text in a label be underlined?
Check
Point
16.3 Button
A button is a control that triggers an action event when clicked. JavaFX provides regular
buttons, toggle buttons, check box buttons, and radio buttons. The common features of these
buttons are defined in ButtonBase and Labeled classes as shown in Figure 16.5.
The Labeled class defines the common properties for labels and buttons. A button is just
like a label except that the button has the onAction property defined in the ButtonBase
class, which sets a handler for handling a button's action.
 
 
 
Search WWH ::




Custom Search