我按照书上的介绍,写了一个swing中画图的代码,但是运行时发现他paint()执行了
两次,请问这是为什么呢
另外public void paint(Graphics g)在这段代码中是在哪个地方被调用的,我感觉只
是被重写,没有被调用啊,之前提问有人跟我说这是因为此处的paint算是Jframe的
事件,和方法不一样,但是我还是不理解,代码如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Gtonew extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gtonew frame = new Gtonew();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Gtonew() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
public void paint(Graphics g){
g.drawRoundRect(300, 150, 50, 50, 100, 100);
System.out.println("wobeidiaoyongle");//此处打印没执行了2次
}
}