📄 eventhandlingdemo4.java
字号:
/**
*A event handling program as keyboard event.
*it creates a small text edo=itor that displays text on screen
*2004.12.29. xhcprince
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class EventHandlingDemo4 extends Applet
{
private String text;
public void init()
{
showStatus("Initializing");
text = "";
addKeyListener(new KeyHandler());
requestFocus();
/*-------------------------------------------------------------
By requesting the focus of the keyboard,the keys pressed
will be sent to your application,if not done then you might
not be able to process the keyboard input,so you have to
use requestFocus()!
-------------------------------------------------------------*/
}
public void paint(Graphics g)
{
g.drawString(text,50,150);
}
class KeyHandler implements KeyListener //Inner class!
{
/*-------------------------------------------------------------
A character key has been pressed
-------------------------------------------------------------*/
public void keyTyped(KeyEvent k)
{
text += k.getKeyChar();
repaint();
}
/*-------------------------------------------------------------
A keyboard key has been pressed
-------------------------------------------------------------*/
public void keyPressed(KeyEvent k)
{
showStatus("Key is pressed");
}
/*-------------------------------------------------------------
A keyboard key has been released
-------------------------------------------------------------*/
public void keyReleased(KeyEvent k)
{
showStatus("Key is released");
}
}
}
/*
<applet code = "EventHandlingDemo4.class" width = 300 height = 180>
</applet>
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -