我正在尝试向hbox动态添加一些窗格。窗格具有自己的类。每个窗格都有一个按钮,一个图像和一个标签。我创建了一个for循环,该循环包含5种产品,然后将其添加到面板中,依此类推。我遇到的一个问题是我两次调用了@FXML private void initialize()
方法。这将5种产品添加到5个窗格中,没有输出它们,然后再次运行,又添加了5种-现在增加了10种-并将这10种干扰到hbox中。我删除了多余的initialize()
,但现在什么也没打印。
商店的首页
public class StoreHomePageController {
private MainController mainController;
private HBox scrollableHBox = new HBox();
private String fileName;
private Product waterProduct = new Product("", 999.00, 1,
"/0.png");
private Product pollutedWater = new Product("", 4.99, 25,
"/1.png");
private Product rawStreamWater= new Product("", 24.99, 50, "/2.png");
private Product rawStreamWater2= new Product("", 24.99, 50, "/3.png");
private Product rawStreamWater1= new Product("", 24.99, 50, "/4.png");
private List<Product> forSaleHBoxProducts = new ArrayList<>();]
public StoreHomePageController(MainController mainController) {
this.mainController = mainController;
this.fileName = waterProduct.getFileName();
HBox hb = new HBox();
borderPane.setTop(hb);
hb.getStyleClass().add("hb");
addToForSalesPane();
}
public void addToForSalesPane(){
forSaleHBoxProducts.add(waterProduct);
forSaleHBoxProducts.add(rawStreamWater);
forSaleHBoxProducts.add(rawStreamWater1);
forSaleHBoxProducts.add(pollutedWater);
forSaleHBoxProducts.add(rawStreamWater2);
for (Product p: forSaleHBoxProducts){
scrollableHBox.getChildren().add(new ProductPaneHomePage(p));
}
}
@FXML
private void initialize(){
// addToForSalesPane();
// putting this here causes the method to run twice. it adds 5
products, doesn't add them
// then adds 5 more and adds those 10.
salesScrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
salesScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
}
}
我要添加的窗格
public class ProductPaneHomePage extends Pane {
private ImageView quickLookImageView = new ImageView();
private Label quickLookLabel = new Label();
private Button quickLookBtn = new Button("Buy Now!");
private Product product;
private File file;
private Image image;
private String fileName;
public ProductPaneHomePage(Product product){
this.product = product;
this.fileName = this.product.getFileName();
file = new File(this.fileName);
setPrefHeight(218);
setPrefWidth(200);
getChildren().add(quickLookImageView);
setQuickLookImageViewDimensions();
getChildren().add(quickLookLabel);
setQuickLookLabelDimensions();
getChildren().add(quickLookBtn);
setText();
// initialize();
}
// private void initialize(){
// setQuickLookLabelDimensions();
// setQuickLookImageViewDimensions();
// setText();
// }
private void setQuickLookImageViewDimensions(){
quickLookImageView.setFitHeight(196);
quickLookImageView.setFitWidth(200);
quickLookImageView.setLayoutX(0);
quickLookImageView.setLayoutY(14);
quickLookImageView.setImage(getImage());
}
private void setQuickLookLabelDimensions(){
quickLookLabel.setAlignment(Pos.CENTER);
quickLookLabel.setLayoutX(55);
quickLookLabel.setLayoutY(220);
quickLookLabel.setPrefHeight(17);
quickLookLabel.setPrefWidth(10);
quickLookLabel.setTextAlignment(TextAlignment.CENTER);
}
public Image getImage(){age = this.product.getImage();
return image;
}
public void setText(){
quickLookLabel.setText(this.product.getName());
}
}
产品类别
public class Product {
private String name;
private String companyName;
private double price;
private int quantity;
private String description;
private ImageView imageView;
private String fileName;
private String info;
private Image image;
public Product(String name, double price, int quantity, String fileName) {
this.name = name;
this.companyName = companyName;
this.price = price;
this.description = description;
this.imageView = imageView;
this.quantity = quantity;
this.fileName = fileName;
this.info = info;
this.image = new Image(this.fileName);
}
public String getName(){
return this.name;
}
public String getCompanyName(){
return this.companyName;
}
public double getPrice(){
return this.price;
}
public Image getImage(){
return image;
}
public ImageView getImageView(){ return this.imageView; }
public String getFileName(){return this.fileName;}
}
因此,理想情况下,五个窗格将输出到hbox中。目前还没有。但是,如果我添加了一个多余的initialize()
方法,我可以卡住10个窗格。谢谢!