JavaFX 舞台/场景自动调整大小

标签 javafx javafx-8

enter image description here

如图所示,上面的红色框是一个 GridBox,下面是一个带有 Splitpane (ListView) 和 Gridpane (2 Buttons) 的 VBox。我想要实现的是在单击按钮“隐藏<<<”时隐藏下面的 VBox。

enter image description here

但是现在,通过调用 root.getChildren().remove(child); 删除了下面的红色框;窗口(舞台)或场景如何自动调整大小。

在 Controller 中:

public class FunctionOwnerFX extends Application{


public static String HIDE = "Hide Libraries <<<";
    public static String SHOW = "Show Libraries >>>";
    private boolean isHidden = false;

    @FXML 
    private TextField textfield;
    @FXML
    private Button btn2;
    @FXML
    private VBox vbox;
    @FXML
    private VBox libraryVbox;

    private Stage primaryStage;
    @Override
    public void start(Stage stage) throws Exception{
        primaryStage = stage;
        Parent root = FXMLLoader.load(getClass().getResource("new_function_owner.fxml"));
        Scene scene = new Scene(root);
        stage.setTitle("New Function Owner");
        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }


    @FXML
    protected void splitPaneControl(){
        isHidden = !isHidden;
        if (isHidden) {
            vbox.getChildren().remove(libraryVbox);
            primaryStage.sizeToScene();
            btn2.setText(SHOW);
        } else {
            vbox.getChildren().add(libraryVbox);
            primaryStage.sizeToScene();
            btn2.setText(HIDE);
        }
    }

    private ArrayList<String> getMatches(String str){
        return null;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

当我使用方法 sizeToScene() 时出现运行时错误:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 49 more
Caused by: java.lang.NullPointerException
at FunctionOwnerFX.splitPaneControl(FunctionOwnerFX.java:65)
... 58 more

最佳答案

我没有足够的声望来发表评论(需要 50 个,但我有 48 个)所以我会做出回答。现在回到主题,尝试在删除节点后在舞台对象上调用方法 sizeToScene。

https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Window.html#sizeToScene--

关于JavaFX 舞台/场景自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48464476/

相关文章:

JavaFX 抛出 ArrayIndexOutOfBoundsException

java - 如何将所有附加的监听器复制到 JavaFX 中的新节点?

java - 如何使用 JavaFX 创建三角形?

java - Ubuntu、JavaFX、Java 运行时环境检测到 fatal error

java - 如何绑定(bind)反向 boolean 值,JavaFX

java - 如何在 Gluon 项目中添加依赖项

java - 使用 javafx 打印日期(作为字符串) - 保持打印当前日期

当使用 fxml 作为 fxml 中的组件时,JavaFX FXML 没有正确地看到我的 Controller 方法 "Controller is not defined on root component"

java - First-Fit Bin Packing 算法跳过数字

JavaFX8 - 从不同线程引用 FXML 对象