為什麼太陽和小孩畫出來後不能清除了呢?想了N久,只怪自己沒學好GUI,後來有人回答了,

加上jp1.repaint();jp2.repaint();就OK!那麼

 

1 import java.awt.*;
2  import javax.swing.*;
3  import java.awt.event.*;
4
5  public class SunAndChild extends JFrame {
6 MyPanel mp=null;
7 /**
8 * @param args
9 */
10 public static void main(String[] args) {
11 // TODO Auto-generated method stub
12 SunAndChild sac=new SunAndChild();
13 }
14
15 public SunAndChild(){
16 mp=new MyPanel();
17 this.add(mp);
18 this.setTitle("太阳和小孩");
19 this.setSize(600,600);
20 this.setLocation(300,100);
21 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22 this.setResizable(false);
23 this.setVisible(true);
24 }
25 }
26
27 class MyPanel extends JPanel implements ActionListener{
28 JPanel jp1,jp2,jp3,jp4;
29 JButton jb1,jb2,jb3,jb4;
30
31 public MyPanel(){
32 jp1=new JPanel();
33 jp2=new JPanel();
34 jp3=new JPanel();
35 jp4=new JPanel();
36
37 jb1=new JButton("太阳");
38 //注册监听
39 jb1.addActionListener(this);
40 jb2=new JButton("小孩");
41 //注册监听
42 jb2.addActionListener(this);
43 jb3=new JButton("清除太阳");
44 //注册监听
45 jb3.addActionListener(this);
46
47 jb4=new JButton("清除小孩");
48 //注册监听
49 jb4.addActionListener(this);
50
51
52 jp3.add(jb1);
53 jp3.add(jb3);
54
55 jp4.add(jb2);
56 jp4.add(jb4);
57 jp1.setLayout(new BorderLayout());
58 jp1.add(jp3,BorderLayout.SOUTH);
59
60 jp2.setLayout(new BorderLayout());
61 jp2.add(jp4, BorderLayout.SOUTH);
62
63 this.setLayout(new GridLayout(1,2));
64 this.add(jp1);
65 this.add(jp2);
66
67 jp1.setBackground(Color.BLACK);
68 jp2.setBackground(Color.BLUE);
69 }
70
71
72 public void drawSun(){
73 Graphics g=this.getGraphics();
74 g.setColor(Color.RED);
75 g.fillOval(100, 100, 100, 100);
76 //上面的线
77 g.drawLine(150, 100, 150, 50);
78 //下面的线
79 g.drawLine(150, 200, 150, 250);
80 //左面的线
81 g.drawLine(100,150,50,150);
82 //右面的线
83 g.drawLine(200,150, 250, 150);
84
85 }
86 public void drawChild(){
87 Graphics g=this.getGraphics();
88 g.setColor(Color.YELLOW);
89 //
90 g.fillOval(400,100,100,100);
91 g.setColor(Color.BLUE);
92 //左眼
93 g.fillRect(425, 125, 5, 5);
94 //右眼
95 g.fillRect(475, 125, 5, 5);
96 //鼻子
97 g.drawLine(450, 125, 450, 150);
98 //
99 g.drawLine(425, 175, 475, 175);
100 //身体
101 //g.drawRect(x, y, width, height);
102 //左胳膊
103 //g.drawRect(x, y, width, height);
104 //右胳膊
105 //g.drawRect(x, y, width, height);
106 //左腿
107 //g.drawRect(x, y, width, height);
108 //右腿
109 //g.drawRect(x, y, width, height);
110 }
111
112 public void actionPerformed(ActionEvent arg0) {
113 // TODO Auto-generated method stub
114 if(arg0.getSource()==jb1)
115 {
116 this.drawSun();
117 }
118 if(arg0.getSource()==jb2)
119 {
120 this.drawChild();
121 }
122 if(arg0.getSource()==jb3)
123 {
124 //jp1.setBackground(Color.black);
125 jp1.repaint(); ////////////////////////////////////////////////////////////////////////////////////////
126 }
127 if(arg0.getSource()==jb4)
128 {
129 //jp2.setBackground(Color.BLUE);
130 jp2.repaint(); ///////////////////////////////////////////////////////////////////////////////////////
131 }
132
133
134 }
135 }
136
137

 

 

 究竟repaint()執行了什麼操作?我就納悶了:接著看了這裡的解釋,更加迷茫了http://blog.csdn.net/yubofighting/archive/2008/09/29/2997676.aspx

既然調用了原來的paint()方法,那應該是重畫太陽和小孩才對呀,為什麼清空了呢?

posted on 2010-06-03 19:39  KuSiuloong  阅读(179)  评论(0编辑  收藏  举报