Summary of Java events
Huang Yusheng, 11,14
It is a bit unclear that I have learned such a long time, I think there is no summary. Let me know the Java event clearer with some of my small examples. Java events are nothing more than keyboard events, mouse events, buttons and other events. Professional points can be divided into semantic events (buttons, etc.) and low-level events (keyboard events, mouse events); I will summarize the following: 1, mouse event: Mouse button, it will call three listener methods: mousepressed, Mouseeleased, mouseclicked.
Mouse events offer mousepressed, mouseclicked, mousedragged, mouseentered, mouseexited, mouseup, mousedown, mousedrag and other incidents. Here is an example of a mouse:
Import java.awt. *;
IMPORT JAVA.Applet.Applet;
Public Class CountClick Extends Applet
{INT CurrentMarks = 0;
INT A, B;
Public Boolean MouseDown (Event EVT, INT X, INT Y) // Mouse Pressing
{CurrentMarks ; // Accounting
Repaint (); // Refresh the floor version
A = x; // Get the horizontal coordinates of the mouse
B = Y; / / Get the shade of the mouse
Return True;
}
Public void Paint (Graphics G)
{g.drawstring (" currentmarks, 10, 10); // Print Press the next number
g.drawstring (" a, 10, 30); // Print the abscissa of the mouse
g.drawstring (" b, 10, 20); // Print the morality of the mouse
}
}
//
2, keyboard event: If we want to use the keyboard to get input information, you must handle the keyboard event. We can be implemented in the CONPONENT's KeyDown. The following example:
Import java.applet.applet; import java.awt. *;
{Char presskey;
Public Boolean KeyDown (Event EVT, INT Key)
{PressKey = (char) key; // Record the key you pressed
REPAINT (); RETURN TRUE;
}
Public void Paint (Graphics G)
{G.drawstring (PressKey, 10, 10);} // Print the key value you pressed
}
3, an ammonium button, etc.: This is more content, and it is a simple introduction through an example.
//*******year 2004,
11
month
8
day
************** //
// ******* Bean children learn more about ****** //
// ******* Note the use of the repaint () method ****** //
// ** With the Java data hidden with Java, there is a close one step. *** ///
//*******keep it up. Gogogogo ***** //
Import java.awt. *;
IMPORT JAVA.Applet.Applet;
Import java.awt.event. *; public class awtAditionTest2 Extends Applet Implements ItemListener, ActionListener
/ / Implement itemListener, ActionListener interface
{
INT NUM = 5;
Choice ch = new choice ();
Button One = New Button ("one");
Button Two = New Button ("Two");
Button Three = New Button ("Three");
Image aa [];
Image a;
Public void init ()
{
AA = new image [NUM];
For (int i = 0; i { AA [I] = GetImage (GetDocumentBase (), "a" (i 1) ". jpg"); } Num = 4; // give an initial value This.SetBackground (Color.White); CH. AddItem ("a1.jpg"); CH. AddItem ("a2.jpg"); CH. AddItem ("a3.jpg"); CH. AddItem ("a4.jpg"); Add (CH); a = getImage (GetDocumentBase (), "a1.jpg"); // A initial value of A; Add (one); Add (TWO); Add (three); Ch.Additemlistener (this); // pay attention to put the itemlistener interface imports One.addActionListener (this); // pay attention to put an actionListener interface imports Two.addActionListener (this); Three.AddActionListener (this); } Public Void ItemStateChanged (ItemEvent E) { A = getImage (GetDocumentBase (), ch.getSelectedItem ()); Repaint (); } Public Void ActionPerformed (ActionEvent E) { IF (E.getsource () == One) { Num = 1; Repaint (); // Refresh the program } IF (E.getsource () == TWO) { Num = 2; Repaint (); } IF (E.GetSource () == Three) { Num = 3; Repaint (); } } Public void Paint (Graphics G) { //g.drawimage (AA [I] ,0, 0, thermist); INT w = a.Getwidth (this); INT H = a.getheight (this); // g.drawline (100, 1, 200, 500); Try { G.drawImage (A, 20, 300, 10 W, 20 H, this); // To catch an abnormality, if the image can't find it? G.drawImage (AA [NUM], 50, 50, 200, 200, this); } Catch (Exception E) { System.out.println (e); } } Public Boolean Handleevent (Event EWT) // Off-1, I originally thought that this can be given the dead cycle, other otherwise. The same is not the program { IF (EWT.ID == Event.Window_Destroy) System.exit (0); Else Return super.handleevent (EWT); Return True; } } // Note: Since I still start learning to write a document, I have nothing to understand.