客户端 UI 编程
快速进入
Starter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class App extends Application { @Override public void start(Stage primaryStage) { Pane pane = new Pane(); Scene scene = new Scene(pane, 800, 600); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { App.launch(args); }}
增加界面元素
3大步
1. 新建组件对象
2. 设置属性
3. 增加到父级容器里
样例代码
1 2 3 4 5 6 7 8+9 10 11+12+13 14 15+16 17 18 19 20 21 22 23 24 public class App extends Application { @Override public void start(Stage primaryStage) { Pane pane = new Pane(); Scene scene = new Scene(pane, 1000, 100); // step 1 Label label = new Label("LABEL"); // step 2 label.setLayoutX(100); label.setLayoutY(20); // step 3 pane.getChildren().add(label); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { App.launch(args); }}
事件响应
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 package com;public class App extends Application { @Override public void start(Stage primaryStage) { Pane pane = new Pane(); Scene scene = new Scene(pane, 1000, 100); ... Button button = new Button("点我啊"); button.setLayoutX(100); button.setLayoutY(80); button.setPrefWidth(100); pane.getChildren().add(button); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { // 被点击时会执行 } }); ... primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { App.launch(args); }}