Java Applet Getting Started 19

zhaozj2021-02-11  238

Getting started with Java Applet

YY435

Pacific Network Academy

Fourth day

Keyboard event example

This example requires you to use the mouse to click in the Javaapplet area, then click on the keyboard, you can press what key to each time you can display it on the screen, no lie to you, try it!

The source code of this example is as follows:

Import java.awt.event;

Import java.awt.graphics;

Import java.applet. *;

Public Class Keystrike Extends Applet

{

Char presskey = 0;

INT OUTX = 5, OUTY = 15; // Defines the initial output position

Public Boolean KeyDown (Event E, INT Key) // Keyboard Processing Method

{

Switch (key) / / Determine the key value pressed

{

Case Event.up: OUTY -; BREAK; // If the up button is pressed ...

Case Event.down: OUTY ; Break; // If you press the down button ...

Case Event.Left: Outx -; Break; // If you press the left button ...

Case Event.right: Outx ; Break; // If you press the right button ...

Case Event.home: OUTX = 5; OUTY = 15; Break; // If you press, it is to the home button.

Case Event.end: Outx = 5; OUTY = Size (). Height -15; Break;

/ If the END button is pressed ......

Case Event.pgup: OUTY- = 5; Break; // If the PageUp button is pressed ......

Case Event.pgdn: OUTY = 15; Break; // If you press the Page Down button

Default: presskey = (char) key; breaf; // If the button is not pressed ......

}

Repaint ();

Return True;

}

Public void Paint (Graphics G)

{

IF (PressKey! = 0)

g.drawstring ("You press:" PressKey "Key", OUTX, OUTY

// Display button information!

}

}

转载请注明原文地址:https://www.9cbs.com/read-4721.html

New Post(0)