简体   繁体   English

Java重绘不正确显示组件

[英]Java repaint not displaying component correctly

im developing an GUI Application, using JSWing, i load XML file, then deserialize it, then i add all created object to the JPanel. 我正在开发一个GUI应用程序,使用JSWing,我加载XML文件,然后反序列化它,然后我将所有创建的对象添加到JPanel。 However, until i move the window, or click on the panel, this is how they looks like 但是,在我移动窗口或点击面板之前,这就是它们的样子

在此输入图像描述

After i move the window, they look correctly, so how to fix this issue < 移动窗口后,它们看起来正确,所以如何解决这个问题<

I looked at this link http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics) and it might be the answer, since in the constructor of the JComponent i use 我查看了这个链接http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics) ,它可能是答案,因为在构造函数中我使用的JComponent

setOpaque(true);

but im still not sure how to fix the issue since that part of documentation is very hard to understand (it somehow just does not make any sense to me :-D ) 但我仍然不确定如何解决这个问题,因为那部分文档很难理解(它对某些方面对我没有任何意义:-D)

by the way, the painting itselfs goes something like this 顺便说一句,这幅画本身就是这样的

for (NetObject o : objects) {

                addNewObject(o);
            }

and addNewObject (not whole code) 和addNewObject(不是整个代码)

public void addNewObject(NetObject o) {



         DraggableComponent object = new DraggableComponent(o, editorIndex); //Creates a draggableComponent
        this.add(object);//Adds this component to main container

        object.setOverbearing(true); //On click ,this panel gains lowest z-buffer


            object.setSize(48, 48);
            object.setLocation(o.x - 23, o.y - 23);
            object.setBackground(Color.WHITE);
            this.repaint(); //this = JPanel

} }

and the overriden paintComponent code 和覆盖的paintComponent代码

@Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (isOpaque()) {
            if (object.type == 2) { //tarnsition
                g.drawImage(transition, 0, 0, this);

            } else if (object.type == 1) {
                boolean test = g.drawImage(place, 0, 0, this); //place
                g.drawString(object.loadTokens(), 3, 27); // 1,2,3,4...
            }
        }

    }

i tried to call this.revalidate - after FOR EACH LOOP - didnt help, the only way that works is to move somehow with the window, strangely, this problem exists only @ Windows, my collegue is developing this exact same application under Linux, and he does not experience ani graphical issues. 我试图调用this.revalidate - 在FOR EACH LOOP之后 - 没有帮助,唯一有效的方法是以某种方式移动窗口,奇怪的是,这个问题只存在@Windows,我的同事正在Linux下开发这个完全相同的应用程序,并且他没有遇到过图形问题。

I know that there been an awfully lot of topics like this, but i honestly was not able to figure out the solution. 我知道有很多这样的话题,但我老实说无法弄清楚解决方案。

Thanks for the answer, OSiRiS 谢谢你的回答,OSiRiS

The setBackground() API mentions that "It is up to the look and feel to honor this property, some may choose to ignore it." setBackground() API提到“尊重此属性取决于外观,有些人可能会选择忽略它”。 Set the graphics context's color explicitly in paintComponent() and invoke fillRect() . paintComponent()显式设置图形上下文的颜色并调用fillRect()

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

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