Java Reference
In-Depth Information
6 import javafx.stage.Stage;
7 import javafx.scene.text.Text;
8 import javafx.scene.text.Font;
9 import javafx.scene.text.FontWeight;
10 import javafx.scene.text.FontPosture;
11
12 public class ShowText extends Application {
13 @Override // Override the start method in the Application class
14 public void start(Stage primaryStage) {
15 // Create a pane to hold the texts
16 Pane pane = new Pane();
17 pane.setPadding( new Insets( 5 , 5 , 5 , 5 ));
18 Text text1 = new Text( 20 , 20 , "Programming is fun" );
19 text1.setFont(Font.font( "Courier " , FontWeight.BOLD,
20 FontPosture.ITALIC, 15 ));
21
create a pane
create a text
set text font
pane.getChildren().add(text1);
add text to pane
22
23 Text text2 = new Text( 60 , 60 , " Programming is fun\nDisplay text" );
24
create a two-line text
add text to pane
pane.getChildren().add(text2);
25
26 Text text3 = new Text( 10 , 100 , "Programming is fun\nDisplay text" );
27 text3.setFill(Color.RED);
28 text3.setUnderline( true );
29 text3.setStrikethrough( true );
30
create a text
set text color
set underline
set strike line
add text to pane
pane.getChildren().add(text3);
31
32 // Create a scene and place it in the stage
33 Scene scene = new Scene(pane);
34 primaryStage.setTitle( "ShowText" ); // Set the stage title
35 primaryStage.setScene(scene); // Place the scene in the stage
36 primaryStage.show(); // Display the stage
37 }
38 }
The program creates a Text (line 18), sets its font (line 19), and places it to the pane
(line 21). The program creates another Text with multiple lines (line 23) and places it to the
pane (line 24). The program creates the third Text (line 26), sets its color (line 27), sets an
underline and a strike through line (lines 28-29), and places it to the pane (line 30).
14.11.2 Line
A line connects two points with four parameters startX , startY , endX , and endY , as shown
in Figure  14.29a. The Line class defines a line. The UML diagram for the Line class is
shown in Figure 14.28. Listing 14.15 gives an example that demonstrates text, as shown in
Figure 14.29b.
L ISTING 14.15
ShowLine.java
1 import javafx.application.Application;
2 import javafx.scene.Scene;
3 import javafx.scene.layout.Pane;
4 import javafx.scene.paint.Color;
5 import javafx.stage.Stage;
6 import javafx.scene.shape.Line;
7
8 public class ShowLine extends Application {
9 @Override // Override the start method in the Application class
10
public void start(Stage primaryStage) {
11
// Create a scene and place it in the stage
 
 
Search WWH ::




Custom Search