JavaFX Stage/Scene 自动调整大小

JavaFX Stage/Scene automatic resize

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

但是现在,通过调用 root.getChildren().remove(child); 删除了下面的红框; Window(Stage) 或 Scene 如何自动调整大小。

在控制器中:

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.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.access00(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$handleMouseEvent4(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$null8(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--

根据您的方法,我认为 Dusko 是正确的,但我想问的是为什么要重新创建轮子?在此示例中,我使用 TitledPane 并随着 Scene's 高度变大或变小调整 Stage。谢谢,@James_D 获取有关如何实现此功能的信息。

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication97 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        Label tpLabel = new Label("text here!");
        TextField tpTextField = new TextField();
        Button tpButton = new Button("OK");
        HBox tpHBox = new HBox(tpTextField, tpButton);
        VBox topPanel = new VBox(tpLabel, tpHBox);

        TextArea bpTextArea = new TextArea();
        Button bpButton = new Button("Button");
        VBox bpVBox = new VBox(bpTextArea, bpButton);
        TextArea bpTextArea2 = new TextArea();
        Button bpButton2 = new Button("Button2");
        VBox bpVBox2 = new VBox(bpTextArea2, bpButton2);
        HBox bpHBox = new HBox(bpVBox, bpVBox2);

        TitledPane titledPane = new TitledPane();
        titledPane.setText("");
        titledPane.setContent(bpHBox);
        titledPane.setAnimated(false);
        Accordion bottomPanel = new Accordion(titledPane);
        VBox root = new VBox(topPanel, bottomPanel);

        //This adjust the Stage when the height of the Accordian change.
        bottomPanel.expandedPaneProperty().addListener((obs, oldVal, newVal) -> {
            System.out.println("hello");
            Platform.runLater(() -> {
                primaryStage.sizeToScene();    
            });
        });

        Scene scene = new Scene(root);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}