1
Paint ()
1 package hello; 2 Import Java. AWT. *; 3 Public class helloworld 4 {5 public static void main (string [] ARGs) throws exception 6 {7 new F (). launchframe (); 8} 9} 10 11 Class F extends frame12 {13 public void launchframe () 14 {15 setbounds (200,300,600,700); 16 setvisible (true ); 17} 18 public void paint (Graphics g) // frame is automatically called 19 {20 color C = G. getcolor (); // obtain the original color of 21 GB. setcolor (color. green); // 22g. filloval (50, 50, 30, 30); 23G. setcolor (color. orange); 24g. fillrect (90, 90, 30, 30); 25g. setcolor (c); // restore the original color 26} 27}
2
Mouselistener, mouseadapter (mouse adapter, adapter can prevent definition of meaningless methods, each listener has adapter) subclass, implement all methods, are empty
Point class
152
1 package hello; 2 Import Java. AWT. *; 3 Import Java. AWT. event. *; 4 Import Java. util. *; 5 public class helloworld 6 {7 public static void main (string [] ARGs) throws exception 8 {9 new F ("wori "); 10} 11} 12 13 class F extends frame 14 {15 arraylist Al = new arraylist (); // point where the mouse points are stored 16 public F (string S) 17 {18 super (s); 19 setlayout (null); 20 setbounds (200,300,600,700); 21 setvisible (true); 22 This. addmouselistener (New M (); // listen to mouse 23 24} 25 public void paint (Graphics g) 26 {27 iterator I = Al. iterator (); // 28 color C = G. getcolor (); 29G. setcolor (color. cyan); 30 While (I. hasnext () 31 {32 point P = (point) I. next (); 33g. filloval (P. x, p. y, 10, 10); 34} 35 36} 37 void addpoint (int A, int B) 38 {39 Al. add (new point (a, B); 40} 41 class M extends mouseadapter // mouseadapter is a subclass of mouselistener 42 {43 public void mousepressed (mouseevent E) 44 {45 f = (f) E. getsource (); // getsource () is the object 46 addpoint (E. getx (), E. gety (); // 47 F. repaint (); // refresh the image 48 49} 50} 51}
3
Windowevent windowlistener
1 package hello; 2 Import Java. AWT. *; 3 Import Java. AWT. event. *; 4 Import Java. util. *; 5 public class helloworld 6 {7 public static void main (string [] ARGs) throws exception 8 {9 frame F = new frame ("wocao"); 10 11 F. setlayout (null); 12 F. setbounds (300,300,300,300); 13 F. addwindowlistener (New WL (); 14 F. setvisible (true); 15} 16 17} 18 19 class WL extends windowadapter20 {21 public void windowclosing (invalid wevent e) 22 {23 24 (FRAME) E. getsource ()). setvisible (false); // note that () must be set (FRAME) E. getsource (). setvisible (false); not *** 25 system. exit (0); // 0 indicates normal exit 26} 27}
4
Keylistener
1 package hello; 2 Import Java. AWT. *; 3 Import Java. AWT. event. *; 4 Import Java. util. *; 5 public class helloworld 6 {7 public static void main (string [] ARGs) throws exception 8 {9 frame F = new frame ("wocao"); 10 11 F. addkeylistener (New kl (); // textfield cannot be added, and input is added to the text box. This is not a frame event 12 F. addwindowlistener (New WL (); 13 F. pack (); 14 F. setvisible (true); 15} 16 17} 18 19 20 class KL extends keyadapter21 {22 public void keytyped (keyevent e) 23 {24 int c = E. getkeycode (); 25 26 system. out. println (C + "is typed"); 27 28} 29} 30 31 32 class WL extends windowadapter33 {34 public void windowclosing (invalid wevent e) 35 {36 (FRAME) E. getsource ()). setvisible (false); 37 system. exit (0); 38} 39}