?? keyeventdemo.java
字號:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
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);
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();
if(c=='o')
{
JFrame newf=new JFrame("新窗口");
newf.setSize(200,200);
//newf.setVisible(true);
newf.show();
}
strKey=strKey+c;
txtInfo.setText(strKey);
lblEvent.setText("產(chǎn)生keyTyped!");
}
public void keyPressed(KeyEvent e)
{
lblEvent.setText("產(chǎn)生keyPressed");
}
public void keyReleased(KeyEvent e)
{
lblEvent.setText("產(chǎn)生keyReleased");
}
public static void main(String args[])
{
new KeyEventDemo();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -