⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyeventdemo.java

📁 这是一个英文版的《Java程序设计与问题解决》现在好多大学都当成教材
💻 JAVA
字号:
/* * KeyEventDemo.java is a 1.4 example that requires * no other files. */import javax.swing.*;import java.awt.event.*;import java.awt.BorderLayout;import java.awt.Dimension;public class KeyEventDemo extends JApplet                           implements KeyListener,                                     ActionListener {    JTextArea displayArea;    JTextField typingArea;    static final String newline = "\n";    public void init() {        JButton button = new JButton("Clear");        button.addActionListener(this);        typingArea = new JTextField(20);        typingArea.addKeyListener(this);        displayArea = new JTextArea();        displayArea.setEditable(false);        JScrollPane scrollPane = new JScrollPane(displayArea);        scrollPane.setPreferredSize(new Dimension(375, 125));        JPanel contentPane = new JPanel();        contentPane.setLayout(new BorderLayout());        contentPane.add(typingArea, BorderLayout.PAGE_START);        contentPane.add(scrollPane, BorderLayout.CENTER);        contentPane.add(button, BorderLayout.PAGE_END);        setContentPane(contentPane);    }    /** Handle the key typed event from the text field. */    public void keyTyped(KeyEvent e) {        displayInfo(e, "KEY TYPED: ");    }    /** Handle the key pressed event from the text field. */    public void keyPressed(KeyEvent e) {        displayInfo(e, "KEY PRESSED: ");    }    /** Handle the key released event from the text field. */    public void keyReleased(KeyEvent e) {        displayInfo(e, "KEY RELEASED: ");    }    /** Handle the button click. */    public void actionPerformed(ActionEvent e) {        //Clear the text components.        displayArea.setText("");        typingArea.setText("");        //Return the focus to the typing area.        typingArea.requestFocus();    }    /*     * We have to jump through some hoops to avoid     * trying to print non-printing characters      * such as Shift.  (Not only do they not print,      * but if you put them in a String, the characters     * afterward won't show up in the text area.)     */    protected void displayInfo(KeyEvent e, String s){        String charString, keyCodeString, modString, tmpString;        char c = e.getKeyChar();        int keyCode = e.getKeyCode();        int modifiers = e.getModifiers();        if (Character.isISOControl(c)) {            charString = "key character = "                       + "(an unprintable control character)";        } else {            charString = "key character = '"                       + c + "'";        }        keyCodeString = "key code = " + keyCode                        + " ("                        + KeyEvent.getKeyText(keyCode)                        + ")";        modString = "modifiers = " + modifiers;        tmpString = KeyEvent.getKeyModifiersText(modifiers);        if (tmpString.length() > 0) {            modString += " (" + tmpString + ")";        } else {            modString += " (no modifiers)";        }        displayArea.append(s + newline                           + "    " + charString + newline                           + "    " + keyCodeString + newline                           + "    " + modString + newline);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -