java - BorderPane 隐藏底部区域,直到窗口最大化

标签 java javafx-2 java-7 borderpane

JavaFx 应用程序中的 BorderPane 不会显示底部区域 Node,除非使用 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);
    }
}

最佳答案

这看起来像是一个错误,似乎已在 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);
    }
}

关于java - BorderPane 隐藏底部区域,直到窗口最大化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23155681/

相关文章:

Java swing JComboBox无法参数化

java - 在父类(super class)和子类中实现同名但不同实现的方法

java - Java 中的 Google Cloud Endpoints 硬编码客户端 ID 以及 useDatastoreForAdditionalConfig 的使用

java - Android - java.lang.ArrayIndexOutOfBoundsException

java - 如何创建密码字段或隐藏 TableView 中的密码

JavaFX ScrollPane - 加速触摸设备上的滑动效果

java - 附加到另一个类的对象?

Java - 通过文本 UI 将人员添加到链接列表

java - jdk1.6更新32和JavaFX2.1可以工作吗?

java - 为什么 Intellij IDEA 不接收我的 java JDK