简体   繁体   English

Swing-未调用paintComponent方法

[英]Swing - paintComponent method not being called

i simply implemented class that inherits JPanel like below 我只是实现了继承JPanel的类,如下所示

public class Orpanel extends JPanel {

....
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(tpaint);
        g2d.fill(rect); 
    }
....
}

class Orpanel is loading image and adjusting it's own size. Orpanel类正在加载图像并调整其自身大小。

here's question. 这是问题。

Calling JFrame's setContentpane(Orpanel's instance) makes it works fine but when i attach Orpanel to JFrame calling add() method instead of setContentpane (i know setcontentpane is not meaning attach.. anyway), it dosen't work. 调用JFrame的setContentpane(Orpanel的实例)使其工作正常,但是当我将Orpanel附加到JFrame时,调用add()方法而不是setContentpane(我知道setcontentpane并不意味着attach ..反正),它就无法正常工作。

finally figured out when i used add() method, Component that was added to JFrame doesn't call paintComponent() method. 最终弄清楚了当我使用add()方法时,添加到JFrame的Component不会调用paintComponent()方法。 even though i call repaint() method manually, still paintComponent() method is not called. 即使我手动调用repaint()方法,仍然不会调用paintComponent()方法。

did i miss something? 我错过了什么? any help will be appreciated! 任何帮助将不胜感激!

thx in advance. 提前。 Jaeyong shin. 海永信


i added extra code. 我添加了额外的代码。

public Test(OwPanel op) 
{
    super();
    Dimension monitor = Toolkit.getDefaultToolkit().getScreenSize();
    op.setBackground(Color.white);
    this.setBackground(Color.white);        
    this.setBounds(monitor.width / 2 - 200 , monitor.height / 2 - 200, 400, 400);       
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setTitle("test");      
    this.setLayout(null);
    this.getContentPane().add(op);
    //this.setContentPane(op);
    this.setVisible(true);
    this.validate();
}

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            OwPanel op = new OwPanel("d:\\java_workplace\\img\\img1.jpg", 100, 100);
            OwLabel ol = new OwLabel("d:\\java_workplace\\img\\img2.jpg", 300, 50);
            Test tst =  new Test(op);
            tst.add(ol);
        }
    });

still doesn't work if setContentpane() method replaced to getContentpane().add(). 如果将setContentpane()方法替换为getContentpane()。add(),仍然无法正常工作。 don't be confused. 不要困惑。 Owpanel and Orpanel is same :) Owpanel和Orpanel相同:)

In your sample code, I see you have chosen NOT to use a LayoutManager, that's a very bad idea, but anyway, sicne you go this way, I see one reason for your Orpanel.paintComponent() not being called: it is probably not visible inside the frame! 在您的示例代码中,我看到您选择不使用LayoutManager,这是一个非常糟糕的主意,但是无论如何,按照这种方式,我看到您的Orpanel.paintComponent()不被调用的一个原因:可能不是在框架内可见!

If you have no LayoutManager , then you must explicitly set the size and location (through setBounds() ) of all components you add to the frame. 如果没有LayoutManager ,则必须(通过setBounds() )显式设置添加到框架的所有组件的大小和位置。

It is likely you didn't do it, hence the size of Orpanel instance is probably 0 hence it never gets painted. 您可能没有这样做,因此Orpanel实例的大小可能为0,因此它永远不会被绘制。

Sounds like you're just using the wrong methods. 听起来您使用的方法错误。 You should be doing this when adding a panel to a frame: 将面板添加到框架时,您应该这样做:

frame.getContentPane().add(panel) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM