簡體   English   中英

在JavaFX中,如何將標簽與VBox布局中的CheckBoxes對齊?

[英]In JavaFX, how do you line up the labels with the CheckBoxes in a VBox layout?

我已經在JavaFX中創建了一個TableView ,它動態地將包含CheckBox列表的VBox加載到行中。 如下圖所示:

在此處輸入圖片說明

如您在圖中所看到的,復選框旁邊的標簽與復選框本身不對齊。

我使用以下內容動態創建VBoxes:

private VBox getVBox(Map<Integer, String> item, Set<Integer> completed, String id) {
    VBox box = new VBox();
    box.setSpacing(0);
    box.setPadding(new Insets(3,3,3,3));

    for(Integer i : item.keySet()) {
        CheckBox checkbox = new CheckBox(item.get(i));
        checkbox.setId("item"+id+"-" + i.intValue());
        if(completed.contains(i)) {
            checkbox.setSelected(true);
        }
        checkbox.setPrefHeight(21);
        box.getChildren().add(checkbox);
    }

    return box;
}

即使我刪除了setSpacing設置和setPadding設置以及setPrefHeight設置,問題仍然保持不變。

我應用於此表的唯一CSS並非默認情況如下:

.table-view{
    -fx-background-color: transparent;
}

.table-view:focused{
    -fx-background-color: transparent;
}

.table-row-cell:odd{
    -fx-background-color: #E0E0E0;
    -fx-background-insets: 0, 0 0 1 0;
    -fx-padding: 0.0em;
}

.table-row-cell:even{
    -fx-background-color: #BBD9E0;
    -fx-background-insets: 0, 0 0 1 0;
    -fx-padding: 0.0em;
}

我的問題:如何使復選框標簽與復選框垂直對齊? VBox不能做到這一點嗎?

感謝您的幫助。

我建議您使用ScenicView工具並檢查應設置哪些元素以獲得垂直對齊

下載它作為庫添加,並在下面的行中放入代碼:

  ScenicView.show(current scene instance);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM