[code="java"]
import javax.swing.*;
import java.awt.*;
public class DrawMessage extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("DrawMessage");
frame.getContentPane().add(new DrawMessage());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Welcome to Java", 40, 40);
}
}
[/code]
frame.getContentPane().add(new DrawMessage());
这句我不能理解,这里的new DrawMessage(),怎么可以在类中创建自身的对象呢?!