2015-02-05 43 views
3

設置某些特定的跨度配置時,GridPane自動調整存在一個問題。JavaFX 8 - GridPane自動調整問題

以下配置工程,我想到:

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.Border; 
import javafx.scene.layout.BorderStroke; 
import javafx.scene.layout.BorderStrokeStyle; 
import javafx.scene.layout.BorderWidths; 
import javafx.scene.layout.CornerRadii; 
import javafx.scene.layout.GridPane; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class GridPaneWidthIssue extends Application { 

    public static void main(String... arguments) { 
     launch(arguments); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     GridPane gridPane = new GridPane(); 

     BorderStroke borderStroke = new BorderStroke(Color.RED, 
       BorderStrokeStyle.SOLID, CornerRadii.EMPTY, 
       BorderWidths.DEFAULT); 

     Border border = new Border(borderStroke); 
     gridPane.setBorder(border); 
     gridPane.setGridLinesVisible(true); 
     gridPane.setHgap(2); 
     gridPane.setVgap(2); 
     gridPane.setPadding(new Insets(10)); 

     Label column0 = new Label("Column 0"); 
     Label column1 = new Label("Column 1"); 
     Label column2 = new Label("Column 2"); 
     Label column01 = new Label("Span column 0 and 1"); 
     Label column012 = new Label("Span column 0, 1 and 2"); 

     gridPane.add(column0, 0, 0); 
     gridPane.add(column1, 1, 0); 
     gridPane.add(column2, 2, 0); 
     gridPane.add(column01, 0, 1, 2, 1); 
     gridPane.add(column012, 0, 2, 3, 1); 

     Scene scene = new Scene(gridPane); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
} 

我不能發佈圖片還沒有證明這一點,但我可以確認以下斷言:

** gridpane.width = column0.width + column1.width + column2.width

當我行0中刪除標籤,我得到了下面的代碼:

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.Border; 
import javafx.scene.layout.BorderStroke; 
import javafx.scene.layout.BorderStrokeStyle; 
import javafx.scene.layout.BorderWidths; 
import javafx.scene.layout.CornerRadii; 
import javafx.scene.layout.GridPane; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class GridPaneWidthIssue extends Application { 

    public static void main(String... arguments) { 
     launch(arguments); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     GridPane gridPane = new GridPane(); 

     BorderStroke borderStroke = new BorderStroke(Color.RED, 
       BorderStrokeStyle.SOLID, CornerRadii.EMPTY, 
       BorderWidths.DEFAULT); 

     Border border = new Border(borderStroke); 
     gridPane.setBorder(border); 
     gridPane.setGridLinesVisible(true); 
     gridPane.setHgap(2); 
     gridPane.setVgap(2); 
     gridPane.setPadding(new Insets(10)); 

     Label column0 = new Label("Column 0"); 
     Label column1 = new Label("Column 1"); 
     Label column2 = new Label("Column 2"); 
     Label column01 = new Label("Span column 0 and 1"); 
     Label column012 = new Label("Span column 0, 1 and 2"); 

     // gridPane.add(column0, 0, 0); 
     // gridPane.add(column1, 1, 0); 
     // gridPane.add(column2, 2, 0); 
     gridPane.add(column01, 0, 1, 2, 1); 
     gridPane.add(column012, 0, 2, 3, 1); 

     Scene scene = new Scene(gridPane); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
} 

而結果是不正確的:

gridpane.width> column0.width + column1.width + column2.width

我沒有驗證,但我猜是:

gridpane.width = column0.width + column1.width + column0.width + column1.width + column2.width

JavaFX問題或我誤解了Gridpane調整策略?

案例1:OK

enter image description here

案例2:KO(在GridPane空白空間)

enter image description here

+1

你能澄清你的意思嗎?我會將你的僞代碼'column0.width'解釋爲'Label column0;'等的寬度。顯然這在你的第二個例子中是沒有意義的,因爲'column0.width'將是零(類似於其他),因爲這些標籤不顯示。 (不管怎樣,第一個例子中都不是這樣。)那麼你是指實際列的寬度?如果是這樣,因爲在第一個例子中它必須包含'hgap' ......但在第二個例子中它變得平凡無奇。所以你是什麼意思? (我會upvote,所以你可以張貼圖像。) – 2015-02-05 18:58:41

+0

你是對的,通過專欄0我不是指標籤,而是列本身,我沒有在我的僞代碼中包含hgaps。我添加了空白來製作截圖明確。我會盡快上傳這兩種情況的截圖,你會清楚地看到問題:在第二種情況下,column2右邊有一個空的空間。這個空間在gridpane裏面。在這個特定的例子中,當column0或column1增長時,這個空間會增長。這就是爲什麼我猜空白空間寬度=列0的寬度+列1的。這只是一個猜測,但無論如何,空蕩蕩的空間令人討厭。 – Jonathan 2015-02-06 07:26:39

回答

0

你看到額外的空間在GridPane因爲權當添加column01標籤(並且您不再有三列但只有一列)時,添加column012標籤和一個2的colspan時,您的殖民地爲3。記住GridPane是一個表格,你需要保持它的列平衡,並跨越它的列/行限制。在你的情況下,空白空間只是GridPane內部任何標籤範圍之外的第三列。

更改此:

gridPane.add(column012, 0, 2, 3, 1); 

要這樣:

gridPane.add(column012, 0, 2, 2, 1); 

你會看到

Image without extra columns

理想的情況下,如果你正在計劃只有一列,你應該刪除該colspan和rowspan,只是離開這個:

gridPane.add(column01, 0, 1); 
gridPane.add(column012, 0, 2); 

如果你繼續列平衡你的GridPane的寬度是可以預測的,因爲它會被計算爲:插圖+缺口+每列寬度

你可能也想看看的ColumnConstraints類根據自己的喜好自定義每個列的行爲。

希望這會有所幫助!