paintComponent() 函数调用
paintComponent() Function Calling
这是我的代码,它运行完美并在 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);
}
}
大致如此。当您使用 UI 组件时,将启动一个 UI 线程来管理不同类型的事件。在这些事件中,有一些与您面板的可见性有关,例如,当主机图形系统希望您的面板可见时,它会向您的应用程序发送一个事件以请求绘制面板,然后 UI 线程回调相应的 paint()
方法,该方法又调用 paintComponent()
.
您实际上并没有显式调用该方法。程序自行决定何时需要调用该方法。它通常发生在发生变化时。
如果您偶然想强制它重新绘制,只需在您的视图 class(或该对象)中调用方法 repaint()
。
同样在你的 paintComponent()
方法中,确保你做的第一件事是调用 super.paintComponent()
.
这是我的代码,它运行完美并在 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);
}
}
大致如此。当您使用 UI 组件时,将启动一个 UI 线程来管理不同类型的事件。在这些事件中,有一些与您面板的可见性有关,例如,当主机图形系统希望您的面板可见时,它会向您的应用程序发送一个事件以请求绘制面板,然后 UI 线程回调相应的 paint()
方法,该方法又调用 paintComponent()
.
您实际上并没有显式调用该方法。程序自行决定何时需要调用该方法。它通常发生在发生变化时。
如果您偶然想强制它重新绘制,只需在您的视图 class(或该对象)中调用方法 repaint()
。
同样在你的 paintComponent()
方法中,确保你做的第一件事是调用 super.paintComponent()
.