在调整JFrame大小时重新绘制JPanel,尽管它没有被遮盖或调整大小

时间:2014-10-24 14:53:23

标签: java swing paintcomponent jsplitpane

我正在测试一个游戏即时写作,当我发现JSplitPane中的一个JPanel在重新调整JFrame时被重新绘制,即使它的区域没有被遮挡,也没有调整大小。

虽然逻辑(更具体地说,计数器)不应该放在paintComponent方法中,但它确实会引发潜在的严重问题。

有没有人遇到/注意到这种行为?我读过一种防止重绘的方法是使用RepaintManager.markCompletlyClean(JComponent)。除RepaintManager之外的任何建议/解决方法?这是一个错误吗?

见下文:

import java.awt.*;
import javax.swing.*;

class test
{
    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame jframe = new JFrame();
                jframe.setLocation(100,100);
                jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel jpanel1 = new JPanel()
                {
                    final static long serialVersionUID = 0;

                    int int_repaints = 0;

                    public void paintComponent(Graphics graphics)
                    {
                        System.out.println("repainting jpanel1=" + (int_repaints++));
                    }

                    public Dimension getPreferredSize()
                    {
                        return new Dimension(300,300);
                    }
                };

                JPanel jpanel2 = new JPanel()
                {
                    final static long serialVersionUID = 0;

                    int int_repaints = 0;

                    public void paintComponent(Graphics graphics)
                    {
                        System.out.println("repainting jpanel2=" + (int_repaints++));
                    }

                    public Dimension getPreferredSize()
                    {
                        return new Dimension(300,300);
                    }
                };

                JSplitPane jsplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jpanel1,jpanel2);

                jframe.setContentPane(jsplitPane);
                jframe.pack();
                jframe.setVisible(true);
            }
        });
    }
}

0 个答案:

没有答案