【问题标题】:Why can't I display the label below the image (javafx)?为什么我不能在图像下方显示标签(javafx)?
【发布时间】:2019-05-06 21:50:55
【问题描述】:

所以我有一个 StackPane。它的高度设置为 300。堆栈窗格内是一个包含图像和标签的图像视图。我将图像高度设置为 250 并将对齐方式设置为顶部中心。我将标签对齐设置为窗格的底部中心。然而,每次我运行代码时,图像都会被推到框架的底部,而标签文本位于框架的顶部,因此难以阅读。如果堆栈窗格的高度为 500,图像为 100 Pos.TOP-CENTER,标签为基线中心,则文本不会像应有的那样显示在图像下方。相反,它显示在图像的底部。有人能告诉我为什么会这样吗?

public class VBoxProductPane extends StackPane {

    private MainController mainController;
    private String name;
    private Image image;
    private String fileName;
    private double price;
    private int quantity;
    private Button button = new Button();
    private Product product;

    public VBoxProductPane(Product product){

        setPrefSize(250, 275);
        this.name = product.getName();
        this.price = product.getPrice();
        this.quantity = product.getQuantity();
        this.fileName = product.getFileName();
        this.product = new Product(this.name, this.price, this.quantity, this.fileName);
        setImage();
        setButton();
        setLabel();
        setOnMouseEntered(e -> {
                    button.setVisible(true);
                });
        setOnMouseExited(e -> {
            button.setVisible(false);
        });
    }
    private void setButton(){
        button.setText("Explore");
        getChildren().add(button);
        button.setVisible(false);
        button.setOnAction(e -> {
            button.setVisible(true);
        });
        button.setOnAction(e -> {
            try {
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(getClass().getResource("/ProductLayout.fxml"));
                Stage secondaryStage = new Stage();
                loader.setController(new ProductPage(mainController, product));
                secondaryStage.setTitle(product.getName());
                secondaryStage.setHeight(450);
                secondaryStage.setWidth(600);

                Scene scene = new Scene(loader.load());
                secondaryStage.setScene(scene);
                secondaryStage.show();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
    }
    private void setLabel(){
        Label label = new Label(getLabelText());
        setAlignment(label, Pos.BOTTOM_CENTER);
// this does nothing
        label.setLayoutY(250);
        label.setWrapText(true);
        label.setTextAlignment(TextAlignment.CENTER);
        getChildren().add(label);
        setAlignment(label, Pos.BOTTOM_CENTER);
    }
    private String getLabelText(){
        if (this.product.getName() == null){
            System.out.println("name is null");
        }
        return this.product.getName();
    }
    private Image getImage(){
        Image image = this.product.getImage();
        return image;
    }
    private void setImage() {
        ImageView imageViews = new ImageView();
        imageViews.setImage(this.product.getImage());
        setAlignment(imageViews, Pos.TOP_CENTER);
// this does nothing
        imageViews.setFitHeight(150);
// does not matter what this height is set to
// image is always displayed at the bottom with text over top
        imageViews.setFitWidth(250);
        imageViews.setY(0);
        getChildren().add(imageViews);
    }
}

【问题讨论】:

    标签: java javafx imageview label


    【解决方案1】:

    ImageViewNode 的一个非常简单的类型。它不可调整大小(它没有最小/最大/首选值)。这使得它(不幸地)在布局中使用起来非常尴尬。

    例如,StackPane 无法正确决定如何处理ImageView(因为它没有首选大小,甚至没有最大大小),只能为其分配尽可能多的空间。

    你可以做些什么来解决这个问题:

    1) 将ImageView 包装在一个容器中并设置其尺寸(将最大尺寸设置为与适合尺寸相同应该可以)。

    2) 使用VBoxBorderPane,这样您就可以将Label 正确放置在底部。

    3) 使用Label 中的setGraphicImage 直接与Label 控件集成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 2013-09-20
      • 1970-01-01
      • 2015-05-04
      • 2021-01-31
      • 1970-01-01
      相关资源
      最近更新 更多