2014-06-30 56 views
1

我想中心一個跨越GridPane的兩列的標籤。但是,它似乎並不奏效。JavaFX colspan不適用於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> 

application.css

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

我想設置它無論是在CSS或FXML ,我只是在測試。

回答

2

您還沒有提供columnIndexTextButton,您要在中心對齊。

<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" />