当前位置: 首页 > 知识库问答 >
问题:

从类对象获取窗格的宽度和高度时出现问题- Javafx

宗政颖逸
2023-03-14

好了,我理解了调整窗格中对象大小的绑定原则,这个问题有点不同。我很困惑,我试图创建一个扩展Pane的类,在我的main中创建一条线,将它的startX和startY坐标绑定到窗格的中心。问题是,当使用getWidth() / 2或getHeight() /2时,当我按下箭头键时,坐标被放置在起始坐标(0,0)的左上某处,当按下箭头键时,它会创建另一条线,该线在按下的给定方向上绘制,并从最后一条线的末端开始。

就像我说的,当我使用getWidth() / 2和getHeight / 2作为新行的startX和startY坐标时,作为回报,该行被放置在一个负坐标中,将其放置在屏幕上方窗格起始坐标(0,0)的左侧。

下面是我的代码的一部分,其中包含我遇到问题的默认构造函数,在非默认构造函数上,我可以手动输入起始坐标,当我这样做时,该行正好位于我想要的位置。

public class LineDrawingObject extends Pane {
    // ArrayList to store the Line Object's
    ArrayList<Line> lines = new ArrayList<>();
    Line line;
    private Color lineColor;
    private double lineLength;
    private int lineCount = 0;
    private double startX;
    private double startY;
    private double endX;
    private double endY;

    /** Default Constructor */
    public LineDrawingObject() {
        this.lineLength = 20;
        line = new Line(this.getWidth() / 2, this.getHeight() / 2,
            (this.getWidth() / 2), (this.getHeight() / 2) - this.lineLength);
        this.lineColor = Color.BLACK;
        line.setStroke(this.lineColor);
        this.lineCount++;
        this.lines.add(line);
        getChildren().add(line);
    }

编辑:我想我可能需要添加更多信息

另外,我想补充一点,我的窗格大小是在新场景中设置的(窗格,250,250),所以中心坐标是(125,125)……如果窗格上使用getWidth和getHeight方法会返回尚未绘制的无效大小吗?我尝试在开始方法中设置首选大小,但似乎不起作用。如果是这种情况,我该如何解决这个问题?

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * Created by John on 7/24/2014.
 */
public class DrawLines extends Application {
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        // Create a pane
        Pane pane = new Pane();

        // Create object to draw lines upon KeyEvent
        LineDrawingObject lineDrawingObject = new LineDrawingObject(20, Color.BLACK,
            pane.getWidth() / 2, pane.getWidth() / 2);
        pane.getChildren().add(lineDrawingObject);
        lineDrawingObject.setOnKeyPressed(e -> {
            lineDrawingObject.paintLine(e.getCode());
        });

        // Create a scene and place it in the pane
        Scene scene = new Scene(pane, 250, 250);
        primaryStage.setTitle("DrawLines"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage

        // Allow object to receive key input
        lineDrawingObject.requestFocus();
    }
}

这是LineDrawing对象:

import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;

import java.util.ArrayList;

/** This object will draw lines inside of a Pane when an arrow key is
 *  pressed and will draw it in that direction from the current line */
public class LineDrawingObject extends Pane {
    // ArrayList to store the Line Object's
    ArrayList<Line> lines = new ArrayList<>();
    Line line;
    private Color lineColor;
    private double lineLength;
    private int lineCount = 0;
    private double startX;
    private double startY;
    private double endX;
    private double endY;

    /** Default Constructor */
    public LineDrawingObject() {
        this.lineLength = 20;
        line = new Line(this.getWidth() / 2, this.getHeight() / 2,
            (this.getWidth() / 2), (this.getHeight() / 2) - this.lineLength);
        this.lineColor = Color.BLACK;
        line.setStroke(this.lineColor);
        this.lineCount++;
        this.lines.add(line);
        getChildren().add(line);
    }

    /** Secondary Constructor, allows you to control the line length and color */
    public LineDrawingObject(double lineLength, Color lineColor, double startX, double startY) {
        this.lineLength = lineLength;
        line = new Line(startX, startY,
                startX, startY - this.lineLength);
        this.lineColor = lineColor;
        line.setStroke(this.lineColor);
        this.lineCount++;
        this.lines.add(line);
        getChildren().add(line);
    }

    public ArrayList<Line> getLines() {
        return lines;
    }

    public void setLines(ArrayList<Line> lines) {
        this.lines = lines;
    }

    public Line getLine() {
        return this.line;
    }

    public void setLine(Line line) {
        this.line = line;
    }

