【发布时间】:2019-02-25 01:42:20
【问题描述】:
我正在开发一个 javafx 应用程序。其中一部分包括使用网格元素的滚动窗格将 List 呈现到 Gui 中。我正在正确获取我需要的值,但我不知道如何将每个网格窗格单元格居中。
// clear existing content if it exists
if(content.getChildren()!=null)
{
content.getChildren().clear();
}
// get Elements to display
OfferService os = new OfferService();
List<Offer> myList = os.afficheroffre();
UserService us = new UserService();
GridPane Container = new GridPane(); // main container for all data specific to an offer
Container.setAlignment(Pos.CENTER);
// Scroll pane to display all the found contact requests
ScrollPane scrollPane = new ScrollPane(Container);
scrollPane.setPrefSize(900, 630);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
AnchorPane.setTopAnchor(scrollPane, 0.);
Container.setPrefWidth(900);
Container.setPrefHeight(630);
content.setRightAnchor(scrollPane, 0.);
content.setBottomAnchor(scrollPane, 0.);
content.setLeftAnchor(scrollPane, 0.);
Container.setPadding(new Insets(30,0,0,30));
// iterate through all offers and create an offer element
int i = 0;
int j = 0;
for (Offer o : myList)
{
final User u = us.getById(o.getIdOfferuser());
//HBox : single with spacing
HBox Hb = new HBox();
//VBox : single
VBox Vb = new VBox();
ImageView img = new ImageView(new Image("/Uploads/" + o.getImageOffer()));
Label name = new Label(o.getNameOffer());
ImageView avatar = new ImageView(new Image("/Uploads/" + u.getAvatar()));
Button profile = new Button(u.getFirstName());
Label price = new Label(String.valueOf(o.getPriceOffer()));
Rating rating = new Rating();
HBox hbb = new HBox();
Button reserve = new Button("Reserve");
Button details = new Button("more details");
hbb.getChildren().add(reserve);
hbb.getChildren().add(details);
Vb.getChildren().add(name);
Vb.getChildren().add(avatar);
Vb.getChildren().add(profile);
Vb.getChildren().add(price);
Vb.getChildren().add(rating);
Vb.getChildren().add(hbb);
Hb.getChildren().add(Vb);
// Add all the service elements to the services container
Container.add(img,i,j);
Container.add(Hb, i, j);
i++;
if(i>2)
{
i = 0;
j++;
}
}
基本上我有这个显示,我希望元素在 Hb 变量(HBox)内居中。我试过 Container.setAlignment(Pos.CENTER);但没有结果。任何帮助都非常感谢
【问题讨论】: