

@Override
public void start(Stage primaryStage) {
// 创建第一个 WebView 实例用于加载 index.html
WebView webView1 = new WebView();
// 获取本地 HTML 文件 index.html 的路径
URL indexUrl1 = getClass().getResource("index.html");
if (indexUrl1 == null) {
System.err.println("Cannot find /web/index.html");
return;
}
// 加载 index.html 到第一个 WebView
webView1.getEngine().load(indexUrl1.toExternalForm());
// 创建根布局
StackPane root = new StackPane();
// 创建黑色小方块
Rectangle square = new Rectangle();
square.setFill(Color.BLACK);
square.getStyleClass().add("left-square"); // 为小方块添加样式类
square.setVisible(isSquareVisible); // 设置小方块初始不可见
// 将小方块的尺寸绑定到根布局
square.widthProperty().bind(Bindings.multiply(root.widthProperty(), 0.25));
square.heightProperty().bind(Bindings.multiply(root.heightProperty(), 1));
// 将小方块放置在左上角
StackPane.setAlignment(square, Pos.TOP_LEFT);
// 创建一个新的 StackPane 来管理按钮
StackPane buttonPane = new StackPane();
Button toggleButton = new Button(isSquareVisible ? "折叠" : "展开");
toggleButton.getStyleClass().add("toggle-button"); // 添加样式类
buttonPane.getChildren().add(toggleButton);
// 将按钮所在的 StackPane 添加到根布局
root.getChildren().addAll(webView1, square, buttonPane);
// 设置按钮始终位于左边,并绑定其位置到方块的宽度
buttonPane.translateXProperty().bind(square.widthProperty().add(-370));
buttonPane.translateYProperty().bind(Bindings.multiply(root.heightProperty(), 0.02));
// 为按钮的点击事件添加动作
toggleButton.setOnAction(event -> {
isSquareVisible = !isSquareVisible;
square.setVisible(isSquareVisible);
toggleButton.setText(isSquareVisible ? "折叠" : "展开");
isButtonClicked = true; // 按钮被点击时设置标志位
});
// 创建场景并设置根布局大小
Scene scene = new Scene(root, 800, 450);