    public Color getLineColor() {
        return this.lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public double getLineLength() {
        return this.lineLength;
    }

    public void setLineLength(double lineLength) {
        this.lineLength = lineLength;
    }

    public int getLineCount() {
        return this.lineCount;
    }

    public void setLineCount(int lineCount) {
        this.lineCount = lineCount;
    }

    public double getStartX() {
        return this.startX;
    }

    public void setStartX(double startX) {
        this.startX = startX;
    }

    public double getStartY() {
        return this.startY;
    }

    public void setStartY(double startY) {
        this.startY = startY;
    }

    public double getEndX() {
        return this.endX;
    }

    public void setEndX(double endX) {
        this.endX = endX;
    }

    public double getEndY() {
        return this.endY;
    }

    public void setEndY(double endY) {
        this.endY = endY;
    }

    public void paintLine(KeyCode keyCode) {
        // Set line start coordinates to the end of the last line
        setStartX(line.getEndX());
        setStartY(line.getEndY());

        // Set line end coordinates
        switch (keyCode) {
            case UP: goUp(); break;
            case LEFT: goLeft(); break;
            case DOWN: goDown(); break;
            case RIGHT: goRight(); break;
        }

        // Create line
        line = new Line(getStartX(), getStartY(), getEndX(), getEndY());
        line.setStroke(lineColor);
        this.lines.add(line);
        getChildren().add(line);
    }

    public void goLeft() {
        setEndX(getStartX() - this.lineLength);
        setEndY(getStartY());
    }

    public void goRight() {
        setEndX(getStartX() + this.lineLength);
        setEndY(getStartY());
    }

    public void goUp() {
        setEndX(getStartX());
        setEndY(getStartY() - this.lineLength);
    }

    public void goDown() {
        setEndX(getStartX());
        setEndY(getStartY() + this.lineLength);
    }
}

共有2个答案

卜阳
2023-03-14

为了解决这个问题,我决定用一种更简单的方法,为构造函数设置一个预设的size属性,并设置一些额外的构造函数来允许你自己设置大小。

import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;

import java.util.ArrayList;

/** This object will draw lines inside of a Pane when an arrow key is
 *  pressed and will draw it in that direction from the current line */
public class LineDrawingObject extends Pane {
    // ArrayList to store the Line Object's
    ArrayList<Line> lines = new ArrayList<>();
    Line line;
    private Color lineColor;
    private double lineLength;
    private double startX;
    private double startY;
    private double endX;
    private double endY;

    /** Default Constructor */
    public LineDrawingObject() {
        // Set a default size for this Pane
        this.setWidth(350);
        this.setHeight(350);
        // Set Line properties
        this.startX = getWidth() / 2;
        this.startY = getHeight() / 2;
        this.lineLength = 20;
        this.lineColor = Color.BLACK;
        // Create line and set it's Stroke
        line = new Line(this.startX, this.startY,
                this.startX, this.startY - this.lineLength);
        line.setStroke(this.lineColor);
        // Add line to ArrayList and Pane
        lines.add(line);
        getChildren().add(line);
    }

    /** Second Constructor, allows you to control the line length, color and center it */
    public LineDrawingObject(double lineLength, Color lineColor) {
        // Set a default size for this Pane
        this.setWidth(350);
        this.setHeight(350);
        // Set line properties
        this.startX = getWidth() / 2;
        this.startY = getHeight() / 2;
        this.lineLength = lineLength;
        this.lineColor = lineColor;
        // Create line and set it's Stroke
        line = new Line(this.startX, this.startY,
                this.startX, this.startY - this.lineLength);
        line.setStroke(this.lineColor);
        // Add line to ArrayList and Pane
        lines.add(line);
        getChildren().add(line);
    }

    /** Third Constructor, allows you to control the line length, color, and pane size */
    public LineDrawingObject(double lineLength, Color lineColor,
                             double paneWidth, double paneHeight) {
        // Set a default size for this Pane
        this.setWidth(paneWidth);
        this.setHeight(paneHeight);
        // Set line properties
        this.startX = getWidth() / 2;
        this.startY = getHeight() / 2;
        this.lineLength = lineLength;
        this.lineColor = lineColor;
        // Create line and set it's Stroke
        line = new Line(this.startX, this.startY,
                this.startX, this.startY - this.lineLength);
        line.setStroke(this.lineColor);
        // Add line to ArrayList and Pane
        lines.add(line);
        getChildren().add(line);
    }

    /** Third Constructor, allows you to control the line length and color, startX, and startY */
    public LineDrawingObject(double lineLength, double startX, double startY, Color lineColor) {
        // Set line properties
        this.startX = startX;
        this.startY = startY;
        this.lineLength = lineLength;
        this.lineColor = lineColor;
        // Create line and set it's Stroke
        line = new Line(this.startX, this.startY,
                this.startX, this.startY - this.lineLength);
        line.setStroke(this.lineColor);
        // Add line to ArrayList and Pane
        lines.add(line);
        getChildren().add(line);
    }

    /** Get the list of lines */
    public ArrayList<Line> getLines() {
        return lines;
    }

    /** Get the current line object */
    public Line getLine() {
        return this.line;
    }

