我正在構建Java應用程序。 GameView有一個包含多個PawnViews的BoardView(在這個例子中只有一個)。在JPanel上更改位置JLabel重繪
例子:
public class GameView extends Frame
{
public GameView()
{
AWTUtilities.setWindowOpaque(this, false);
this.setUndecorated(true);
this.setLayout(null);
this.setResizable(false);
this._boardview = new BoardView();
int x = 0;
int y = 0;
PawnView pv = new PawnView();
this._boardview.AddPawn(pv, 10, 10);
this._boardview.MovePawn(pv, 20, 10);
}
}
public class BoardView extends JPanel
{
public BoardView()
{
this.setOpaque(false);
RepaintManager.currentManager(null).setDoubleBufferingEnabled(true);
this.setLayout(null);
}
@Override
public void update(Graphics g)
{
paint(g);
}
public void AddPawn(PawnView pawnview, int x, int y)
{
this.add(pawnview);
pawnview.setLocation(x, y);
}
public void MovePawn(PawnView pawnview, int x, int y)
{
pawnview.setLocation(x, y);
//this.repaint();
}
}
public class PawnView extends JLabel
{
public PawnView()
{
this.setOpaque(false);
RepaintManager.currentManager(null).setDoubleBufferingEnabled(true);
this.setLayout(null);
}
}
起初一切看上去很不錯(不MovePawn):
http://dl.dropbox.com/u/7438271/1.png
當我打電話MovePawn它看起來像:
http://dl.dropbox.com/u/7438271/2.png
我試着請撥打this.revalidate()
,this.updateUI()
,this.repaint()
,this.paintImmediately()
以各種形式,但它們都使情況變得更糟:整個JPanel獲得白色背景。
我也嘗試覆蓋JPanel的paintComponent函數也沒有效果。
這隻發生在Mac OS X(完全更新)上,但我也遇到了在Windows中重新繪製的一些問題。
任何人都可以請幫忙嗎?
我認爲我們需要更多的信息能夠更好地幫助你。你能否儘可能簡化問題,甚至創建一個我們可以編譯和運行的最小代碼示例,它演示了你的問題,但是不包含與問題無關的多餘代碼,[sscce](http:// SSCCE)? – 2012-03-16 22:35:23