【问题标题】:getHeight() and getWidth() both return 0getHeight() 和 getWidth() 都返回 0
【发布时间】:2017-02-25 17:08:52
【问题描述】:

我正在尝试用线条在 JPanel 中创建一个网格,为此,我绘制均匀间隔的水平和垂直线,直到到达 JPanel 的末端。我使用了一个名为 Drawing 的类,它扩展了 JPanel 并且是我添加到窗口中的对象。下面是我的代码。

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();
        }
    }
}

问题是 'this.getHeight()' 和 'this.getWidth()' 都返回 0,所以网格不会被绘制。画线效果很好,只是面板显然没有尺寸。我该如何解决这个问题。

【问题讨论】:

  • 当您提出问题时,请发布正确的minimal reproducible example。我们不知道该面板在您的实际应用程序中是如何使用的上下文,只能猜测。
  • 我在 JSplitPane 的 Window 类中实例化了 Panel
  • 声明不是minimal reproducible example。无论如何,你有一个建议,所以试试这个建议。如果您需要更多帮助,那么这个问题和所有未来的问题都应该附有适当的示例。

标签: java swing jframe jpanel paintcomponent


【解决方案1】:

不是主要问题,但您需要覆盖类的 getPreferredSize() 方法以返回大小。

每个组件负责确定自己的首选大小。 然后布局管理器可以在面板添加到父面板时使用此信息。

当然,这假设父面板正在使用您应该使用的布局管理器。

有关更多信息和工作示例,请阅读 Custom Painting 上的 Swing 教程部分

真正的问题是当您调用以下方法时:

    initWorkSpace();

创建组件时,所有组件的大小都为零。因此,当从构造函数调用上述方法时,大小将始终为零。

如果您的绘画代码是动态的,这意味着它会随着框架大小的调整而改变,那么您需要在 paintComponent() 方法中调用该逻辑。

或者,如果您的逻辑过于复杂,无法在每次重绘组件时执行,您可以在面板中添加ComponentListener 并处理componentResized 方法并调用该方法。

另外,我不确定您在进行自定义绘画时为什么要使用 GroupLayout。

【讨论】:

  • 抱歉这个问题很糟糕...下次我会尝试更彻底。 initWorkSpace() 的调用;是问题所在。谢谢!
猜你喜欢
  • 2014-03-02
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
相关资源
最近更新 更多