Swing中JFrame的刷新问题?

qqlwq123 2011-10-24 07:17:22
看API介绍的时候JFrame好像已经实现了双缓冲,但我调用repaint()的时候,原来存在的填充不会刷新掉,还保留在原来的位置上,一般的Frame我重写一个update()方法实现双缓冲就能把旧的填充刷掉。JFrame怎样才能刷掉整个框架?
...全文
4853 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
diandimei 2011-10-26
  • 打赏
  • 举报
回复
关键点在于,首先remove(以前的组件) 自然可以修改多个
然后来个invalidate()
继而调用repaint()
然后必须有的哦setVisible(true)
呵呵,被自己感动地说
diandimei 2011-10-26
  • 打赏
  • 举报
回复
终于搞定了,这回可是货真价实的,呵呵
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();
}

}
huntor 2011-10-26
  • 打赏
  • 举报
回复
JComponent的子类支持双缓冲
  void paintForceDoubleBuffered(java.awt.Graphics);
void setCreatedDoubleBuffer(boolean);
boolean getCreatedDoubleBuffer();
public void setDoubleBuffered(boolean);
public boolean isDoubleBuffered();


JFrame 是 Frame的子类。

swing和awt的绘制过程不同,看书去吧。
  • 打赏
  • 举报
回复
我也遇到了这个问题
查查网上资料说
Frame中可以,在JFrame中则不能直接重画
要用一个panel 来帮助实现
sd4324530 2011-10-25
  • 打赏
  • 举报
回复
调用updateUI();这个方法是刷新该组件上所有的内容
nizhicheng 2011-10-25
  • 打赏
  • 举报
回复
学习~
风尘中国 2011-10-25
  • 打赏
  • 举报
回复
如果你想要整个JFrame全部重新刷新调用 JFrame.validate();这个方法
这个方法会验证JFrame容器下面的所有组件,然后重绘和重排布各个组件
diandimei 2011-10-25
  • 打赏
  • 举报
