Java中的paintComponent()未被调用 [英] paintComponent() in Java is not being called

查看:461
本文介绍了Java中的paintComponent()未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一个简单的矩形,但我认为paintComponent方法没有被调用。
以下是使用main方法的类的代码:

I am trying to draw a simple rectangle but I think the paintComponent method is not getting called. Here is the code for the class with main method:

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

public class Painting  {       

    public static void main(String[] args) {
        JFrame jf;       
        jf = new JFrame("JUST DRAW A RECTANGLE");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setLayout(null);
        jf.setLocationRelativeTo(null);
        jf.setSize(600,600);       
        jf.setVisible(true);
        Mainting maint = new Mainting();
        jf.add(maint);        
     }    
}

以及带有paintComponent()的类

and the class with paintComponent()

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

public class Mainting extends JPanel {
    @Override  
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawRect(0, 0 , 200, 200);
        System.out.println("haha");
        g.setColor(Color.red);
    }
}

这里有什么问题,我无法弄清楚。 ..

What is the problem here, I cannot figure out...

推荐答案

虽然已经提供的答案可能导致矩形出现,但这种方法并不理想。此示例旨在显示更好的方法。请阅读代码中的注释以获取详细信息。

While the answers already provided might have resulted in the rectangle appearing, the approach was less than optimal. This example aims to show a better approach. Read the comments in the code for details.

请注意,应在EDT上启动Swing / AWT GUI。这留给读者练习。

Note that Swing/AWT GUIs should be started on the EDT. This is left as an exercise for the reader.

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

public class Painting {

    public static void main(String[] args) {
        JFrame jf = new JFrame("JUST DRAW A RECTANGLE");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // null layouts cause more problems than they solve. DO NOT USE!
        //jf.setLayout(null);
        jf.setLocationRelativeTo(null);
        /* if components return a sensible preferred size, 
        it's better to add them, then pack */
        //jf.setSize(600, 600);
        //jf.setVisible(true); //  as mentioned, this should be last
        Mainting maint = new Mainting();
        jf.add(maint);
        jf.pack(); // makes the GUI the size it NEEDS to be
        jf.setVisible(true);
    }
}

class Mainting extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED); 
        g.drawRect(10, 10, 200, 200);
        System.out.println("paintComponent called");
        /* This does nothing useful, since nothing is painted
        before the Graphics instance goes out of scope! */
        //g.setColor(Color.red); 
    }

    @Override
    public Dimension getPreferredSize() {
        // Provide hints to the layout manager! 
        return new Dimension(220, 220);
    }
}

这篇关于Java中的paintComponent()未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