ImageView无法正确调整大小

时间:2014-06-17 05:08:28

标签: java javafx

我在我的UI中使用Java和JavaFX,我想在一些Label之后放置一个Image。图像应与标签的高度相同。

但是,如果我将fitHeightProperty绑定到标签高度,则图像会正确调整大小,但围绕它的AnchorPane不会。

预期的行为是,AnchorPane也会缩小;与图像一起。

编辑:问题似乎与here相同,其中声明此行为是一个错误。 AnchorPane似乎没有注意到Image的大小变化。此外,我可以确认更新场景使AnchorPane捕捉到所需的大小(我尝试通过更改MouseClick上的标签文本)。

此外,问题可以使用fxml-only(没有java代码)重现。

我创建了short, self contained, correct example

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application
{
    public static void main(String[] args)
    {
        launch(args);
    }



    public void start(Stage stage) throws Exception
    {
        //Setup Window
        stage.setTitle("ImageResizeDemo");
        stage.setWidth(400);
        stage.setHeight(400);

        stage.setScene(new Scene(new Main().getUI()));

        stage.show();
    }


    public Parent getUI()
    {
        AnchorPane main = new AnchorPane();
        AnchorPane inner = new AnchorPane();

        Label label = new Label();
        label.setText("Test");
        inner.getChildren().add(label);

        //Load image and bind its height to the height of the label
        ImageView image = new ImageView(new Image(this.getClass().getResourceAsStream("image.png")));
        image.fitHeightProperty().bind(label.heightProperty());
        inner.getChildren().add(image);

        main.getChildren().add(inner);

        //Add dotted borders around the panes to see the size of them
        addDebugBorder(inner);
        addDebugBorder(main);

        return main;
    }



    public static void addDebugBorder(Region region)
    {
        region.borderProperty().set(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.DOTTED, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
    }
}

要运行此功能,您只需要在该类旁边有一个image.png文件。

我使用了jdk1.8.0和jdk1.8.0_05,问题仍然存在。

编辑:我在JavaFX的Jira跟踪器上创建了一个Bugreport。 RT-37670

0 个答案:

没有答案
相关问题