JPanel drawingArea = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g); Rectangle rect;
for (int i = 0; i < objects.size(); i++) {
rect = (Rectangle)objects.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
};这是不是匿名内部类??当调用方法drawingArea.repaint()时,自动调用此内部的方法吧?这样写比直接写成public void paintComponent(Graphics g)有什么好处?还有为什么要写成protected形式?
protected void paintComponent(Graphics g) {
super.paintComponent(g); Rectangle rect;
for (int i = 0; i < objects.size(); i++) {
rect = (Rectangle)objects.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
};这是不是匿名内部类??当调用方法drawingArea.repaint()时,自动调用此内部的方法吧?这样写比直接写成public void paintComponent(Graphics g)有什么好处?还有为什么要写成protected形式?
/** @Override */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < objects.size(); i++) {
.....
}
}....
JPanel drawingArea = new MyPanel();
谢谢您了!