java awt处理keypress事件示例,awtkeypress,package cn.o
分享于 点击 12734 次 点评:190
java awt处理keypress事件示例,awtkeypress,package cn.o
package cn.outofmemory.snippets.desktop;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Frame;import java.awt.TextArea;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;public class KeyListener { public static void main(String[] args) { // Create frame with specific title Frame frame = new Frame("Example Frame"); // Create a component to add to the frame; in this case a text area with sample text Component textArea = new TextArea("You pressed []: \n"); textArea.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { TextArea source = (TextArea)evt.getSource(); source.setText("You pressed [" + evt.getKeyText(evt.getKeyCode()) +"] : "); } }); // Add the components to the frame; by default, the frame has a border layout frame.add(textArea, BorderLayout.NORTH); // Show the frame int width = 300; int height = 300; frame.setSize(width, height); frame.setVisible(true); }}
用户点评