我先把代码帖出来   已经把不必要和这个问题没有关心的地方全部删除了已经是最方便大家看的了   想要解决的问题是   在下一个棋子的时候    屏幕都又重新刷新一遍  怎么样才能避免这样的问题发生?我期望的结果是  在下一个棋子的时候   棋盘和其他棋子不刷新(棋子不刷新已经解决了)只是屏幕上多了一个棋子   研究了2天   没解决   快疯了都还有就是现在棋盘把棋子给覆盖了   代码如下:主frame的代码package src.com.http.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;public class FristMain extends JFrame implements MouseListener{ private Font font = new Font("ËÎÌå", 0, 12);
private Dimension  dm ;
private JPanel panelMain =(JPanel)this.getContentPane();   
private JPanel panel = new JPanel(new   FlowLayout());

private final int Width = 800;
private final int Height = 770;

private final int cellwidth = 46;
private final int cellheigth = 44; private boolean flagExcte = true;
private Borad borad = new Borad(Color.PINK,48 ,26 ,690 ,660 );

public FristMain() {
// TODO Auto-generated constructor stub
this.setTitle("FristMain");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setFont(font);
dm = this.getToolkit().getScreenSize();
this.setBounds((int)((dm.getWidth() - Width)/2), (int)((dm.getHeight() - Height)/2) , Width, Height); panelMain.setLayout( new BorderLayout());
panelMain.setBackground(Color.WHITE);

panel.add(borad);
panel.addMouseListener(this); panelMain.add(panel);
this.setResizable(false);
this.setVisible(true);
}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
new FristMain();
}

public void update(Graphics g){
this.paint(g);
} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent arg0) {
int mapx =0;
int mapy =0;

mapx = (arg0.getX()+48)%cellwidth;
if(mapx != 0 && mapx <= (cellwidth /2)){
mapx = (arg0.getX()+48)/cellwidth * cellwidth;
}else if ((cellwidth /2) < mapx && mapx !=cellwidth){
mapx = ((arg0.getX()+48)/cellwidth+1) * cellwidth;
}else{
mapx = (arg0.getX()+48);
}

mapy = (arg0.getY()+26)%cellheigth;
if(mapy != 0 && mapy <= (cellheigth /2)){
mapy = (arg0.getY()+26)/cellheigth * cellheigth;
}else if ((cellheigth /2) < mapy && mapy !=cellheigth){
mapy = ((arg0.getY()+26)/cellheigth+1) * cellheigth;
}else{
mapy = (arg0.getY()+26);
}

Chess chess = new Chess();
if(flagExcte){
chess.setColor(Color.PINK);
flagExcte = false;
}else{
chess.setColor(Color.GRAY);
flagExcte = true;
}
chess.setX(mapx-42);
chess.setY(mapy-18);
chess.setVisible(true);
panel.invalidate();
panel.add(chess);
panel.validate();

this.repaint();
}

public void paintComponent(Graphics g){
   super.paintComponents(g);
}

}
棋盘的代码:package src.com.http.swing;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.HashMap;
import javax.swing.*;import java.awt.event.*;
public class Borad extends JPanel{ private Color color ;
private final int cellwidth = 46;
private final int cellheigth = 44;
private int width;
private int height;
private int x;
private int y;

public Borad (Color color , int x ,int y, int width , int height  ){
this.color = color;
     this.width = width ;  
     this.height = height ;  
     this.x = x ;  
     this.y = y ;
     this.setLayout(null);
    
    }    public void paintComponent(Graphics g){

    this.setBounds(x, y, width+1, height+1);
             
     g.setColor(color);
     for(int i = 0 ; i < 15*cellwidth ; ){
     for(int j = 0 ; j < 15*cellheigth ; ){
     g.drawRect(i,j,cellwidth,cellheigth);
     j = j + cellheigth;
     }
     i = i + cellwidth;
     } 
    }
 
 public void update(Graphics g){
paintComponent(g);
 }
} 棋子的代码package src.com.http.swing;import java.awt.*;
import java.util.HashMap;import javax.swing.*;
public class Chess extends JComponent{

private Color color ;
private int x;
private int y;
private boolean  chessState ;

public Chess(){

}

    public Chess(Color color , int x , int y ){
     this.color = color;
     this.x  = x-20;
     this.y = y-20;  
    }
    
    public Chess( Color color){
     this.color = color;
    }
    
    public void paintComponent(Graphics g){
    
     this.setBounds(x, y, 40, 40);
     g.setColor(color);
     g.fillOval(0, 0, 40, 40);
    }
    
    public void setStater(boolean chessState){
     this.chessState = chessState;
    }
    
    public boolean getState(){
     return this.chessState;
    }
    
    public void setColor(Color color){
     this.color = color;
    }
    
    public Color getColor(){
     return this.color;
    }
      
    public int getX(){
     return this.x;
    }
    
    public int getY(){
     return this.y;
    }
    
    public void setX( int x){
     this.x= x-20;
    }
    
    public void setY( int y){
     this.y= y-20;
    }
}这些代码复制了以后就可以运行   大家复制了后帮我看看怎么解决。

解决方案 »

  1.   

    我想可以事先把棋盘填满棋子(透明的) 下棋的时候再改变点中棋子的颜色 就不用刷新panel
      

  2.   

    我以前也是那么做的 
    你没看见我都给棋子里加了一个 chessState    属性  
    就是为了判断这个是不是要显示的
    我一开始建立了一个 chess[16][16]得数组但是也还是出现这个问题你说的是我最先使用的方法  但是还是出现这个问题所以我才改成这样得   没想到还是没解决你在想想还有没有其他什么办法这个问题是由于你向jpanelt 添加组件的时候  他在加载新的组建的时候都重新绘制一遍 自己所以有抖动现象
      

  3.   

    请问  updateUI() 这个东西里面写什么东西?
    paintComponent(g)吗?这个方法是写在  主frame里 ?
      

  4.   

    2楼的  如果把setVisible(false)  设置成 setVisible(true)还是会触发jpanel 刷新事件的现在的问题是  能不能让棋盘 和 棋子不再一层然后只刷新棋子那层棋盘那层就不刷新了能实现吗  ?  能的话   怎么实现我尝试过了  没成功 
      

  5.   

    updateUI() 我加在了 主frame类里没好用  还有没有其他方法?
      

  6.   

    组件可能是整体刷新 试试重写update方法
    里面用一个Graphics对象来画棋盘和棋子
    如果愿意 还可以用双缓存
      

  7.   

    组件可能是整体刷新 试试重写update方法
    里面用一个Graphics对象来画棋盘和棋子
    如果愿意 还可以用双缓存
      

  8.   

    对于JFrame这种轻量级组件 则重写paint方法
    public void paint(Graphics g) {
        if (memoryGraphics != null) {
            borad.drawBoradImage(memoryGraphics);
            if (chess != null) {
                chess.drawChessImage(memoryGraphics);
            }
        }
        g.drawImage(memoryImage, 0, 0, this);
    }
    再在mouseReleased事件方法中repaint