繁体   English   中英

JavaFX AnchorPane无法正常工作

[英]JavaFX AnchorPane is not working

我正在尝试使用AnchorPane锚定两个矩形,尽管出于某些原因这似乎不起作用:

正常画面

在此处输入图片说明

屏幕最大化

在此处输入图片说明 2这是我的代码:

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

    @Override
    public void start(Stage MainStage) throws Exception {

        // Setup window
        MainStage.setTitle("Minx IDE - Version 1.0.0");
        Group Root = new Group();
        Scene MainScene = new Scene(Root, 1000, 600, Color.rgb(44, 62, 80));
        MainStage.setScene(MainScene);
        MainStage.show();

        // Setup window components
        // Info box
        Rectangle InfoBox = RectangleBuilder.create()
                .x(700)
                .y(0)
                .width(300)
                .height(600)
                .fill(Color.rgb(149, 165, 166))
                .build();

        // Save bar
        Rectangle SaveBar = RectangleBuilder.create()
                .x(0)
                .y(575)
                .width(700)
                .height(25)
                .fill(Color.rgb(52, 152, 219))
                .build();

        AnchorPane Anchor = new AnchorPane(InfoBox, SaveBar);
        AnchorPane.setBottomAnchor(SaveBar, 0.0);
        AnchorPane.setLeftAnchor(SaveBar, 0.0);
        AnchorPane.setTopAnchor(InfoBox, 0.0);
        AnchorPane.setRightAnchor(InfoBox, 0.0);

        // Add components to root and anchor
        Root.getChildren().addAll(InfoBox, SaveBar);

    }

请告诉我我在做什么错。

考虑到无论屏幕大小如何,都需要将矩形锚定在它们各自的位置,因此需要删除“组”作为“场景”的根并将“ AnchorPane”作为场景的根。

布局不会自动调整大小,并保留其子级的大小。

创建一个以anchorPane为根的场景将解决您的问题,因为它会根据空间的可用性自行调整大小。

Scene mainScene = new Scene(anchorPane, 1000, 600, Color.WHITE);

注意 :

  1. RectangleBuilder在JavaFX 8中已弃用,因此请避免使用它。
  2. 命名变量时,请尝试使用CamelCase 它是Java中遵循的默认命名约定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM