java - 在 JPanel 重绘上更改位置 JLabel

标签 java swing jpanel jlabel repaint

我正在构建一个 Java 应用程序。 GameView 有一个 BoardView,其中包含多个 PawnView(在示例中只有一个)。

例子:

    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 中也遇到了一些重绘问题。

有人可以帮忙吗?

最佳答案

您的代码不可运行,

1) 不要将 AWT Frame 与 Swing JComponents 混合使用,不确定(从未测试过)但我认为 AWTUtilities仅针对 Swing JComponent,则

public class GameView extends Frame

可能是

public class GameView extends JFrame

2)

this._boardview = new BoardView();

必须是

BoardView _boardview = new BoardView();

3) 为什么有代码行,

RepaintManager.currentManager(null).setDoubleBufferingEnabled(true);

我看到用于打印的代码 where is for faster printing turn double buffering off, but value currentManager(null), simply doesn't make any sense

4) 不要使用public void update(Graphics g),这个方法在API内部使用,使用paintComponent代替

5) 使用 JComponents 移动使用 Swing Timer调用 repaint()

6) JLabel默认透明移除 this.setOpaque(false);

7) JLabel还没有实现任何LayoutManager , 删除 this.setLayout(null);

关于java - 在 JPanel 重绘上更改位置 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9743529/

相关文章:

java - 程序不会打开新窗口

java - 数据结构推荐

java - setLocationRelativeTo() 一个组件,然后稍微移动它

java - 我的 JTextPane 如何显示多种字体?

java - 图形程序适用于 Windows,但不适用于 Mac

javascript - 在响应中发回 JSON 对象(不是字符串版本)

java - 使用 JBOSS JNDI 数据源时,Quartz 在 BLOB 上遇到异常

java - JLabel 位置不起作用,无法选择位置

java - 在 JScrollPane 内的 JPanel 内包装 JLabel

java - JPanel不会出现在JTabbedPane中?