我有三个按钮,矩形,圆形,三角形,我希望点击哪个按钮,就按顺序添加到中间的网格中,可是就是添加不进去,请高手解答一下import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;class PaintShapePanel extends JPanel {
// 三个按钮
private JButton jbRectangle;
private JButton jbEllipse;
private JButton jbPolygon; // 三个面板
private JPanel northPanel;
private JPanel centerPanel;
private JPanel southPanel; private int count;//计数器
private JPanel[] jpDrawShape; //中间面板的9个面板数组 public PaintShapePanel() {
setLayout(new BorderLayout()); // 设置总面板的边框布局
// 三个面板
northPanel = new JPanel();
centerPanel = new JPanel();
southPanel = new JPanel(); // 三个面板的布局
northPanel.setLayout(new GridLayout(1, 3));
southPanel.setLayout(new GridLayout(1, 1));
centerPanel.setLayout(new GridLayout(3, 3)); // 顶部面板的三个按钮
jbRectangle = new JButton(ShapeName.SHAPE_RECTANGLE);
jbEllipse = new JButton(ShapeName.SHAPE_ELLIPSE);
jbPolygon = new JButton(ShapeName.SHAPE_POLYGON);
northPanel.add(jbRectangle);
northPanel.add(jbEllipse);
northPanel.add(jbPolygon);

// 注册监听器
DrawAction da = new DrawAction(); // 实例化监听器对象
jbRectangle.addActionListener(da);
jbEllipse.addActionListener(da);
jbPolygon.addActionListener(da); // 底部面板的一个按钮
JButton jbClear = new JButton("Clear 清除");
southPanel.add(jbClear);
jbClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 清除按键的功能实现,暂未实现
}
}); // 中间面板的九个面板
count = 0;
jpDrawShape = new JPanel[9];
for (int i = 0; i < jpDrawShape.length; i++) {
jpDrawShape[i] = new JPanel(); // 实例面板对象
jpDrawShape[i].setBorder(BorderFactory
.createLineBorder(Color.GREEN));
centerPanel.add(jpDrawShape[i]);
} //添加三个面板到总面板
add(northPanel, BorderLayout.NORTH);
add(southPanel, BorderLayout.SOUTH);
add(centerPanel, BorderLayout.CENTER);
} //三个按钮的监听器对象
private class DrawAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
if (count < 9) {
if (jb == jbRectangle) {
jpDrawShape[count].add(new DrawPanel(
ShapeName.SHAPE_RECTANGLE));
} else if (jb == jbEllipse) { } else if (jb == jbPolygon) { }
jpDrawShape[count].repaint();
}
count++;
}
} class DrawPanel extends JPanel {
private String shapeName;
Shape shape = null;

public DrawPanel(String shapeName) {
this.shapeName = shapeName;
setVisible(true);
} public Shape getShape(int width, int height) {
// 获取面板的宽高

if (shapeName.equals(ShapeName.SHAPE_RECTANGLE)) {
shape = new Rectangle2D.Double(20, 20, width - 20, height - 20);
} else if (shapeName.equals(ShapeName.SHAPE_ELLIPSE)) {
Dimension dimension = new Dimension();
dimension.setSize(width - 20, height - 20);
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(new Point2D.Double(20, 20), dimension);
shape = ellipse;
} else if (shapeName.equals(ShapeName.SHAPE_POLYGON)) { }
return shape;
} @Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fill(getShape(100,100));
}
}
}class ShapeName {
// 三个静态常量代表三种图形
public static final String SHAPE_RECTANGLE = "Rectangle";
public static final String SHAPE_ELLIPSE = "Ellipse";
public static final String SHAPE_POLYGON = "Polygon";
}class PaintShapeFrame extends JFrame {
public PaintShapeFrame() {
add(new PaintShapePanel());
setSize(new Dimension(700, 500));
// add(new PaintShapePanel.DrawPanel(ShapeName.SHAPE_RECTANGLE));
//command.MyFrameUitl.init(this, new Dimension(700, 500), "绘图", null,true);
setVisible(true);
}
}public class PaintShapeTest {
public static void main(String[] args) {
new PaintShapeFrame();
}
}