.setVisible(true)立即重绘

Zym*_*mox 5 java swing transparency visibility repaint

在一个简短的方法中,我使用setVisible(false)隐藏JFrame.然后我截取屏幕截图并使用setVisible(true)恢复JFrame.

在再次显示之后,窗口应该显示与之前不同的图片(假设拍摄截图的一部分).

问题是在调用setVisible(true)之后,在调用paintComponent并绘制更新状态之前,窗口会使用旧内容闪烁一瞬间.

我可能会以一种丑陋的方式解决这个问题,但我想知道是否有更好的解决方案.

在此先感谢您的帮助

编辑:在准备一个例子的时候,我注意到,当我不在程序中使用透明度时几乎看不到效果.应该提到这一点.这是我想出的:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.sun.awt.AWTUtilities;
public class Test {

    static boolean flag = false;
    static Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame();
        frame.setUndecorated(true);
        AWTUtilities.setWindowOpaque(frame, false);  //draw on a transparent window
        frame.setSize(scrSize.width, scrSize.height);
        frame.setContentPane(new JPanel() {
            protected void paintComponent(Graphics g) 
            {
                if (Test.flag) {
                    g.setColor(Color.RED);
                    g.drawRect(50, 50, scrSize.width - 100, scrSize.height - 100);
                }
                else {
                    g.setColor(Color.GREEN);
                    g.fillOval(50, 50, scrSize.width - 100, scrSize.height - 100);
                }
            }
        });
        frame.setVisible(true); //green oval shown
        Thread.sleep(1000);
        frame.setVisible(false);
        flag = true; // next draw produces red rectangle
        Thread.sleep(1000);
        frame.setVisible(true); // before the next draw, 
                                // you can see a flash of the green oval
    }

}
Run Code Online (Sandbox Code Playgroud)

rth*_*sen 3

我意识到这个答案是在问题提出一年后提出的,但我也遇到了类似的问题,当然在尝试解决它之前进行了一些研究。对于遇到此问题的任何人,请尝试在打包并使其可见之前处理掉您的窗户/框架。