简体   繁体   English

重新绘制另一个JPanel中的JPanel问题

[英]Repaint issue with JPanel inside another JPanel

I'm drawing shapes inside this JPanel , which is also inside another main JPanel . 我正在这个JPanel内部绘制形状,该JPanel也在另一个主JPanel内部。 At repaint() it only draws the shapes for one millisecond and then they disappear. repaint()它仅绘制形状一毫秒,然后它们消失。 They don't stay painted, why? 他们没有画画,为什么?

My paintComponent method is something like this 我的paintComponent方法是这样的

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    for (int i = 0; i < reportElements.size(); i++) {
        this.reportElements.get(i).display((Graphics2D) pageComponents.get(i).getGraphics());

    }
}

When the parent is a JEditorPane with setEditable() enabled, it works and we can see the shapes but when it's a JPanel , after a millisecond all I see is empty panels. 当父级是启用setEditable()JEditorPane时,它可以工作,我们可以看到形状,但是当它是JPanel ,在一毫秒后,我看到的只是空面板。

您应该将其绘制为JPanel的Graphics对象,以使其永久...在您的示例中,这是Graphics g ...

ultrajohn is dead on. Ultrajohn死了。 You need to use the Graphics you have been passed. 您需要使用已通过的Graphics Read on for the why... 继续阅读为什么...

Java 1.6 introduced the RepaintManager that supports the optimizing of repaint requests. Java 1.6引入了RepaintManager ,它支持优化重画请求。 It has some subtle effects on painting. 它对绘画有一些微妙的影响。

In this case, you're working with multiple Graphics2D objects: the g passed into paintComponent and the value returned by the getGraphics call. 在这种情况下,您要使用多个Graphics2D对象:将g传递给paintComponent并由getGraphics调用返回的值。

The repaint manager has handed you g on which to paint. 重绘经理递给你g上作画。 Note: this does not paint on the screen, but on a temporary buffer (assuming the default double buffering). 注意:这不会在屏幕上绘制,而是在临时缓冲区上绘制(假定使用默认的双缓冲)。

In the paintComponent call you are painting to the graphics obtained from getGraphics of the various components. paintComponent调用中,您正在绘制从各个组件的getGraphics获得的图形。 This is bypassing the repaint manager and painting directly to the unbuffered display. 这绕过了重新绘制管理器,直接绘制到无缓冲的显示。

When paintChildren returns, the RepaintManager kicks in to handle updating the double buffer. paintChildren返回时, RepaintManager启动以处理更新双缓冲区。 It paints the blank temporary buffer over the displayed buffer, effectively erasing what was painted through the graphics object obtained from getGraphics 它在显示的缓冲区上绘制空白的临时缓冲区,从而有效擦除通过从getGraphics获得的图形对象绘制的内容

in this case what happens to those pageComponents, the small JPanels? 在这种情况下,那些pageComponents(小的JPanels)会怎样? The parent panel is not gonna draw the shapes on them is it? 父面板不会在其上绘制形状吗?

I'm not sure I undertand your comment. 我不确定我是否理解您的评论。 Your main panel should contain child panels. 您的主面板应包含子面板。 The child panels should be added to the main panel using a layout manager. 子面板应使用布局管理器添加到主面板。 Then when Swing decides to repaint the main panel, it will also repaint all the child panels and in turn the child panels will repaint there shapes. 然后,当Swing决定重新粉刷主面板时,它还将重新粉刷所有子面板,然后这些子面板将重新粉刷那里的形状。

For what its worth Custom Painting Approaches has a working example of drawing shapes on a panel. 自定义绘画方法有什么用,可以在面板上绘制形状。

It might be that paintChildren or paintBorder overdraws whatever you've drawn in your method. 可能是paintChildrenpaintBorder重绘了您在方法中绘制的内容。 Perhaps try overriding paint instead. 也许尝试改写paint There you have full control over what will be painted on the component, and you can decide yourself whether to further call paintComponent , paintChildren or paintBorder at all. 在那里,您可以完全控制将在组件上绘制的内容,并且可以决定是否进一步调用paintComponentpaintChildrenpaintBorder

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

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