【问题标题】:JavaFX colspan not working for Text in GridPaneJavaFX colspan 不适用于 GridPane 中的文本
【发布时间】:2014-08-20 14:33:51
【问题描述】:

我正在尝试使跨越 GridPane 两列的标签居中。但是,它似乎不起作用。

应用类

public class GUIMainFXML extends Application {
@Override
public void start(Stage primaryStage) {
    try {

        GridPane p = (GridPane) FXMLLoader.load(getClass().getResource(
                "FirstGUIexample.fxml"));

        Scene scene = new Scene(p);

        scene.getStylesheets().add(
                getClass().getResource("application.css").toExternalForm());

        primaryStage.setTitle("Welcome (FXML CODE)");
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

fxml 文件:

<GridPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
hgap="5" vgap="5">
<padding>
    <Insets top="10" right="10" bottom="10" left="10" />
</padding>
<children>
    <Text text="Welcome" GridPane.columnSpan="2" GridPane.halignment="CENTER" 
        GridPane.rowIndex="0" />
    <Button text="Press me" GridPane.halignment="CENTER"
        GridPane.rowIndex="1" GridPane.columnSpan="2" />

    <CheckBox text="Don't touch me" GridPane.rowIndex="2"
        GridPane.halignment="CENTER" GridPane.columnSpan="2" />

    <Label text="Type me: " GridPane.halignment="CENTER"
        GridPane.columnIndex="1" GridPane.rowIndex="3" />

    <TextField GridPane.rowIndex="3" GridPane.columnIndex="2"
        GridPane.halignment="CENTER" />
</children>
</GridPane>

应用程序.css

Text {
    -fx-font-size: 15pt;
    -fx-font-family: Tahoma;
    -fx-font-weight: bold;
}

我想在css或fxml中设置它,我只是在测试。

【问题讨论】:

    标签: javafx


    【解决方案1】:

    您尚未为要居中对齐的TextButton 提供columnIndex

    <Text text="Welcome" GridPane.columnSpan="2" GridPane.halignment="CENTER" 
        GridPane.columnIndex="1" GridPane.rowIndex="0" id="argh" />
    <Button text="Press me" GridPane.halignment="CENTER" GridPane.columnIndex="1"
        GridPane.rowIndex="1" GridPane.columnSpan="2"  />
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 2010-11-19
      • 2017-02-11
      • 2016-08-25
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多