有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在JavaFX上调整画布大小以适应大小

我是JavaFX新手,在调整画布大小时遇到了一个问题。 我试图让画布的大小正好适合我放入其中的内容。 输出应该是片段5/7中唯一的一个,但是我在我画的数字旁边有很多空白。 我尝试使用setWidth调整画布的大小,甚至从一开始就将画布设置为小尺寸,但似乎没有任何帮助

这是我的密码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class Example extends Application {

    Font fontLarge = Font.font("Droid Sans", FontWeight.BOLD, 15);

    @Override
    public void start(Stage stage) {

        HBox root = new HBox();
        Scene scene = new Scene(root);

        root.getChildren().add(getCanvasOfRationalNumber("5", "7"));
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }

    public Canvas getCanvasOfRationalNumber(String num, String denom) {
        final Canvas rnCanvas = new Canvas(300, 55);
        GraphicsContext gc = rnCanvas.getGraphicsContext2D();
        gc.setFont(fontLarge);

        gc.fillText(num, 0, 15);
        gc.fillText("___", 0, 20);
        gc.fillText(denom, 0, 40);

        rnCanvas.setWidth(15);
        return rnCanvas;
    }

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

}

共 (1) 个答案

  1. # 1 楼答案

    编辑:我之前的答案是错的,这是新的

    我们需要澄清你真正想要什么。如果我现在理解正确的话,你想要的是窗口大小本身不大于画布大小

    正如jewelsea在评论中指出的,你可以改变画布的宽度和高度。事实上,您已经在代码中这样做了

    我建议你为画布和场景选择不同的颜色。如果你这样做,例如:

    public class Example extends Application {
    
        Font fontLarge = Font.font("Droid Sans", FontWeight.BOLD, 15);
    
        @Override
        public void start(Stage stage) {
    
            HBox root = new HBox();
            Scene scene = new Scene(root, Color.YELLOW);
    
            root.getChildren().add(getCanvasOfRationalNumber("5", "7"));
            scene.setRoot(root);
            stage.setScene(scene);
            stage.show();
        }
    
        public Canvas getCanvasOfRationalNumber(String num, String denom) {
            final Canvas rnCanvas = new Canvas(300, 55);
            GraphicsContext gc = rnCanvas.getGraphicsContext2D();
            gc.setFont(fontLarge);
    
            gc.setFill(Color.LIGHTBLUE);
            gc.fillRect(0, 0, 300, 55);
    
            gc.setFill(Color.BLUE);
    
            gc.fillText(num, 0, 15);
            gc.fillText("___", 0, 20);
            gc.fillText(denom, 0, 40);
    
            rnCanvas.setWidth(15);
            return rnCanvas;
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    你会得到这个

    enter image description here

    您可以清楚地看到画布已调整大小。你面临的问题是窗户不能再小了。这是场景中的白色(这里是黄色)空间,而不是画布上的。您可以尝试将宽度设置为1,将高度设置为1,但最终仍然会出现以下情况:

    enter image description here

    我的猜测是,窗口大小不能因为最小/最大/关闭按钮而变小。您可以使用initStyle方法更改样式以去掉按钮

    这一切都取决于你的要求


    旧(错)答案:

    不能修改Canvas的宽度/高度

    Canvas is an image that can be drawn on using a set of graphics commands provided by a GraphicsContext.

    A Canvas node is constructed with a width and height that specifies the size of the image into which the canvas drawing commands are rendered. All drawing operations are clipped to the bounds of that image.

    您的解决方案是要么将画布区域复制到另一个大小的新画布,要么根本不使用画布,而是使用Text节点。但这一切都取决于你的要求