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();
}
@Override
public 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();
}
@Override
public 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