简体   繁体   English

getHeight()和getWidth()均返回0

[英]getHeight() and getWidth() both return 0

I am trying to create a grid in a JPanel with lines, and to do this, I draw evenly spaced horizontal and vertical lines until I reach the end of the JPanel. 我试图在带有线条的JPanel中创建一个网格,并且要做到这一点,我绘制均匀间隔的水平和垂直线,直到到达JPanel的尽头。 I use a class called Drawing which extends JPanel and is the object I add to the window. 我使用了一个名为Drawing的类,该类扩展了JPanel,并且是我添加到窗口中的对象。 Below is my code. 下面是我的代码。

public final class Drawing extends JPanel {

    public Drawing() {
        super();
        setBackground(new Color(255, 255, 255));
        setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));

        GroupLayout workspacePanelLayout = new GroupLayout(this);
        setLayout(workspacePanelLayout);
        workspacePanelLayout.setHorizontalGroup(workspacePanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 343, Short.MAX_VALUE));
        workspacePanelLayout.setVerticalGroup(workspacePanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE));

        initWorkSpace();
    }

    private static class Line {

        final int x1;
        final int y1;
        final int x2;
        final int y2;
        final Color color;

        public Line(int x1, int y1, int x2, int y2, Color color) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.color = color;
        }
    }

    private final LinkedList<Line> lines = new LinkedList<>();

    public void addLine(int x1, int x2, int x3, int x4) {
        addLine(x1, x2, x3, x4, Color.black);
    }

    public void addLine(int x1, int x2, int x3, int x4, Color color) {
        lines.add(new Line(x1, x2, x3, x4, color));
        repaint();
    }

    public void clearLines() {
        lines.clear();
        repaint();
    }

    @Override
    private void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Line line : lines) {
            g.setColor(line.color);
            g.drawLine(line.x1, line.y1, line.x2, line.y2);
        }
    }

    public void initWorkSpace() {
        int x = 0;
        int y = 0;
        while (x < this.getWidth()) {
            addLine(x, 0, x, this.getHeight(), new Color(153, 153, 153));
            x += getSpacing();
        }
        while (y < this.getHeight()) {
            addLine(0, y, this.getWidth(), y, new Color(153, 153, 153));
            y += getSpacing();
        }
    }
}

The problem is that 'this.getHeight()' and 'this.getWidth()' both return 0 so the grid doesn't get drawn. 问题在于“ this.getHeight()”和“ this.getWidth()”都返回0,因此不会绘制网格。 drawing the lines works fine, its just that the panel apparently has no dimension. 绘制线条效果很好,只是面板显然没有尺寸。 How do I solve this. 我该如何解决。

Not the main problem but you need to override the getPreferredSize() method of your class to return the size. 这不是主要问题,但是您需要重写类的getPreferredSize()方法以返回大小。

Each component is responsible for determining its own preferred size. 每个组件负责确定自己的首选大小。 Then the layout manager can use this information when the panel is added to a parent panel. 然后,当面板添加到父面板时,布局管理器可以使用此信息。

Of course this assumes the parent panel is using a layout manager which you should be doing. 当然,这假设父面板正在使用您应该做的布局管理器。

For more information and working examples read the section from the Swing tutorial on Custom Painting 有关更多信息和工作示例,请阅读Swing教程中有关“ 自定义绘画”的部分

The real problem is when you invoke the following method: 真正的问题是当您调用以下方法时:

    initWorkSpace();

All components have a zero size when the component is created. 创建组件时,所有组件的大小均为零。 So when the above method is invoked from the constructor the size will always be zero. 因此,当从构造函数调用上述方法时,大小将始终为零。

If your painting code is dynamic which means it changes as the frame is resized, then you need to invoke that logic inside the paintComponent() method. 如果您的绘画代码是动态的,这意味着它随着框架大小的改变而变化,那么您需要在paintComponent()方法内调用该逻辑。

Or if your logic is too complex to execute every time the component is repainted, you can add a ComponentListener to the panel and handle the componentResized method and invoke that method. 或者,如果您的逻辑过于复杂而无法在每次重新绘制组件时执行,则可以向面板添加ComponentListener并处理componentResized方法并调用该方法。

Also, I'm not sure why you are using a GroupLayout when you are doing custom painting. 另外,我不确定在进行自定义绘画时为什么使用GroupLayout。

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

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