[Document Introduction] interaction with users is the main function of Java, which is also the reason why Java attracts people. Users can talk to Java Applet through the mouse and keyboard.
Interaction with users is the main role of Java, which is also the reason why Java attracts people. Users can talk to Java Applet through the mouse. Let's first look at the response mouse example:
// Mouse. java
Import java. awt .*;
Import java. applet .*;
Public class Mouse extends Applet
{
String text = "";
Public void paint (Graphics g)
{
G. drawString (text, 20, 20 );
}
Public boolean mouseDown (Event evt, int x, int y) // process the function by pressing the mouse
{
Text = "Mouse Down ";
Repaint ();
Return true;
}
Public boolean mouseUp (Event evt, int x, int y) // release the Handler
{
Text = "";
Repaint ();
Return true;
}
}
When you click a program, the program displays "Mouse Down", indicating that the program responds to the Mouse. However, note that Java does not distinguish between the left and right keys of the mouse.
Let's take a look at the keyboard response example:
// Keyboard. java
Import java. awt .*;
Import java. applet .*;
Public class Keyboard extends Applet
{
String text = "";
Public void paint (Graphics g)
{
G. drawString (text, 20, 20 );}
Public boolean keyDown (Event evt, int x) // processing function for pressing the keyboard
{
Text = "Key Down ";
Repaint ();
Return true;
}
Public boolean keyUp (Event evt, int x) // handler function released on the keyboard
{
Text = "";
Repaint ();
Return true;
}
}
}
When the keyboard is pressed, the program displays "Key Down". When the keyboard is released, the text is cleared. With these functions, we can use the mouse and keyboard functions to interact with users.