首页 > 解决方案 > 在 AnchorPane JavaFX 中的窗格中缩放节点

问题描述

我在窗格中有 2 个节点(一个文本,另一个是圆圈),并且该窗格位于 AnchorPane 内。我这样做的原因是我想使用自动缩放属性,这是调整 AnchorPane 大小的结果来调整节点的大小(如拉伸和收缩)。

我还有一个测试背景,看看我是否也可以缩放背景。

我的问题是,当我调整窗口大小时背景会缩放,而节点不会。

这是我的代码:

package javacode;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

    public boolean debug = false;

    Pane stack;

    @Override
    public void start(Stage stage) {

        // Check for updates
        /*
        System.out.println("Checking for update");
        boolean newUpdate = Updater.updateAvalible();
        System.out.println("New update avalible: " + newUpdate);
        if (newUpdate) {
            Updater.showUpdatePrompt();
        }
        */

        // Get the debug background image
        Image debugbackgroundimage = new Image(
                this.getClass().getClassLoader().getResourceAsStream("resources/debugbackground.jpg"));

        // Create a stack pane to add all the objects into
        stack = new Pane();

        // Setup the background
        stack.setBackground(new Background(new BackgroundImage(debugbackgroundimage, BackgroundRepeat.NO_REPEAT,
                BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, new BackgroundSize(
                        debugbackgroundimage.getWidth(), debugbackgroundimage.getHeight(), false, false, true, true))));

        // Set the preferred and minimum sizes for the stack
        stack.setPrefSize(debugbackgroundimage.getWidth(), debugbackgroundimage.getHeight());
        stack.setMinSize(16, 9);

        // We need an anchor pane go help automatically constrain the maximum and
        // minimum sizes of things
        AnchorPane anchor = new AnchorPane();
        AnchorPane.setTopAnchor(stack, 0.0);
        AnchorPane.setBottomAnchor(stack, 0.0);
        AnchorPane.setLeftAnchor(stack, 0.0);
        AnchorPane.setRightAnchor(stack, 0.0);


        // Add the stack to the anchor pane
        anchor.getChildren().addAll(stack);

        // Set the anchor background to a light gray, that way we can check for overlap
        anchor.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, CornerRadii.EMPTY, Insets.EMPTY)));

        // Set the scene for the visualizer, use the anchor pane defined above
        Scene mCatScene = new Scene(anchor);

        // Set the contents of the window to that of the scene
        stage.setScene(mCatScene);

        // Show the window (stage)
        stage.show();

        // Add a test node to check if it scales properly
        Text test = new Text(String.format("Test\nFoo bar"));

        test.setFont(new Font("Arial", 15));
        test.setWrappingWidth(500);
        test.setFill(Color.RED);
        test.setLayoutX(0);
        test.setLayoutY(50);

        Circle test2 = new Circle();
        test2.setLayoutX(50);
        test2.setLayoutY(120);
        test2.setFill(Color.GREEN);
        test2.setRadius(10);


        stack.getChildren().add(0, test);
        stack.getChildren().add(1, test2);

    }

    public static void main(String[] args) {

        Main This = new Main();

        // Check if debug mode is enabled (Basically check for console spam enabled)
        try {
            This.debug = Boolean.parseBoolean(args[0]);
        } catch (Exception e) {
            This.debug = false;
        }

        launch(args);

    }
}

这是我使用的背景图片: 在此处输入图像描述

编辑: 是背景如何反应(理想)与节点如何反应(不理想)的 gif

标签: javajavafx

解决方案


推荐阅读