【发布时间】:2019-02-04 21:02:11
【问题描述】:
我实际上是在学习 Java 中的图形。我无法理解这个JFrame 对象如何调用MyDrawpanel 中的paintComponent 方法,该方法扩展了JPanel。
frame.repaint() 再次调用paintComponent 但如何?为什么我不能像drawPanel.repaint()一样使用MyDrawPanel的drawPanel对象;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui3C implements ActionListener
{
JFrame frame;
public static void main(String[] args)
{
SimpleGui3C gui = new SimpleGui3C();
gui.go();
}
public void go()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Change Colors");
button.addActionListener(this);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
frame.repaint();
}
}
class MyDrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.fillRect(0,0,this.getWidth(), this.getHeight());
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
Color randomColor = new Color(red,green,blue);
g.setColor(randomColor);
g.fillOval(70,70,100,100);
}
}
【问题讨论】:
-
调用
repaint()告诉绘制管理器绘制组件及其拥有的所有子组件。它是绘画“链”的一部分。 -
查看:Lesson: Performing Custom Painting:Swing 图形入门教程。 AND Painting in AWT and Swing:Swing 图形高级教程
-
但是框架对象如何调用repaint()
-
简短的回答是“这很复杂”,有时,您需要接受一点“黑魔法”,直到您获得足够的 API 经验。话虽如此,您应该阅读Painting in AWT and Swing 以更好地了解绘画系统的工作原理
-
"但是框架对象如何调用 repaint()" - 并且绘制请求是由框架响应
setSize和setVisible。并非所有方法都这样做,但有些方法必须触发布局/绘制通道