我有一个JPanel
,它有一个按钮,按下时会创建一个新的圆圈。但是,这并没有发生。一个新的圆圈不会出现,当我调整窗口大小时,现有圆圈会断开,按钮的克隆会出现在面板的另一侧,所有东西都会冻结。
class Panel extends JPanel {
private JButton button;
private Ellipse2D.Double[] circles;
Integer count;
public Panel() {
setup();
}
private void setup() {
count=new Integer(0);
circles=new Ellipse2D.Double[10];
button=new JButton(count.toString());
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
circles[count]=new
Ellipse2D.Double(10, 10, 100, 100);
count++;
button.setText(count.toString());
}
});
add(button);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
paintStuff(g);
}
private void paintStuff(Graphics g) {
Graphics2D g2=(Graphics2D) g;
g2.setPaint(Color.RED);
Ellipse2D.Double circle2=new Ellipse2D.Double(getWidth()/2-
getHeight()/4,
getHeight()/4, getHeight()/2, getHeight()/2);
g2.draw(circle2);
if (count!=0) {
for (Ellipse2D.Double circle: circles) {
g2.draw(circle);
}
}
}
}
public class Frame extends JFrame {
private Panel panel;
public Frame() {
panel=new Panel();
add(panel);
}
public static void main(String[] args) {
Frame frame=new Frame();
}
}
有什么问题,我应该怎么做才能解决它?