[英]JavaFX BorderPane Layout (Stretch Left and Right Pane)
我正在使用 BorderPane 在 JavaFX 中实现布局。
假设 BorderPane 的 maxPrefWidth 设置为 1000。左窗格中有一个按钮,右窗格中有一个按钮。 在中心窗格中,还有另一个大小未知的按钮。
有没有办法告诉左右窗格自动增长(水平)直到中心节点被击中?
BorderPane
通过设计扩展了中间区域您的意图与BorderPane
的设计意图不符。
引用 Javadoc:
顶部和底部的孩子将被调整到他们喜欢的高度并扩展边框窗格的宽度。 左右子节点将被调整为其首选宽度并延长顶部和底部节点之间的长度。 并且中心节点将调整大小以填充中间的可用空间。 任何位置都可以是 null。
这表示:
把中间想象成一个盒子,里面有一个强人向上、向下和向外推。
对于许多业务应用程序来说,这种逻辑通常非常方便。 外部区域通常用于导航、面包屑、菜单栏、工具栏、状态栏等。 内部区域则保存主要的兴趣内容。 鉴于这种用法,只为外部区域分配必要的空间,而将大部分空间分配给内部内容区域是有意义的。
例如,这是一个使用 JavaFX 14 的完整示例应用程序。
在这个示例应用程序的中心区域,我们放置了一个包含单个按钮的HBox
布局。 我们将该布局的背景颜色设置为cornflowerblue
蓝色,以显示中心区域的内容如何扩展以占用所有额外空间。
package work.basil.example;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.time.ZonedDateTime;
/**
* JavaFX App
*/
public class App extends Application
{
@Override
public void start ( Stage stage )
{
// Widgets
ToolBar toolBar = new ToolBar();
Button button = new Button( "Click Me" );
toolBar.getItems().add( button );
Button buttonLeft = new Button( "Left" );
Button buttonRight = new Button( "Right" );
HBox appContent = new HBox( new Button( "Bonjour le monde" ) );
appContent.setStyle( "-fx-background-color: cornflowerblue;" );
HBox statusBar = new HBox( new Label( "Status goes here. Now: " + ZonedDateTime.now() ) );
// Arrange
BorderPane borderPane = new BorderPane();
borderPane.setTop( toolBar );
borderPane.setLeft( buttonLeft );
borderPane.setCenter( appContent );
borderPane.setRight( buttonRight );
borderPane.setBottom( statusBar );
var scene = new Scene( borderPane , 1000 , 1000 );
stage.setScene( scene );
stage.show();
}
public static void main ( String[] args )
{
launch();
}
}
正如对问题的评论,您应该根据您的意图使用不同的布局管理器。
您也许可以使用HBox
。 为了最大限度地控制,您需要花一些时间来掌握GridPane
。
GridPane
你的问题并不完全清楚。 如果您想要的是中心内容的固定宽度为 500 像素,而左侧和右侧是灵活的,按比例分配任何额外空间,然后使用GridPane
将左侧的ColumnConstraints
设置为Priority.SOMETIMES
或Priority.ALWAYS
和正确的细胞。
这是一个完整的示例应用程序。
我们在单行GridPane
的每个单元格中放置一个按钮,每个按钮都嵌套在一个彩色HBox
中。 colors 戏剧化了此处显示的大小调整行为。 或者,您可以删除彩色HBox
,而是调用gridPane.setStyle( "-fx-grid-lines-visible: true;" )
来显示每个单元格周围的边框线。
package work.basil.example;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.time.ZonedDateTime;
/**
* JavaFX App
*/
public class App extends Application
{
@Override
public void start ( Stage stage )
{
// Widgets
Button buttonLeft = new Button( "Left" );
HBox left = new HBox( buttonLeft );
left.setStyle( "-fx-background-color: Salmon;" );
Button buttonCenter = new Button( "Center" );
HBox center = new HBox( buttonCenter );
center.setStyle( "-fx-background-color: CornflowerBlue;" );
Button buttonRight = new Button( "Right" );
HBox right = new HBox( buttonRight );
right.setStyle( "-fx-background-color: MediumSeaGreen;" );
// GridPane
GridPane gridPane = new GridPane();
gridPane.addRow( 0 , left , center , right ); // Add these widgets in first row (annoying zero-based counting means index 0 is row 1).
gridPane.setStyle( "-fx-grid-lines-visible: true ;" ); // Add lines to the edges of each cell (row/column) in the grid. Useful for learning and debugging. https://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#gridpane
// Grid constraints
ColumnConstraints column1 = new ColumnConstraints();
column1.setHgrow( Priority.SOMETIMES ); // Extra space alloted to this column.
ColumnConstraints column2 = new ColumnConstraints( 500 ); // Fixed width of 500 pixels.
ColumnConstraints column3 = new ColumnConstraints();
column3.setHgrow( Priority.SOMETIMES );// Extra space alloted to this column.
gridPane.getColumnConstraints().addAll( column1 , column2 , column3 ); // first column gets any extra width
// Render
var scene = new Scene( gridPane , 1000 , 150 );
stage.setScene( scene );
stage.setTitle( "Example of JavaFX GridPane, by Basil Bourque" );
stage.show();
}
public static void main ( String[] args )
{
launch();
}
}
正在运行的应用程序的屏幕截图。 请注意左右如何分别获得 250 像素的剩余空间。 我们将 window ( Stage
) 设置为 1,000 像素,并将中心部分的宽度固定为 500 像素。 剩下 500 个像素要分配。 左右单元格都设置为相同的优先级,因此它们在它们之间平均分配空间:每个 500/2 = 250 像素。
如果用户将 window 的宽度缩小到 600 像素,则左右单元格将分别为 50 像素:600 - 500 = 100、100/2 = 50 像素。
一般来说,我建议发布代码而不是试图描述它。 例如,这个mre可以代表您的问题:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Button leftBtn = new Button("Left");
Button centerBtn = new Button("Center");
centerBtn.widthProperty().addListener((obs, oldValue,newValue)-> {
//change the logic as needed
leftBtn.setPrefWidth(newValue.doubleValue() >= 600 ? 200 : 250);
});
centerBtn.setPrefSize(600, 0);
Button rightBtn = new Button("Right");
Pane root = new BorderPane(centerBtn, null, rightBtn, null, leftBtn);
root.setPrefSize(1000, 150);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这也使帮助变得更加容易。 例如,评论中提出的带有HBox
的解决方案只需要对 mre 进行细微更改(将操作框添加到中心按钮以更改其宽度):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class Main extends Application {
private static final double MIN = 300, MAX = 700, DELTA = 100;
private Button centerBtn;
@Override
public void start(Stage stage) throws Exception {
Button leftBtn = new Button("Left");
HBox.setHgrow(leftBtn, Priority.NEVER);
centerBtn = new Button("Click to change width");
HBox.setHgrow(leftBtn, Priority.NEVER);
centerBtn.widthProperty().addListener((obs, oldValue,newValue)-> {
//change the logic as needed
leftBtn.setPrefWidth(newValue.doubleValue() >= 600 ? 200 : 250);
});
centerBtn.setPrefSize(600, 0);
centerBtn.setOnAction(e-> changeCenterBtnWidth());
Button rightBtn = new Button("Right");
HBox.setHgrow(rightBtn, Priority.ALWAYS);
Pane root = new HBox(leftBtn,centerBtn,rightBtn);
root.setPrefSize(1000, 150);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
private void changeCenterBtnWidth() {
double newWidth = centerBtn.getWidth() + DELTA;
newWidth = newWidth < MAX ? newWidth : MIN;
centerBtn.setPrefWidth(newWidth);
}
public static void main(String[] args) {
launch(args);
}
}
演示基于GridPane
的解决方案只需要一些小改动:
public void start(Stage stage) throws Exception {
Button leftBtn = new Button("Left");
centerBtn = new Button("Click to change width");
centerBtn.widthProperty().addListener((obs, oldValue,newValue)-> {
//change the logic as needed
leftBtn.setPrefWidth(newValue.doubleValue() >= 600 ? 200 : 250);
});
centerBtn.setPrefSize(600, 0);
centerBtn.setOnAction(e-> changeCenterBtnWidth());
Button rightBtn = new Button("Right");
GridPane root = new GridPane();
root.add(leftBtn,0, 0);
root.add(centerBtn,1, 0);
root.add(rightBtn,2, 0);
root.setPrefSize(1000, 150);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.