移动的JFrame的一部分不想刷新

时间:2017-03-10 22:29:36

标签: java swing jframe window

当移动JFrame时,我有时会看到它的一部分,即框架及其旧位置重叠的部分,不会刷新。

Here's an Horrible Paintjob Of An Explanation.

我亲爱的理论是,默认情况下,Java不会更新隐藏在另一个窗口后面的JFrame的内容,并且它有时无法理解这个其他窗口实际上是JFrame本身,但是之前移动你做到了(and where did that other dog come from...)。

是否有办法强制JFrame完全重绘自己,无论其环境如何,或至少让它重新检查所述环境?在哪里/如何获得窗口移动事件的解释方法,最后我放置它?

代码示例:

MyFrame:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
public class MyFrame extends JFrame{
    private static final long serialVersionUID = 1L;
    public MyFrame(){
        this.setTitle("2D view");
        this.setSize(640,480);
        MyPanel mp=new MyPanel();
        this.setContentPane(mp);
        Timer timer=new Timer();
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                timer.cancel();
                MyFrame.this.dispose();
            }
            public void windowOpened(WindowEvent e){
                if(MyFrame.this.isFocused()){
                    mp.requestFocus();
                }
            }
        });
        timer.scheduleAtFixedRate(new TimerTask(){
            public void run() {
                mp.repaint();
            }
        },0,1000/60);
    }
}

MyPanel:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MyPanel extends JPanel{
    private static final long serialVersionUID = 1L;
    private static final int tsx=15;
    private static final int tsy=15;
    private int vscis;
    private int hscis;
    public MyPanel(){
        this.vscis=this.hscis=-1;
    }
  @Override
    public void setBounds(int x,int y,int width,int height){
        super.setBounds(x, y, width, height);
        this.vscis=-1;
        this.hscis=-1;
    }
    public void paintComponent(Graphics g){
        int w=this.getWidth(),h=this.getHeight();
        if(this.hscis==-1){
            this.hscis=(((w-(1+MyPanel.tsx))/2)/MyPanel.tsx)+1;
        }
        if(this.vscis==-1){
            this.vscis=(((h-(1+MyPanel.tsy))/2)/MyPanel.tsy)+1;
        }
        int i,j,posY=(h-((vscis*2+1)*tsy))/2,bposX=(w-((hscis*2+1)*tsx))/2,posX;
        Color clearC=new Color(11,66,66);
        Color clearW=new Color(0,0,0);
        for(i=0;i<=vscis*2;++i){
            posX=bposX;
            for(j=0;j<=hscis*2;++j){
                g.setColor(clearC);
                g.fillRect(posX,posY,tsx,tsy);
                g.setColor(clearW);
                g.drawLine(posX, posY, posX+tsx, posY);
                g.setColor(clearW);
                g.drawLine(posX, posY, posX, posY+tsy);
                posX+=tsx;
            }
            posY+=tsy;
        }
    }
}

实例化MyFrame,并在屏幕上移动几次。我使用的是Win7,当我移动它时,我的窗口内容没有显示(取消选中某处的选项)。这个小故障本质上是不可再现的,但我在其他网站上提供了[截图] [5]。

[5] user.oc-static(dot)com / upload / 2017/03/10 / 148918317799_Sans%20titrescreencap.png

无法发布两个以上的链接,数字。

0 个答案:

没有答案