如何使组件填充 JavaFX 上的拆分窗格?
How to make a component fill a splitpane on JavaFX?
我想在 JavaFX 拆分窗格的一侧添加一个组件。如何让它填满拆分面板的侧面板? hgrow 和 vgrow 似乎在拆分窗格中不可用。
事实上,我正在尝试在拆分窗格中添加带标题的窗格。这是代码:
public void start(Stage stage) throws Exception {
TitledPane titledPane1 = new TitledPane("Panel 1", new Group());
TitledPane titledPane2 = new TitledPane("Panel 2", new Group());
SplitPane rootPane = new SplitPane();
rootPane.getItems().add(titledPane1);
rootPane.getItems().add(titledPane2);
Scene scene = new Scene(rootPane);
stage.setWidth(400);
stage.setHeight(300);
stage.setScene(scene);
stage.show();
}
你应该使用 Pane instead of a Group.
Group 上的 javadoc 说,
A Group will take on the collective bounds of its children and is not directly resizable.
编辑(根据用户评论)
这里的问题不在于 Pane,而在于 TitledPane
,因为 TitledPane 扩展了 Labeled
您可以使用
将 titledPane 的高度与 splitpane 的高度绑定
titledPane1.prefHeightProperty().bind(rootPane.heightProperty());
一个带有 TiltledPane
高度绑定的工作示例,另一个带有 Pane
添加到 SplitPane
public class StoreNumbers extends Application {
public void start(Stage stage) throws Exception {
Pane pane1 = new Pane();
Pane pane2 = new Pane();
TitledPane titledPane1 = new TitledPane("Panel 1", pane1);
TitledPane titledPane2 = new TitledPane("Panel 2", pane2);
SplitPane rootPane = new SplitPane();
rootPane.getItems().add(titledPane1);
rootPane.getItems().add(pane2);
Scene scene = new Scene(rootPane);
stage.setWidth(400);
stage.setHeight(300);
stage.setScene(scene);
stage.show();
pane1.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));
pane2.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));
titledPane1.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.DASHED, null, null)));
titledPane2.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.DASHED, null, null)));
titledPane1.prefHeightProperty().bind(rootPane.heightProperty());
}
public static void main(String[] args) {
launch(args);
}
}
titledPane1.setMaxHeight(Double.MAX_VALUE);
也解决了我遇到的问题
我想在 JavaFX 拆分窗格的一侧添加一个组件。如何让它填满拆分面板的侧面板? hgrow 和 vgrow 似乎在拆分窗格中不可用。
事实上,我正在尝试在拆分窗格中添加带标题的窗格。这是代码:
public void start(Stage stage) throws Exception {
TitledPane titledPane1 = new TitledPane("Panel 1", new Group());
TitledPane titledPane2 = new TitledPane("Panel 2", new Group());
SplitPane rootPane = new SplitPane();
rootPane.getItems().add(titledPane1);
rootPane.getItems().add(titledPane2);
Scene scene = new Scene(rootPane);
stage.setWidth(400);
stage.setHeight(300);
stage.setScene(scene);
stage.show();
}
你应该使用 Pane instead of a Group.
Group 上的 javadoc 说,
A Group will take on the collective bounds of its children and is not directly resizable.
编辑(根据用户评论)
这里的问题不在于 Pane,而在于 TitledPane
,因为 TitledPane 扩展了 Labeled
您可以使用
将 titledPane 的高度与 splitpane 的高度绑定titledPane1.prefHeightProperty().bind(rootPane.heightProperty());
一个带有 TiltledPane
高度绑定的工作示例,另一个带有 Pane
添加到 SplitPane
public class StoreNumbers extends Application {
public void start(Stage stage) throws Exception {
Pane pane1 = new Pane();
Pane pane2 = new Pane();
TitledPane titledPane1 = new TitledPane("Panel 1", pane1);
TitledPane titledPane2 = new TitledPane("Panel 2", pane2);
SplitPane rootPane = new SplitPane();
rootPane.getItems().add(titledPane1);
rootPane.getItems().add(pane2);
Scene scene = new Scene(rootPane);
stage.setWidth(400);
stage.setHeight(300);
stage.setScene(scene);
stage.show();
pane1.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));
pane2.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));
titledPane1.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.DASHED, null, null)));
titledPane2.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.DASHED, null, null)));
titledPane1.prefHeightProperty().bind(rootPane.heightProperty());
}
public static void main(String[] args) {
launch(args);
}
}
titledPane1.setMaxHeight(Double.MAX_VALUE);
也解决了我遇到的问题