1

JavaFx 应用程序中的 BorderPane 不显示底部区域节点,除非在使用 Button 事件切换场景时窗口最大化。如果场景一个接一个地切换,它的安排就完美了。我的代码中有错误还是这是默认行为?谢谢。系统:Windows XP Java 版本:7

我的SSCE:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class Main extends Application {
@Override
public void start(final Stage stage) {
    try {
        ///// 2nd scene
        BorderPane root2 = new BorderPane();
        root2.setPrefSize(stage.getWidth(),stage.getHeight());
        HBox buttons2=new HBox(50);
        buttons2.getChildren().addAll(new Button("Button1"),new Button("Button2"));
        buttons2.setAlignment(Pos.BOTTOM_CENTER);
        root2.setBottom(buttons2);
        final Scene scene2 = new Scene(root2,stage.getWidth(),stage.getHeight());
        ///// 1st scene
        VBox buttons1=new VBox();
        buttons1.setPrefSize(stage.getWidth(),stage.getHeight());
        Button nextSceneBtn=new Button("NEXT");
        buttons1.getChildren().add(nextSceneBtn);
        buttons1.setAlignment(Pos.CENTER);
        Scene scene1=new Scene(buttons1,stage.getWidth(),stage.getHeight());

            ////action event
            nextSceneBtn.setOnMouseClicked(new EventHandler<MouseEvent>(){
                @Override
                public void handle(MouseEvent event) {
                    stage.setScene(scene2);
                }

            });
            ///stage
            Screen screen = Screen.getPrimary();
            Rectangle2D bounds = screen.getVisualBounds();
            stage.setX(0);
            stage.setY(0);
            stage.setWidth(bounds.getWidth());
            stage.setHeight(bounds.getHeight());

            stage.setScene(scene1); //if it's #setScene(scene2) at the beginning, it's ok
        stage.show();       
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        launch(args);
    }
}
4

1 回答 1

1

这看起来像是一个错误,似乎已在 JavaFX 8 中修复。显然,如果您在 Windows XP 上运行,那么它的用途有限。

一种可能的解决方法是切换场景的根,而不是场景本身:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class Main extends Application {
@Override
public void start(final Stage stage) {
    try {
        ///// 2nd scene
        final BorderPane root2 = new BorderPane();
        root2.setPrefSize(stage.getWidth(),stage.getHeight());
        HBox buttons2=new HBox(50);
        buttons2.getChildren().addAll(new Button("Button1"),new Button("Button2"));
        buttons2.setAlignment(Pos.BOTTOM_CENTER);
        root2.setBottom(buttons2);
//        final Scene scene2 = new Scene(root2,stage.getWidth(),stage.getHeight());
        ///// 1st scene
        VBox buttons1=new VBox();
        buttons1.setPrefSize(stage.getWidth(),stage.getHeight());
        Button nextSceneBtn=new Button("NEXT");
        buttons1.getChildren().add(nextSceneBtn);
        buttons1.setAlignment(Pos.CENTER);
        final Scene scene1=new Scene(buttons1,stage.getWidth(),stage.getHeight());

            ////action event
            nextSceneBtn.setOnMouseClicked(new EventHandler<MouseEvent>(){
                @Override
                public void handle(MouseEvent event) {
//                    stage.setScene(scene2);
                    scene1.setRoot(root2);
                }

            });
            ///stage
            Screen screen = Screen.getPrimary();
            Rectangle2D bounds = screen.getVisualBounds();
            stage.setX(0);
            stage.setY(0);
            stage.setWidth(bounds.getWidth());
            stage.setHeight(bounds.getHeight());

            stage.setScene(scene1); //if it's #setScene(scene2) at the beginning, it's ok
        stage.show();       
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        launch(args);
    }
}
于 2014-04-18T14:34:56.130 回答