JavaFx
富因特网应用,桌面,Web浏览器,支持触摸设备
javafx.application.Application
- javafx通过一个类继承Application来定义主类。
Application是javafx程序的入口点,是一个抽象类,必须子类化,它的start 方法为抽象方法,必须覆盖。 - start方法用于展示stage舞台。start方法的签名为start(stage)
- stage舞台类似于一个窗口,用于容纳场景scene。
舞台显示:stage.show() - 场景scene以树的形式组织,每一个子组件就是它的一个节点。根节点一般是Pane面板。
例:
public class MyJavaFX extends Application {
public MyJavaFX()
{
// System.out.println("constructor");
}
// @Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// System.out.println("start");
// Create a button and place it in the scene
Button btOK = new Button("OK");
Scene scene = new Scene(btOK, 200, 250);
primaryStage.setTitle("MyJavaFX"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
// System.out.println("launch");
launch(args);
}
}
Application的生命周期
public class Test extends Application{
public Test(){
System.out.println("Test constructor is invoked");
}
public void start(Stage primaryStage){
System.out.println("start method is invoked");
}
public static void main(String[] args){
System.out.println("launch application");
Application.launch(args);
}
}
运行结果:
launch application
Test constructor is invoked
start method is invoked
其运行顺序为: main—>constructor—>start
面板
容器类,自动将节点布局在一个希望的位置和大小。
布局面板
- Pane:布局面板的基类,它有getChildren()方法来返回面板中节点列表。
加入节点:pane.getChildren().add(节点)
删除节点:pane.getChildren().remove(节点)
- StackPane:节点放置在面板中央,并且叠加在其他节点之上。
加入节点:pane.getChildren().add(节点)
删除节点:pane.getChildren().remove(节点)
- FlowPane:节点以水平方式一行一行放置,或者垂直方式一列一列放置。
加入节点:pane.getChildren().add(节点)
删除节点:pane.getChildren().remove(节点)
- GridPane:节点放置在一个二维网格的单元格中。
加入节点:pane.add(节点)
删除节点:pane.getChildren().remove(节点)
- BorderPane:将节点放置在顶部,右部,底部,左边以及中间区域。
加入节点:pane.setTop/setRight/setBottom/setLeft/setCenter()
删除节点:pane.setTop/setRight/setBottom/setLeft/setCenter(null)
- HBox:节点放在单行中。
加入节点:pane.getChildren().add(节点)
删除节点:pane.getChildren().remove(节点)
- VBox:节点放在单列中。
加入节点:pane.getChildren().add(节点)
删除节点:pane.getChildren().remove(节点)
节点
可视化组件,比如一个形状,一个图像视图,一个UI组件或者一个面板。
- Text:在起始点(x,y)显示一个字符串。(x, y, “content”)
- Line:一个线段。
(startX,startY,endX,endY)
- Rectangle:一个矩形通过参数x,y,width,height,arcWidth及arcHeight定义。
- Circle:一个圆,由centerX,centerY,radius定义。
- Ellipse:一个椭圆,由centerX,centerY,radiusX及radiusY定义。
- Arc:一个圆弧。
Arc(centerX,centerY,radiusX,radiusY,startAngle,length)
- Polygon与 polyline:
属性绑定
定义
JavaFX的属性经常通过绑定来综合,这是一种表达变量间关系的强大机制。当对象被绑定后,一个对象的改变会自动被反射到另一个对象。对应用程序来说,这是非常有用的。
绑定可应用在图形用户界面(GUI)中,用来将应用程序的基础数据的改变同步显示出来。
使用
一个目标(target)采用 bind 方法和源(source)绑定
----> target.bind(source)
bind(Observable<?extends T> observable)
【其中,target要实现Property接口,source要是可观察的值(ObservaleValue允许值改变时通知绑定在其上的值)】
例
public class BindingDemo {
public static void main(String[] args) {
DoubleProperty d1 = new SimpleDoubleProperty(1);
DoubleProperty d2 = new SimpleDoubleProperty(2);
d1.bind(d2);
System.out.println("d1 is " + d1.getValue()
+ " and d2 is " + d2.getValue());
d2.setValue(70.2);
System.out.println("d1 is " + d1.getValue()
+ " and d2 is " + d2.getValue());
}
}
d1 is 2.0 and d2 is 2.0
d1 is 70.2 and d2 is 70.2
说明
- 此时target只能由source改变,即source单向改变target。
若想双向改变,则使用 target.bindBidirectional (source)(两边同一对象类型) - 使用unbind()解绑