在JPanel上绘制多个JComponent不起作用

斯蒂芬·福克(Stefan Falk)

我就是不明白这一点。我想向中添加Highlight extends JComponent对象,PdfPage extends JPanelHighlight组件很简单,没有绘制。我可以看到它们的paint()paintComponent()方法以正确的顺序被调用,但是它们没有显示出来。解决我的问题的唯一方法是添加:

for(Component component : this.getComponents()) {           
    component.paint(g);         
}

进入PdfPanel.paint()方法,但这不是我想要方法。我想PdfPage extends JPanel呈现JComponent我要添加的任何内容,paint()尽可能不覆盖

这是我HighlightPdfPage面板添加组件的方式

for (DatasheetError datasheetError : datasheetErrorList) {

    int pageNumber = datasheetError.getPageNumber();
    Highlight highlight = createErrorHighlight(datasheetError);

    PdfPage pdfPage = pdfPages[pageNumber];     
    pdfPage.add(highlight);
}

这是什么PdfPage样子。请注意,我没有使用a,LayoutManager因为我正在打电话super(null);

public class PdfPage extends JPanel  {
    private static final long serialVersionUID = 7756137054877582063L;

    final Image pageImage;

    public PdfPage(Image pageImage) {
        // No need for a 'LayoutManager'
        super(null);

        this.pageImage = pageImage;     
        Rectangle bounds = new Rectangle(0, 0, pageImage.getWidth(null), pageImage.getHeight(null));
        this.setBounds(bounds);
        this.setLayout(null);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        paintPdfPage(g);
    }

    private void paintPdfPage(Graphics g) {
        // For now transparent background to see if `Highlight` gets painted
        g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.0f));
        g.fillRect(0, 0, getWidth(), getHeight());
    }

}

Highlight.java中,您可以看到我打电话this.setBounds(bounds);

public class Highlight extends JComponent {
    private static final long serialVersionUID = -1010170342883487727L;

    private Color borderColor = new Color(0, 0, 0, 0);

    private Color fillColor;

    public Highlight(Rectangle bounds, Color fillColor) {
        this.fillColor = fillColor; 
        this.setBounds(bounds);
    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Rectangle bounds = this.getBounds();

        g.setColor(this.fillColor);
        g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);

        g.setColor(this.borderColor);
        g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);    
    }
}
斯坦尼斯拉夫

看起来问题是坐标空间

protected void paintComponent(Graphics g) {
...
        Rectangle bounds = this.getBounds();

        g.setColor(this.fillColor);
        g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
...
    }

getBounds()返回父容器上组件的Rectanle 所以当你可以打电话给g.fillRect(0, 0, bounds.width, bounds.height);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在JComponent或Jpanel上绘制多个形状?

来自分类Dev

在HTML5画布上绘制多个图像有时不起作用

来自分类Dev

Laravel 上的多个地方不起作用

来自分类Dev

Valign JPanel不起作用

来自分类Dev

Valign JPanel不起作用

来自分类Dev

Javascript:在画布上绘制矩形在IE上不起作用

来自分类Dev

多个“ \ a”不起作用

来自分类Dev

JComponent无法在JPanel中绘制

来自分类Dev

绘制矩形不起作用

来自分类Dev

在JLabel中绘制线条并在JFrame中添加为JPanel不起作用

来自分类Dev

JComponent contains方法在JTable的MouseEvent操作中不起作用

来自分类Dev

刷新JComponent进行倒计时不起作用

来自分类Dev

kendogrid上的多个过滤器不起作用

来自分类Dev

在多维数据集上的opentk多个纹理不起作用

来自分类Dev

Jframe和Jpanel大小不起作用

来自分类Dev

JPanel / JDialog中的mouseLIstener“不起作用”

来自分类Dev

JPanel覆盖方法不起作用

来自分类Dev

GridLayout在JPanel中不起作用

来自分类Dev

到JPanel的行不起作用

来自分类Dev

在画布上绘制可绘制图形不起作用

来自分类Dev

多个.on()函数不起作用

来自分类Dev

多个IF语句不起作用?

来自分类Dev

绘制数据框熊猫不起作用

来自分类Dev

在PHP中绘制矩形不起作用

来自分类Dev

JFrame中的绘制矩形不起作用

来自分类Dev

Matplotlib绘制多条线不起作用

来自分类Dev

JFrame中的绘制矩形不起作用

来自分类Dev

在PHP中绘制矩形不起作用

来自分类Dev

数据表绘制不起作用