出错,不知道是哪里?????
找了很久,始终不知道哪里出错了.出错代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class DrawingPanel extends JFrame implements ActionListener {
JButton btnSanJiao,btnYuan,btnJuXing; //定义三个按钮,三角形,圆,矩形
JPanel pan[]; //定义面版,用来画出上面的三个形状
int count=1; //统计已画出的图形数量
JPanel jpTop,jpFloor; //使用全局面版
public DrawingPanel() {
JPanel [] pan=new JPanel[9];
setTitle("DrawingPanel");
setSize(400,550);
btnSanJiao=new JButton("三角形");
btnYuan=new JButton("圆形");
btnJuXing=new JButton("矩形");
Container cp=this.getContentPane();
JPanel jpTop=new JPanel();
JPanel jpFloor=new JPanel();
jpFloor.setLayout(new GridLayout(9,9));
cp.add(jpTop,BorderLayout.NORTH);
cp.add(jpFloor,BorderLayout.CENTER);
jpTop.add(btnSanJiao);
jpTop.add(btnYuan);
jpTop.add(btnJuXing);
btnSanJiao.addActionListener(this);
btnYuan.addActionListener(this);
btnJuXing.addActionListener(this);
//这里为什么总是运行时出错呢?哪里有问题????
for(count=1;count<10;count++){
pan[count]=new DrawPoygon();
jpFloor.add(pan[count]);
}
}
//ActionListener 实现部分
public void actionPerformed(ActionEvent e){
if(count<=9){
if(e.getSource()==btnSanJiao){
System.out.println ("btnSanJiao");
//pan [count]=new DrawPoygon();
//jpFloor.add(pan[count]);
}
if(e.getSource()==btnYuan){
System.out.println ("btnYuan");
}
if (e.getSource()==btnJuXing){
System.out.println ("btnJuXing");
}
}
}
//画三角形
public class DrawPoygon extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
int xPoints[]={20,100,40};
int yPoints[]={20,20,40};
g.setColor(Color.cyan);
g.fillPolygon(xPoints,yPoints,3);
count++;
}
}
public static void main(String[] args) {
// Create application frame.
JFrame frame = new DrawingPanel();
// Show frame
frame.setVisible(true);
}
}