我想知道扩展和折叠Scene
时如何调整TitledPane
的大小。
我已经尝试过只有1个TitledPane
的情况
1个标题为Pane
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();
}
}
我期望在结帐时
这是我在开业期间的期望
但是,目前,当它打开或关闭时,它始终保持相同的方式(箭头方向除外)
我想知道,如何包装我更改后的
Scene
大小,以便在打开和关闭期间,它总是能“适合” TitledPane
的实际大小。更新:
@James_D提出了一种克服1个
TitledPane
情况的好方法。但是,使用2个
TitledPane
时2标题标题
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
工作吗? 最佳答案
这似乎可行:
tp.heightProperty().addListener((obs, oldHeight, newHeight) -> stage.sizeToScene());
关于javafx - 打开和关闭TitledPane时调整场景大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27849258/