1. 舞台-场景-控件
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;import java.util.Arrays;public class Main extends Application {public static void main(String[] args) {launch();}@Overridepublic void start(Stage stage) throws Exception {stage.setTitle("javaFx01-场景");// 控件Button button1 = new Button("button1");Button button2 = new Button("button2");Button button3 = new Button("button3");//布局// Pane box = new HBox(); // 水平布局Pane box = new VBox(); // 水平布局box.getChildren().addAll(Arrays.asList(button1, button2, button3)); // 将控件添加到布局// 场景Scene scene = new Scene(box, 800, 600); // 布局-场景的宽度和高度stage.setScene(scene);// 舞台展示stage.show();}
}
效果:
2.舞台
2.1窗口相关事件处理
import javafx.application.Application;
import javafx.stage.Stage;public class Main extends Application {public static void main(String[] args) {launch();}@Overridepublic void start(Stage stage) throws Exception {stage.setTitle("javaFx01-场景");// 窗口关闭/展示中/展示完成事件处理stage.setOnCloseRequest(e -> System.out.println("closeRequest:e->" + e.getEventType()));stage.setOnShowing(e -> System.out.println("showing:e->" + e.getEventType()));stage.setOnShown(e -> System.out.println("shown:e->" + e.getEventType()));// 舞台展示stage.show();}
}
打开应用程序后,并点击关闭按钮,有如下日志输出:
showing:e->WINDOW_SHOWING
shown:e->WINDOW_SHOWN
closeRequest:e->WINDOW_CLOSE_REQUEST