我只是试图理解当我改变场景时为什么窗口大小(我设置为全屏)正在改变,是否有一种方法(即一个命令)可以发出一次让Java FX知道不改变大小吗?
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
Scene scene,scene_2;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage Win_primary) throws Exception {
Win_primary.setTitle("AN-0");
Button start,exit_pro,back_to_start;
start= new Button();
start.setText("Start");
Image exit_door = new Image("file:C:\\Users\\MyPc\\Pictures\\FX\\vector-icons_05-128 (2).png");
exit_pro= new Button ();
exit_pro.setGraphic(new ImageView(exit_door));
start.setStyle(
"-fx-background-radius: 100em; " +
"-fx-min-width: 200px; " +
"-fx-min-height: 200px; " +
"-fx-max-width: 200px; " +
"-fx-max-height: 170px;" +
"-fx-font-size: 20px"
);
exit_pro.setMaxSize(50, 50);
exit_pro.setOnAction(e -> Win_primary.close());
StackPane layout = new StackPane();
layout.setStyle("-fx-background-color: #FFFFFF ;");
layout.getChildren().add(start);
layout.setAlignment(exit_pro, Pos.BOTTOM_CENTER);
layout.getChildren().add(exit_pro);
start.setOnAction(e -> {
Win_primary.setScene(scene_2);
});
scene = new Scene(layout, 300, 250);
//scene 2
StackPane layout2 = new StackPane();
back_to_start = new Button("Cancel");
layout2.setStyle("-fx-background-color: #FFFFFF ;");
layout2.getChildren().add(back_to_start);
layout.setAlignment(back_to_start, Pos.BOTTOM_CENTER);
scene_2 = new Scene (layout2,300,250);
back_to_start.setOnAction(e -> Win_primary.setScene(scene));
Win_primary.setScene(scene);
Win_primary.setMinHeight(700);
Win_primary.setMinWidth(700);
Win_primary.setMaximized(true);
Win_primary.setFullScreen(true);
Win_primary.show();
}
}
答案 0 :(得分:1)
我有同样的问题,最终没有改变场景,只是场景的根。所以而不是:
start.setOnAction(e -> {
Win_primary.setScene(scene_2);
});
然后你会写:
start.setOnAction(e -> {Win_primary.getScene().setRoot(layout2)});
答案 1 :(得分:1)
我喜欢这样做:
StackPane layout = new StackPane();
layout.getChildren().add(exit_pro);
layout.getChildren().add(back_to_start);
start.setOnAction(e -> {
back_to_start.toFront();
exit_pro.setVisible(false);
});
back_to_start.setOnAction(e -> {
exit_pro.toFront();
back_to_start.setVisible(false);
});
切换StackPane
的孩子,就像切换两个场景一样。