多读书多实践,勤思考善领悟

JavaFX Layout Anchorpane(布局锚面板)

本文于1801天之前发表,文中内容可能已经过时。

锚点窗格允许子节点的边缘锚定到锚点窗格边缘的偏移量。如果锚窗格具有边框和/或填充集,则将从这些插图的内边缘测量偏移。

如果我们在应用程序中使用Anchor窗格,则其中的节点将锚定在距窗格特定距离处。

javafx.scene.layout的名为AnchorPane的类表示Anchor窗格。添加节点后,您需要从窗格的所有方向(顶部,底部,右侧和左侧)设置锚点。要设置锚点,此类提供四种方法,即 - setBottomAnchor(),setTopAnchor(),setLeftAnchor(),setRightAnchor()。对于这些方法,您需要传递表示锚点的double值。

以下程序是Anchor Pane布局的示例。在此,我们在锚窗格中插入旋转圆柱体。同时,我们将它设置在离所有方向(顶部,左侧,右侧,底部)的窗格50个单元的位置。

将此代码保存在名为AnchorPaneExample.java的文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import javafx.animation.RotateTransition; 
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class AnchorPaneExample extends Application {
@Override
public void start(Stage stage) {
//Drawing a Cylinder
Cylinder cylinder = new Cylinder();

//Setting the properties of the Cylinder
cylinder.setHeight(180.0f);
cylinder.setRadius(100.0f);

//Preparing the phong material of type diffuse color
PhongMaterial material = new PhongMaterial();
material.setDiffuseColor(Color.BLANCHEDALMOND);

//Setting the diffuse color material to Cylinder5
cylinder.setMaterial(material);

//Setting rotation transition for the cylinder
RotateTransition rotateTransition = new RotateTransition();

//Setting the duration for the transition
rotateTransition.setDuration(Duration.millis(1000));

//Setting the node for the transition
rotateTransition.setNode(cylinder);

//Setting the axis of the rotation
rotateTransition.setAxis(Rotate.X_AXIS);

//Setting the angle of the rotation
rotateTransition.setByAngle(360);

//Setting the cycle count for the transition
rotateTransition.setCycleCount(RotateTransition.INDEFINITE);

//Setting auto reverse value to false
rotateTransition.setAutoReverse(false);

//playing the animation
rotateTransition.play();

//Creating an Anchor Pane
AnchorPane anchorPane = new AnchorPane();

//Setting the anchor to the cylinder
AnchorPane.setTopAnchor(cylinder, 50.0);
AnchorPane.setLeftAnchor(cylinder, 50.0);
AnchorPane.setRightAnchor(cylinder, 50.0);
AnchorPane.setBottomAnchor(cylinder, 50.0);

//Retrieving the observable list of the Anchor Pane
ObservableList list = anchorPane.getChildren();

//Adding cylinder to the pane
list.addAll(cylinder);

//Creating a scene object
Scene scene = new Scene(anchorPane);

//Setting title to the Stage
stage.setTitle("Anchor Pane Example");

//Adding scene to the stage
stage.setScene(scene);

//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

使用以下命令从命令提示符编译并执行保存的java文件。

1
2
javac AnchorPaneExample.java 
java AnchorPaneExample

执行时,上面的程序生成一个JavaFX窗口,如下所示。

AnchorPane