?? keyeventdemo.java
字號:
package chapter14;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyEventDemo implements ActionListener,KeyListener
{
JFrame frmMain;
JLabel lblInfo,lblTest,lblEvent;
JTextField txtTest,txtInfo;
JButton btnClear=null;
String strKey="";
public KeyEventDemo()
{
frmMain=new JFrame("鍵盤事件演示");
Container conPane=frmMain.getContentPane();
conPane.setLayout(new GridLayout(3,2));
lblInfo=new JLabel("您的輸入:");
txtInfo=new JTextField();
txtInfo.setBackground(Color.CYAN);
txtInfo.enable(false);
lblTest=new JLabel("輸入字符:");
txtTest=new JTextField();
txtTest.requestFocus();
txtTest.addKeyListener(this);
lblEvent=new JLabel();
btnClear=new JButton("重置(C)");
btnClear.setMnemonic('C');
btnClear.addActionListener(this);
Font fntDisp=new Font("宋體",Font.PLAIN,12);
lblTest.setFont(fntDisp);
txtTest.setFont(fntDisp);
lblInfo.setFont(fntDisp);
txtInfo.setFont(fntDisp);
lblEvent.setFont(fntDisp);
btnClear.setFont(fntDisp);
conPane.add(lblTest);
conPane.add(txtTest);
conPane.add(lblInfo);
conPane.add(txtInfo);
conPane.add(lblEvent);
conPane.add(btnClear);
frmMain.setSize(250,150);
frmMain.show();
}
public void actionPerformed(ActionEvent e)
{
strKey="";
txtInfo.setText("");
txtTest.setText("");
txtTest.requestFocus();
}
public void keyTyped(KeyEvent e)
{
char c=e.getKeyChar();/*注意getKeyChar()的用法*/
strKey=strKey+c;
txtInfo.setText(strKey);
lblEvent.setText("產生keyTyped!");
}
public void keyPressed(KeyEvent e)
{
lblEvent.setText("產生keyPressed!");
}
public void keyReleased(KeyEvent e)
{
lblEvent.setText("產生keyReleased!");
}
public static void main(String[] args)
{
new KeyEventDemo();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -