2017-04-12 29 views
1

首先,我將解釋我的目標。我想呈現一個表是這樣的:Javafx Tile Pane,設置最大列數

enter image description here

每一個單元格的內容執行時間是確定的,但他的大小固定爲13×13。

所以,我的方法是創建一個瓦片窗格,將列數設置爲13並創建單元格。

pane = new TilePane(); 
pane.setPadding(new Insets(10, 10, 10, 10)); 
pane.setVgap(5); 
pane.setHgap(5); 
pane.setPrefColumns(13); 

這工作:

enter image description here

但是,當我調整窗口大小:

enter image description here

還有更多的13列吧!

我的測試代碼:

package sample; 

import javafx.application.Application; 
import javafx.collections.ObservableList; 
import javafx.geometry.Insets; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.Region; 
import javafx.scene.layout.TilePane; 
import javafx.stage.Stage; 

public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      TilePane pane = new TilePane(); 
      pane.setPadding(new Insets(10, 10, 10, 10)); 
      pane.setVgap(5); 
      pane.setHgap(5); 
      pane.setPrefColumns(13); 
      pane.setMaxWidth(Region.USE_PREF_SIZE); 
      ObservableList<Node> list = pane.getChildren(); 

      for (int i = 0;i < 200;i++){ 
       Label view = new Label(); 
       view.setText(""+(i+1)); 
       list.add(view); 
      } 

      primaryStage.setScene(new Scene(pane,400,400)); 
      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

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

你有什麼想法?

回答

2

將最大寬度設置爲使用首選寬度是正確的方法。在你的例子中,代碼唯一的問題是你已經將瓦片窗格作爲場景的根。根節點將始終根據場景大小設置大小,忽略最小值,最大值和首選大小。 (如果不指定場景的大小,則根據根節點的首選大小確定場景的大小,但根節點仍允許任意增大或縮小。)

因此,如果您只是包裝在另一個窗格中的瓷磚面板,它將按預期工作:

primaryStage.setScene(new Scene(new StackPane(pane))); 

enter image description here

enter image description here

+0

好聽點,但現在有一個問題......這不是reponsible,寬度當你重新建立時,這些單元格不會改變窗戶。 – amchacon

+0

@amchacon也許你可以編輯你的問題來包含你的所有需求? ['TilePane'文檔](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/TilePane.html)說'每個「tile」的大小默認爲所需的大小以包含tilepane的孩子最大的首選寬度和高度......應用程序還可以通過設置'prefTileWidth' /'prefTileHeight'屬性直接控制圖塊的大小......'所以唯一的方法是通過綁定'prefTileWidth'並做一些計算。使用'GridPane'可能會更容易一些。 –