You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX中ImageView节点Y轴位置异常问题求助

JavaFX中ImageView节点Y轴位置异常问题求助

我在JavaFX的Scene里摆放ImageView时遇到了个奇怪的问题。

我想要三张同样尺寸(200x200)的图片在同一高度并排显示,所以给每张图片都设置了相同的setTranslateY()值,但不知道为啥它们显示在不同高度——每一张都比前一张低200像素。

下面是我的代码:

public class Main extends Application {

    @Override
    public void start(Stage myStage) throws IOException {
        myStage.setTitle("title");
        myStage.setScene(getScene());
        stage = myStage;
        myStage.show();
    }

    public static void main(String[] args) {
        launch();
    }

    private Scene getScene() {
        List<Node> nodes = new ArrayList<>();
        Image i1 = loadFromPath("... some path");
        Image i2 = loadFromPath("... some other path");
        Image i3 = loadFromPath("... again some other path");
        ImageView v1 = new ImageView(i1);
        ImageView v2 = new ImageView(i2);
        ImageView v3 = new ImageView(i3);
        v1.setTranslateX(100);
        v1.setTranslateY(100);
        v2.setTranslateX(320);
        v2.setTranslateY(100);
        v3.setTranslateX(540);
        v3.setTranslateY(100);
        nodes.add(v1);
        nodes.add(v2);
        nodes.add(v3);
        VBox box = new VBox(nodes.toArray(new Node[0]));
        box.setMinWidth(800);
        box.setMaxWidth(800);
        box.setMinHeight(400);
        box.setMaxHeight(400);

        return new Scene(box);
    }

    private Image loadFromPath(String path) {
        FileInputStream input = null;
        try {
            input = new FileInputStream(path);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return new Image(input);
    }
}

实际显示效果是三张图片垂直堆叠,每张依次向下偏移200像素,完全不是我想要的水平并排效果。

当我换成setLayoutX()和setLayoutY()来设置位置时,效果更糟——图片的位置错乱得更离谱,完全不符合预期。

有没有大佬能帮我解释下这是怎么回事?

备注:内容来源于stack exchange,提问作者user16038501

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:50:26