【问题标题】:paintComponent() Function CallingpaintComponent() 函数调用
【发布时间】:2016-04-09 11:18:06
【问题描述】:

这是我的代码,它工作完美,在JFrame 中绘制形状。每个方法都按其名称调用,在我的程序中我没有调用任何 paintComponent() 方法。那么paintComponent()方法的调用方法在哪里呢?

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

public class drawings extends JPanel {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame();
        frame.setTitle("Shapes");
        frame.setVisible(true);
        frame.setBounds(150, 10, 1000, 700);
        frame.setResizable(false);
        frame.add(new drawings());
    }
    public void paintComponent(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(20, 10, 200, 100);
        g.setColor(Color.magenta);
        g.fill3DRect(230, 10, 200, 100, false);
    }
}

【问题讨论】:

标签: java swing graphics jframe jpanel


【解决方案1】:

大概。当您使用 UI 组件时,会启动一个 UI 线程来管理不同类型的事件。在这些事件中,有一些与您的面板的可见性有关,例如,当主机图形系统希望您的面板可见时,它会向您的应用程序发送一个事件以请求绘制面板,然后是 UI 线程回调适当的paint() 方法,该方法又调用paintComponent()

【讨论】:

    【解决方案2】:

    您实际上并没有显式调用该方法。程序自行决定何时需要调用该方法。它通常发生在发生变化时。

    如果您想强制它重新绘制,只需在您的视图类(或该对象上)调用repaint() 方法。

    同样在您的paintComponent() 方法中,确保您首先调用super.paintComponent()

    【讨论】:

      猜你喜欢
      • 2022-12-17
      • 1970-01-01
      • 2012-05-07
      • 2017-01-03
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      相关资源
      最近更新 更多