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

6

这里输入图片描述

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

这里输入图片描述

但现在,通过调用root.getChildren().remove(child)来移除下方的红色框,那么窗口(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$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

2
primaryStage 只在启动应用程序时创建的 Application 实例中初始化(因为这是调用 start() 的对象)。它从未在控制器中初始化:因此它仍然为空,您会收到空指针异常。将 Application 类用作控制器类是非常糟糕的想法,因为它会导致此类问题。请使用单独的类作为控制器。您可以通过调用 getScene().getWindow() 从其中包含的任何节点获取窗口:例如,您可以简单地执行 vbox.getScene().getWindow().sizeToScene() - James_D
为什么不直接使用 TilePane - SedJ601
1
@Sedrick 你是不是指的是TitledPane - James_D
不,我没有什么窍门。 - SedJ601
1
@Sedrick 原来它只是调用了 sizeToScene() - James_D
显示剩余7条评论
2个回答

11

嗯,当我写这个的时候,它看起来更像是一条注释。 - Duško Mirković
1
我想我应该说这是一个很好的答案,考虑到问题中没有代码。无论如何,你现在可以发布评论了。 - James_D
非常感谢,这意味着很多,真的。 - Duško Mirković
抱歉,这是我第一次在StackOverflow上发布问题。我无法使用编辑器来发布我的代码(需要更多时间来弄清楚)。不管怎样,当我使用sizeToScene()时,运行时错误Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: Java.lang.reflect.InvocationTargetException发生了。 - Lio Mou
请在@LioMou 编辑您的问题,并在那里发布完整的堆栈跟踪信息。 - James_D

2

我认为Dusko的方法是正确的,但我想问一下为什么要重新发明轮子?在这个例子中,我使用了一个TitledPane并调整了Stage,使其与Scene的高度变化相适应。感谢@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);
    }

}

enter image description here

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接