    /** Manually set the current line object */
    public void setLine(Line line) {
        this.line = line;
    }

    /** Get the current line object's color */
    public Color getLineColor() {
        return this.lineColor;
    }

    /** Set the current line object's color */
    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    /** Get length of the lines */
    public double getLineLength() {
        return this.lineLength;
    }

    /** Set a new length for the lines */
    public void setLineLength(double lineLength) {
        this.lineLength = lineLength;
    }

    /** Get the count of the number of line's currently in existence */
    public int getLineCount() {
        return this.lines.size();
    }

    /** Get the startX coordinates */
    public double getStartX() {
        return this.startX;
    }

    /** Set the startX coordinates */
    public void setStartX(double startX) {
        this.startX = startX;
    }

    /** Get the startY coordinates */
    public double getStartY() {
        return this.startY;
    }

    /** Set the startY coordinates */
    public void setStartY(double startY) {
        this.startY = startY;
    }

    /** Get the endX coordinates */
    public double getEndX() {
        return this.endX;
    }

    /** Set the endX coordinates */
    public void setEndX(double endX) {
        this.endX = endX;
    }

    /** Get the endY coordinates */
    public double getEndY() {
        return this.endY;
    }

    /** Set the endY coordinates */
    public void setEndY(double endY) {
        this.endY = endY;
    }

    /** Paint the next line based on the key pressed */
    public void paintLine(KeyCode keyCode) {
        // Set line start coordinates to the end of the last line
        setStartX(line.getEndX());
        setStartY(line.getEndY());

        // Set line end coordinates
        switch (keyCode) {
            case UP: goUp(); break;
            case LEFT: goLeft(); break;
            case DOWN: goDown(); break;
            case RIGHT: goRight(); break;
        }

        // Create line
        line = new Line(getStartX(), getStartY(), getEndX(), getEndY());
        line.setStroke(lineColor);
        this.lines.add(line);
        getChildren().add(line);
    }

    /** Set the end coordinates to the left of the last line */
    public void goLeft() {
        setEndX(getStartX() - this.lineLength);
        setEndY(getStartY());
    }

    /** Set the end coordinates to the right of the last line */
    public void goRight() {
        setEndX(getStartX() + this.lineLength);
        setEndY(getStartY());
    }

    /** Set the end coordinates above the last line */
    public void goUp() {
        setEndX(getStartX());
        setEndY(getStartY() - this.lineLength);
    }

    /** Set the end coordinates below the last line */
    public void goDown() {
        setEndX(getStartX());
        setEndY(getStartY() + this.lineLength);
    }
}
杜俊楚
2023-03-14

您正在构造函数中调用 getWidth()getHeight(),这必须在将窗格添加到任何实时场景之前执行。因此,这些将返回 0(因为窗格尚未布局)。

相反,将线的坐标绑定到基于< code>widthProperty和< code>heightProperty的值:

line = new Line();
line.startXProperty().bind(widthProperty().divide(2));
line.startYProperty().bind(heightProperty().divide(2));
line.endXProperty().bind(widthProperty().divide(2));
line.endYProperty().bind(heightProperty().divide(2).subtract(lineLength));
 类似资料:
  • 因此,我创建了一个简单的模拟,其中方块是随机产生的,随机向量和窗口边缘的反弹。 我希望它能考虑到正在调整的窗口大小。因此,如果我将窗口的尺寸从600x600改为1200x600,则新边框的方块将会弹出,而不是600x600。 我尝试执行getWidth()getHeight(),但它将返回0。因此,我将它放在pain()(因为它在window resize中被调用)方法中,并将返回值保存为局部变量

  • 我不知道如何获得堆栈窗格的宽度: 如果我指的是 http://docs.oracle.com/javafx/2.0/api/javafx/scene/layout/StackPane.html,堆栈窗格的尺寸应该适应它的内容: 堆栈窗格的父级将在布局期间在堆栈窗格的可调整大小范围内调整堆栈窗格的大小。默认情况下,堆栈窗格根据其内容计算此范围,如下表所述。 首选宽度左/右插入加上最大的子首选宽度。

  • 我是JavaFX的新手,我正在编写一个简单的登录应用程序,我想在我的web浏览器中启动它。 我如何获得web浏览器的宽度和高度?是否有一些对象用于此目的,或一个方法,我希望能够调整应用程序的大小,每当浏览器的大小改变。

  • 本文向大家介绍jQuery获得document和window对象宽度和高度的方法,包括了jQuery获得document和window对象宽度和高度的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了jQuery获得document和window对象宽度和高度的方法。分享给大家供大家参考。具体如下: 希望本文所述对大家的jQuery程序设计有所帮助。

  • 显然,有两个方法和 是这样的: 我看了这篇文章,但它推荐了一种已弃用的方法: 如何在爪哇fx中获取标签

  • 我有一个base64 img编码,你可以在这里找到。我怎样才能得到它的高度和宽度?