打开时调整场景大小&关闭TitledPane

时间:2015-01-08 20:50:11

标签: javafx

我想知道,在扩展和扩展时,我如何调整Scene的大小。折叠TitledPane

我曾尝试过只有1 TitledPane

的案例

1 TitledPane

package fxinswing;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class FXInSwing extends Application {

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

    @Override public void start(Stage stage) {

        TitledPane tp = new TitledPane();

        GridPane grid = new GridPane();
        grid.setVgap(4);
        grid.setPadding(new Insets(5, 5, 5, 5));
        grid.add(new Label("First Name: "), 0, 0);
        grid.add(new TextField(), 1, 0);
        grid.add(new Label("Last Name: "), 0, 1);
        grid.add(new TextField(), 1, 1);
        grid.add(new Label("Email: "), 0, 2);
        grid.add(new TextField(), 1, 2);        
        tp.setText("Grid 1");
        tp.setContent(grid);

        stage.setTitle("TitledPane");
        Scene scene = new Scene(new Group());
        scene.setFill(Color.GHOSTWHITE);

        Group root = (Group)scene.getRoot();
        root.getChildren().add(tp);

        stage.setScene(scene);
        stage.show();
    }
}

我在关闭期间的期望

enter image description here

这是我在开业期间所期待的

enter image description here

但是,目前,当它打开或关闭时,它始终保持相同的方式(箭头的方向除外)

enter image description here

我很想知道,我如何打包Scene相应的尺寸,以便它在打开和放置期间始终“适合”TitledPane的实际尺寸。闭。

更新

@James_D提出了克服1 TitledPane案例的好方法。

然而,当使用2 TitledPane s

2 TitledPanes

package fxinswing;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class FXInSwing extends Application {

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

    @Override public void start(final Stage stage) {
        stage.setTitle("TitledPane");
        Scene scene = new Scene(new GridPane());
        scene.setFill(Color.GHOSTWHITE);

        final GridPane  root = (GridPane )scene.getRoot();

        TitledPane tp = new TitledPane();

        GridPane grid = new GridPane();
        grid.setVgap(4);
        grid.setPadding(new Insets(5, 5, 5, 5));
        grid.add(new Label("First Name: "), 0, 0);
        grid.add(new TextField(), 1, 0);
        grid.add(new Label("Last Name: "), 0, 1);
        grid.add(new TextField(), 1, 1);
        grid.add(new Label("Email: "), 0, 2);
        grid.add(new TextField(), 1, 2);        
        tp.setText("Grid 1");
        tp.setContent(grid);

        tp.heightProperty().addListener(new ChangeListener() {

            @Override
            public void changed(ObservableValue ov, Object t, Object t1) {
                stage.sizeToScene();
            }

        });

        TitledPane tp2 = new TitledPane();

        GridPane grid2 = new GridPane();
        grid2.setVgap(4);
        grid2.setPadding(new Insets(5, 5, 5, 5));
        grid2.add(new Label("First Name: "), 0, 0);
        grid2.add(new TextField(), 1, 0);
        grid2.add(new Label("Last Name: "), 0, 1);
        grid2.add(new TextField(), 1, 1);
        grid2.add(new Label("Email: "), 0, 2);
        grid2.add(new TextField(), 1, 2);        
        tp2.setText("Grid 2");
        tp2.setContent(grid2);


        tp2.heightProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue ov, Object t, Object t1) {
                stage.sizeToScene();
            }            
        });

        root.add(tp, 0, 0);
        root.add(tp2, 0, 1);

        stage.setScene(scene);
        stage.show();
    }
}

结果如下。 Scene永远不会调整大小,无论您是打开还是关闭TitledPane

我不使用Accordion,因为我希望两个TitledPane彼此独立。

我试过了

root.requestLayout();

但是,仍然没有任何改变。知道如何让2 TitledPane工作吗?

enter image description here

1 个答案:

答案 0 :(得分:3)

这似乎有效:

    tp.heightProperty().addListener((obs, oldHeight, newHeight) -> stage.sizeToScene());