JavaFX 中的属性绑定(例题)

JavaFX的属性经常通过绑定来综合。这是一种表达变量间关系的机制。可以将一个目标对象和一个源对象绑定,如果源对象中的值改变了,则目标对象也将自动改变。目标对象称为绑定对象绑定属性,源对象称为可绑定对象可观察对象。下。下列程序是对《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.scene.shape.Line;
 6 import javafx.scene.shape.Rectangle;
 7 import javafx.stage.Stage;
 8 
 9 public class java1414 extends Application {
10     @Override // Override the start method in the Application class
11     public void start(Stage primaryStage) {
12         Pane pane = new Pane();
13         double paneWidth = 200;
14         double paneHeight = 200;
15 
16         // Draw the front rectangle
17         Rectangle r1 = new Rectangle();
18         r1.xProperty().bind(pane.widthProperty().multiply(0.05));
19         r1.yProperty().bind(pane.heightProperty().multiply(0.30));
20         r1.widthProperty().bind(pane.widthProperty().multiply(0.75));
21         r1.heightProperty().bind(pane.heightProperty().multiply(0.60));
22         r1.setFill(new Color(1, 1, 1, 0));
23         r1.setStroke(Color.BLACK);
24 
25         // Draw the back rectangle
26         Rectangle r2 = new Rectangle();
27         r2.xProperty().bind(pane.widthProperty().multiply(0.15));
28         r2.yProperty().bind(pane.heightProperty().multiply(0.06));
29         r2.widthProperty().bind(pane.widthProperty().multiply(0.75));
30         r2.heightProperty().bind(pane.heightProperty().multiply(0.60));
31         r2.setFill(new Color(1, 1, 1, 0));
32         r2.setStroke(Color.BLACK);
33 
34         // Connect the corners
35         Line line1 = new Line();
36         line1.startXProperty().bind(pane.widthProperty().multiply(0.05));
37         line1.startYProperty().bind(pane.heightProperty().multiply(0.30));
38         line1.endXProperty().bind(pane.widthProperty().multiply(0.15));
39         line1.endYProperty().bind(pane.heightProperty().multiply(0.06));
40         Line line2 = new Line();
41         line2.startXProperty().bind(pane.widthProperty().multiply(0.05));
42         line2.startYProperty().bind(pane.heightProperty().multiply(0.90));
43         line2.endXProperty().bind(pane.widthProperty().multiply(0.15));
44         line2.endYProperty().bind(pane.heightProperty().multiply(0.66));
45         Line line3 = new Line();
46         line3.startXProperty().bind(pane.widthProperty().multiply(0.80));
47         line3.startYProperty().bind(pane.heightProperty().multiply(0.30));
48         line3.endXProperty().bind(pane.widthProperty().multiply(0.90));
49         line3.endYProperty().bind(pane.heightProperty().multiply(0.06));
50         Line line4 = new Line();
51         line4.startXProperty().bind(pane.widthProperty().multiply(0.80));
52         line4.startYProperty().bind(pane.heightProperty().multiply(0.90));
53         line4.endXProperty().bind(pane.widthProperty().multiply(0.90));
54         line4.endYProperty().bind(pane.heightProperty().multiply(0.66));
55 
56         pane.getChildren().addAll(r1, r2, line1, line2, line3, line4);
57 
58         // Create a scene and place it in the stage
59         Scene scene = new Scene(pane, paneWidth, paneHeight);
60         primaryStage.setTitle("Exercise14_14"); // Set the stage title
61         primaryStage.setScene(scene); // Place the scene in the stage
62         primaryStage.show(); // Display the stage
63     }
64 }

 

转载于:https://www.cnblogs.com/PabloZeal/p/7536634.html

JavaFX绑定事件处理器对象通常是通过使用`EventHandler`接口或者通过lambda表达式来实现的。事件处理器是一种特定类型的对象,它包含了对事件的响应逻辑,当事件发生时,该逻辑会被执行。 1. 使用`EventHandler`接口:你可以创建一个实现了`EventHandler`接口的类,然后在类重写`handle`方法来定义当事件发生时应执行的操作。之后,你可以将这个事件处理器对象添加到相应的节点上。 2. 使用lambda表达式:Java 8及以后版本支持lambda表达式,这为事件处理器的创建提供了更简洁的方式。你可以直接使用lambda表达式来定义事件处理逻辑,并将其作为参数传递给`setOnEventType`方法,其`EventType`是你想要监听的事件类型。 以下是一个简单的例子,展示如何在JavaFX使用lambda表达式来绑定事件处理器对象: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.event.ActionEvent; import javafx.event.EventHandler; public class EventHandlerExample extends Application { @Override public void start(Stage primaryStage) { // 创建一个按钮 Button btn = new Button(); btn.setText("点击我"); // 使用lambda表达式绑定事件处理器 btn.setOnAction(event -> { System.out.println("按钮被点击了!"); }); // 创建根节点和场景 StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("事件处理器示例"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 在这个例子,我们创建了一个按钮,并通过`setOnAction`方法为它绑定了一个事件处理器,当按钮被点击时,控制台会输出一条消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值