JavaFXGridPane,如何居中节点?(文字/标签)
我正在努力学习Javafx,准备参加一个课程。我在从HTML+ERB模板转换到Javafx框架时遇到了很多困难(我是Rails开发人员) 由于某些原因,我无法在网格窗格中将标签节点居中。这是我的JavaFXGridPane,如何居中节点?(文字/标签),java,javafx,Java,Javafx,我正在努力学习Javafx,准备参加一个课程。我在从HTML+ERB模板转换到Javafx框架时遇到了很多困难(我是Rails开发人员) 由于某些原因,我无法在网格窗格中将标签节点居中。这是我的start方法: @Override public void start(Stage stage) throws Exception { GridPane root = new GridPane(); FlowPane leftbanner = new FlowPa
start
方法:
@Override
public void start(Stage stage) throws Exception {
GridPane root = new GridPane();
FlowPane leftbanner = new FlowPane();
leftbanner.setPrefWidth(200);
String bgStyle = "-fx-background-color: lightblue;"
+ "-fx-background-radius: 0%;"
+ "-fx-background-inset: 5px;";
leftbanner.setStyle(bgStyle);
root.add(leftbanner, 0, 0, 1, 1);
root.add(createGridPane(), 1, 0, 1, 1);
Scene scene = new Scene(root, 700, 500);
stage.setTitle("Recommendify");
stage.setScene(scene);
stage.show();
}
grid.setGridLinesVisible(true);
我正在使用createGridPane
方法构建我的应用程序。以下是该方法的内容,其中包括我尝试将标签居中:
public GridPane createGridPane() {
GridPane grid = new GridPane();
grid.setPadding(new Insets(10));
grid.setHgap(10);
grid.setVgap(10);
Text txt = new Text("Recommendify");
txt.setFont(Font.font("Dialog", FontWeight.BOLD, 12));
GridPane.setHalignment(txt, HPos.CENTER);
grid.add(txt, 0, 0, 1, 1);
grid.add(new Separator(), 0, 1, 3, 1);
return grid;
}
我还尝试了以下发布的解决方案:
我是否需要将此标签节点放入容器中,以使其在GridPane中居中?就像HBox或VBox一样?您的代码实际上没有什么问题。“推荐”文本在其单元格中居中。但是,单元格的宽度不超过文本本身 通过在
createGridPane()方法中打开网格线,可以看到这一点:
@Override
public void start(Stage stage) throws Exception {
GridPane root = new GridPane();
FlowPane leftbanner = new FlowPane();
leftbanner.setPrefWidth(200);
String bgStyle = "-fx-background-color: lightblue;"
+ "-fx-background-radius: 0%;"
+ "-fx-background-inset: 5px;";
leftbanner.setStyle(bgStyle);
root.add(leftbanner, 0, 0, 1, 1);
root.add(createGridPane(), 1, 0, 1, 1);
Scene scene = new Scene(root, 700, 500);
stage.setTitle("Recommendify");
stage.setScene(scene);
stage.show();
}
grid.setGridLinesVisible(true);
要测试对齐,可以将列约束添加到网格窗格中
:
grid.getColumnConstraints().add(new ColumnConstraints(150));
上述语句将第一列的首选宽度设置为150
。以下是新的结果:
如您所见,文本
节点正确居中
编辑:请记住,如果希望文本
居中于添加到第二行的分隔符
上,则需要
它也跨越3根柱子
实际上,您的代码没有问题。“推荐”文本在其单元格中居中。但是,单元格的宽度不超过文本本身
通过在createGridPane()方法中打开网格线,可以看到这一点:
@Override
public void start(Stage stage) throws Exception {
GridPane root = new GridPane();
FlowPane leftbanner = new FlowPane();
leftbanner.setPrefWidth(200);
String bgStyle = "-fx-background-color: lightblue;"
+ "-fx-background-radius: 0%;"
+ "-fx-background-inset: 5px;";
leftbanner.setStyle(bgStyle);
root.add(leftbanner, 0, 0, 1, 1);
root.add(createGridPane(), 1, 0, 1, 1);
Scene scene = new Scene(root, 700, 500);
stage.setTitle("Recommendify");
stage.setScene(scene);
stage.show();
}
grid.setGridLinesVisible(true);
要测试对齐,可以将列约束添加到网格窗格中
:
grid.getColumnConstraints().add(new ColumnConstraints(150));
上述语句将第一列的首选宽度设置为150
。以下是新的结果:
如您所见,文本
节点正确居中
编辑:请记住,如果希望文本
居中于添加到第二行的分隔符
上,则需要
它也跨越3根柱子
顺便提一下,您的问题提到了标签
节点,但您的代码使用的是文本
节点。无论哪种方式,结果都是一样的,但是为了避免混淆,您可能希望回答这个问题。您正在使用GridPane
好的。但是,您知道网格中有多少列和/行吗(网格通常指行和列)。所以,计算出有多少行/列,然后是标签/文本的位置。顺便说一句,标签
和文本
有不同的用途。标签用于生成静态文本,文本
用于创建在应用程序过程中可以更改的文本,如状态栏文本。要对齐网格窗格单元格中的标签,请使用:GridPane.setHalignment(labelToBeCentered,javafx.geometry.HPos.CENTER)代码>。此外,看起来您正在网格窗格中创建网格窗格-(1)GridPane root=new GridPane()在start
中的code>,以及(2)在createGridPane()
中将其添加到根目录中:root.add(createGridPane(),1,0,1,1)代码>。以下是使用各种布局的示例:。@prasad_u2;-我相信他们实际上打算使用两个网格,一个在另一个内。顺便提一下,您的问题提到了标签
节点,但您的代码使用的是文本
节点。无论哪种方式,结果都是一样的,但是为了避免混淆,您可能希望回答这个问题。您正在使用GridPane
好的。但是,您知道网格中有多少列和/行吗(网格通常指行和列)。所以,计算出有多少行/列,然后是标签/文本的位置。顺便说一句,标签
和文本
有不同的用途。标签用于生成静态文本,文本
用于创建在应用程序过程中可以更改的文本,如状态栏文本。要对齐网格窗格单元格中的标签,请使用:GridPane.setHalignment(labelToBeCentered,javafx.geometry.HPos.CENTER)代码>。此外,看起来您正在网格窗格中创建网格窗格-(1)GridPane root=new GridPane()在start
中的code>,以及(2)在createGridPane()
中将其添加到根目录中:root.add(createGridPane(),1,0,1,1)代码>。以下是使用各种布局的示例:。@prasad_u2;-我相信他们实际上打算有两个网格,一个在另一个内。