日期:2014-05-20  浏览次数:20720 次

Swing中JFrame的刷新问题?
看API介绍的时候JFrame好像已经实现了双缓冲,但我调用repaint()的时候,原来存在的填充不会刷新掉,还保留在原来的位置上,一般的Frame我重写一个update()方法实现双缓冲就能把旧的填充刷掉。JFrame怎样才能刷掉整个框架?

------解决方案--------------------
看看frame.revoilate(),看看行不?
------解决方案--------------------
如果你想要整个JFrame全部重新刷新调用 JFrame.validate();这个方法
这个方法会验证JFrame容器下面的所有组件,然后重绘和重排布各个组件
------解决方案--------------------
学习~
------解决方案--------------------
调用updateUI();这个方法是刷新该组件上所有的内容
------解决方案--------------------
我也遇到了这个问题
查查网上资料说
Frame中可以,在JFrame中则不能直接重画
要用一个panel 来帮助实现

------解决方案--------------------
JComponent的子类支持双缓冲
Java code
  void paintForceDoubleBuffered(java.awt.Graphics);
  void setCreatedDoubleBuffer(boolean);
  boolean getCreatedDoubleBuffer();
  public void setDoubleBuffered(boolean);
  public boolean isDoubleBuffered();

------解决方案--------------------
终于搞定了,这回可是货真价实的,呵呵
package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class FrameUpdate implements ActionListener{

JFrame frame;
JButton one;
JButton two;

public FrameUpdate(){
frame = new JFrame();
one = new JButton("下一个");
one.addActionListener(this);
two = new JButton("上一个");
two.addActionListener(this);
frame.add(one);
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent actionEvent){
String actionName = actionEvent.getActionCommand();
if(actionName.equals("下一个")){
frame.remove(one);
frame.add(two);
frame.invalidate();
frame.repaint();
frame.setVisible(true);
}
else if(actionName.equals("上一个")){
frame.remove(two);
frame.add(one);
frame.invalidate();
frame.repaint();
frame.setVisible(true);
}
}
public static void main(String[] args) {
new FrameUpdate();
}

}