Java Applet programming response mouse and keyboard

Source: Internet
Author: User

[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.