Java Reference
In-Depth Information
The abstract
Node
class defines many properties and methods that are common to all nodes.
Key
Point
Nodes share many common properties. This section introduces two such properties
style
and
rotate
.
JavaFX style properties are similar to cascading style sheets (CSS) used to specify the styles
for HTML elements in a Web page. So, the style properties in JavaFX are called
JavaFX CSS
.
In JavaFX, a style property is defined with a prefix
-fx-
. Each node has its own style proper-
ties. You can find these properties from
http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/
cssref.html
.
For information on HTML and CSS, see Supplements V.A and V.B. If you are not
familiar with HTML and CSS, you can still use JavaFX CSS.
The syntax for setting a style is
styleName:value
. Multiple style properties for a node
can be set together separated by semicolon (
;
). For example, the following statement
JavaFX CSS
circle.setStyle(
"
-fx-stroke: black; -fx-fill: red;
"
);
setStyle
sets two JavaFX CSS properties for a circle. This statement is equivalent to the following two
statements.
circle.setStroke(Color.BLACK);
circle.setFill(Color.RED);
If an incorrect JavaFX CSS is used, your program will still compile and run, but the style
is ignored.
The
rotate
property enables you to specify an angle in degrees for rotating the node
from its center. If the degree is positive, the rotation is performed clockwise; otherwise, it is
performed counterclockwise. For example, the following code rotates a button 80 degrees.
button.setRotate(
80
);
Listing 14.7 gives an example that creates a button, sets its style, and adds it to a pane. It
then rotates the pane 45 degrees and set its style with border color red and background color
light gray, as shown in FigureĀ 14.8.
L
ISTING
14.7
NodeStyleRotateDemo.java
1
import
javafx.application.Application;
2
import
javafx.scene.Scene;
3
import
javafx.scene.control.Button;
4
import
javafx.stage.Stage;
5
import
javafx.scene.layout.StackPane;
6
7
public class
NodeStyleRotateDemo
extends
Application {
8 @Override
// Override the start method in the Application class
9
public void
start(Stage primaryStage) {
10
// Create a scene and place a button in the scene
11 StackPane pane =
new
StackPane();
12 Button btOK =
new
Button(
"OK"
);
13 btOK.setStyle(
"
-fx-border-color: blue;
"
);
14 pane.getChildren().add(btOK);
15
16
pane.setRotate(
45
);
rotate the pane
set style for pane
17
pane.setStyle(
18
"
-fx-border-color: red; -fx-background-color: lightgray;
"
);
19
20 Scene scene =
new
Scene(pane,
200
,
250
);
21 primaryStage.setTitle(
"
NodeStyleRotateDemo
"
);
// Set the stage title
22 primaryStage.setScene(scene);
// Place the scene in the stage
Search WWH ::
Custom Search