回复
看看frame.revoilate(),看看行不?
package com.kerence.mine.mineGUI; import java.awt.BorderLayout; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.util.LinkedList; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JFrame; import com.kerence.mine.data_structure.MineMatrix; import com.kerence.mine.mineGUI.dialog.JEnterNameDialog; import com.kerence.mine.mineGUI.menu.JMineMenuBar; import com.kerence.mine.mine_model.Strategy.MineMatrixSettable; import com.kerence.mine.mine_model.Strategy.PreliminaryLevel; import com.kerence.mine.record.Rank; public class JMineSweeperFrame extends JFrame implements WindowListener { JMineMenuBar menuBar = new JMineMenuBar(); JStatusPanel statusPanel = new JStatusPanel(); JMineMatrixPanel jMineMatrixPanel = new JMineMatrixPanel(); public List preliminaryLevelRanks = new LinkedList(); public List intermediateLevelRanks = new LinkedList(); public List advancedLevelRanks = new LinkedList(); /** * 构造方法 对扫雷界面进行初始化,等级为初级,初始化菜单,添加标题栏图标,等操作 */ public JMineSweeperFrame() { // this.setResizable(false); this.setIconImage(new ImageIcon("./image/icon.gif").getImage()); this.setTitle("扫雷"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setJMenuBar(menuBar); this.add(statusPanel, BorderLayout.NORTH); this.add(jMineMatrixPanel); // 初始为初级 jMineMatrixPanel.addMineSweeperFrame(this); statusPanel.addMineSweeperFrame(this); setGameMode(new PreliminaryLevel()); this.addWindowListener(this); menuBar.setMineSweeperFrame(this); this.setResizable(false); this.pack(); } /** * 设置是否开始游戏 用于表示游戏的开始状态 * * @param b * 游戏开始状态 */ public void setIsFirstDig(boolean b) { isFirstDig = b; } /** * 得到当前游戏模式的雷的个数 * * @return 当前游戏模式雷的个数 */ public int getMineCount() { return this.jMineMatrixPanel.getMineCount(); } /** * 得到当前游戏模式的行数 * * @return 当前游戏模式的行数 */ public int getRowCount() { return this.jMineMatrixPanel.getRowCount(); } /** * 得到当前游戏模式的列数 * * @return 当前游戏模式的列数 */ public int getColumnCount() { return this.jMineMatrixPanel.getColumnCount(); } private boolean isFirstDig = true; private boolean button1Pressed; private boolean button3Pressed; private JMineBlock currentMineBlock; private JMineBlock[][] jMineMatrix; private MineMatrix mineMatrix = new MineMatrix(); /** * 游戏开始 激活状态栏的计时器 */ public void gameCommences() { // 开始计时 this.statusPanel.startsTimer(); // this.jMineMatrixPanel.resetGame();这是不用滴 } /** * 游戏失败 停止状态栏的计时器并将表情设置为哭泣 */ public void gameOver() {// 游戏结束 // 终止游戏 并把笑脸设置为哭泣 this.statusPanel.stopTimer(); this.statusPanel.setFaceCry(); } /** * 游戏完成 刷新雷区界面,全部标上红旗,停止计算器,把状态栏的雷数设置为0,跳出输入姓名对话框,并把姓名,等级,时间记录到集合框架 */ public void gameComplete() {// 完成游戏 // 终止游戏 并把笑脸设置为快乐 jMineMatrixPanel.refreshMineMatrixPanel(); this.statusPanel.stopTimer(); this.statusPanel.setLEDMineCountLeft(0); this.statusPanel.setFaceHappy(); // 判断是哪个等级的 int time = this.statusPanel.getLEDTime();// 得到计时器上的时间。 // 弹出对话框输入大名。 if (getGameModeName().equals("PreliminaryLevel")) {// 如果是完成初级 String name = getHeorName(); preliminaryLevelRanks.add(new Rank("初级", time, name)); } else if (getGameModeName().equals("IntermediateLevel")) {// 如果是完成级 String name = getHeorName(); intermediateLevelRanks.add(new Rank("级", time, name)); } else if (getGameModeName().equals("AdvancedLevel")) {// 如果是完成高级 String name = getHeorName(); advancedLevelRanks.add(new Rank("高级", time, name)); } } /** * 弹出一个对话框显示输入英雄名 * * @return 输入的姓名 */ private String getHeorName() { JEnterNameDialog d = new JEnterNameDialog(); d.showDialog(this); return d.getHeroName(); } /** * 得到当前游戏模式的名称 * 只有四种PreliminaryLevel,IntermediateLevel,AdvancedLevel,SelfDefinedLevel * * @return 模式名称字符串 */ private String getGameModeName() { String str[] = this.getGameMode().split("[.]"); return str[str.length - 1]; } /** * 在雷区上按下左键的操作,将状态置为惊讶 */ public void leftButtonPressedOnMineBlock() { this.statusPanel.setFaceSurprised(); } /** * 得到游戏模式,返回的是游戏模式的包名和类名 */ private String getGameMode() { return this.jMineMatrixPanel.getGameMode(); } /** * 左键在雷区上放开 将表情置为笑脸 */ public void leftButtonReleasedOnMineBlock() { // 在表情上放左键意味着重置游戏 在雷区放左键意味着挖雷 就是把状态栏里的表情变成微笑 this.statusPanel.setFaceSmile(); } /** * 设置游戏模式 * * @param s * 游戏模式对象 */ public void setGameMode(MineMatrixSettable s) {// 设置游戏模式 // 在游戏模式更改和Frame的构造函数都会调用这个方法 // 将雷区重置 this.statusPanel.setFaceSmile(); this.jMineMatrixPanel.setMineMatrix(s); // 将状态栏重置 // int mineCount = this.jMineMatrixPanel.getMineCount(); // // 初始化剩余雷数 // statusPanel.setLEDMineCountLeft(mineCount); // // 停止计时器 // statusPanel.stopTimer(); resetGame(); ...................... ................

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