学习Java的记录(三)---JavaFx

JavaFx

富因特网应用,桌面,Web浏览器,支持触摸设备

javafx.application.Application

  1. javafx通过一个类继承Application来定义主类。
    Application是javafx程序的入口点,是一个抽象类,必须子类化,它的start 方法为抽象方法,必须覆盖。
  2. start方法用于展示stage舞台。start方法的签名为start(stage)
  3. stage舞台类似于一个窗口,用于容纳场景scene。
    舞台显示:stage.show()
  4. 场景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

在这里插入图片描述

面板

容器类,自动将节点布局在一个希望的位置和大小。

布局面板

  1. Pane:布局面板的基类,它有getChildren()方法来返回面板中节点列表。
    加入节点:pane.getChildren().add(节点)
    删除节点:pane.getChildren().remove(节点)
  2. StackPane:节点放置在面板中央,并且叠加在其他节点之上。
    加入节点:pane.getChildren().add(节点)
    删除节点:pane.getChildren().remove(节点)
  3. FlowPane:节点以水平方式一行一行放置,或者垂直方式一列一列放置。
    加入节点:pane.getChildren().add(节点)
    删除节点:pane.getChildren().remove(节点)
  4. GridPane:节点放置在一个二维网格的单元格中。
    加入节点:pane.add(节点)
    删除节点:pane.getChildren().remove(节点)
  5. BorderPane:将节点放置在顶部,右部,底部,左边以及中间区域。
    加入节点:pane.setTop/setRight/setBottom/setLeft/setCenter()
    删除节点:pane.setTop/setRight/setBottom/setLeft/setCenter(null)
  6. HBox:节点放在单行中。
    加入节点:pane.getChildren().add(节点)
    删除节点:pane.getChildren().remove(节点)
  7. VBox:节点放在单列中。
    加入节点:pane.getChildren().add(节点)
    删除节点:pane.getChildren().remove(节点)

节点

可视化组件,比如一个形状,一个图像视图,一个UI组件或者一个面板。
形状

  1. Text:在起始点(x,y)显示一个字符串。(x, y, “content”)
  2. Line:一个线段。(startX,startY,endX,endY)
  3. Rectangle:一个矩形通过参数x,y,width,height,arcWidth及arcHeight定义。
  4. Circle:一个圆,由centerX,centerY,radius定义。
  5. Ellipse:一个椭圆,由centerX,centerY,radiusX及radiusY定义。
  6. Arc:一个圆弧。Arc(centerX,centerY,radiusX,radiusY,startAngle,length)
  7. 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

说明
  1. 此时target只能由source改变,即source单向改变target。
    若想双向改变,则使用 target.bindBidirectional (source)(两边同一对象类型)
  2. 使用unbind()解绑
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值