1

有人会知道为什么不调用我的 PF 类的函数 paintComponent 吗?

public class Fenetre
{

    private JFrame jframe;

    public Fenetre()
    {
        jframe = new JFrame(" works");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setLayout(new BorderLayout());
        jframe.setLocationRelativeTo(null);
        jframe.setContentPane(new P());
        jframe.pack();
        jframe.setVisible(true);
    }

    public class P extends JComponent
    {
        public P()
        {
            this.setPreferredSize(new Dimension(WI,HI));
            this.add(new PF());
        }

        public class PA extends JComponent
        {
          @Override protected void paintComponent(Graphics g) {

                DrawX(g);
                DrawY(g);
            }

            public void DrawX(Graphics g)
            {
              \* .. */
            }

            public void DrawY(Graphics g)
            {
               \* ..*/
            }

        }

        public class PF extends PA
        {

            @Override protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                drawS(g);
                drawT(g);
            }

            public void drawS(Graphics g)
            { \* .. */
             }

            public void drawT(Graphics g)
            {
               \* ... */
            }

        }

当我在 PF 构造函数中设置 setSize 函数时,它可以工作,但是 setPreferredSize 也可以工作,但不允许类 PF 可调整大小,也就是说,如果我调整 Jframe 的大小,PF 绘制不会调整大小..

希望有人可以帮助我,谢谢。

4

1 回答 1

3

可能的问题可能与PA没有PF首选大小(默认为 0x0)以及JComponent默认情况下没有布局管理器的事实有关

绘制子系统已确定不需要绘制子组件,因为从技术上讲,它们在屏幕上不可见

首先设置布局管理器并P覆盖getPreferredSize.PA

It is very important that you alway call super.paintComponent when wever your override the method. This is doubly important when you are dealing with transparent components like JComponent as this will introduce very nasty paint artifacts if you don't

于 2013-10-22T23:20:06.640 回答