mport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test3 extends JFrame {
static JFrame f;
static JPanel p;
static JTextField tf1, tf2;
static JLabel l;
Test3(){
f = new JFrame();
p = new JPanel();
tf1 = new JTextField(10);
tf2 = new JTextField(10);
l = new JLabel("输入开始 -输入n结束-输入其他键继续");
l.setFont(new Font("微软雅黑", Font.PLAIN, 13));
Listener listener = new Listener();
tf1.addKeyListener(listener);
p.setLayout(new GridLayout(3,1));
p.add(l);
p.add(tf1);
p.add(tf2);
f.add(p);
f.setTitle("Test");
//f.setSize(300, 300);
f.setBounds(500, 300, 300, 300);//前面两个参数f在屏幕中的位子 后面两个参数f的大小
f.setVisible(true);
}
public static void main(String args[]){
new Test3();
}
static class Listener implements KeyListener {
public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == 's'){
tf2.setText("You put in the char : "+e.getKeyChar()+" Code: "+e.getKeyCode()+" command: Start!");
}else if (e.getKeyChar() =='n'){
tf2.setText("You put in the char : "+e.getKeyChar()+" Code: "+e.getKeyCode()+" command: Stop!");
}else{
tf2.setText("You put in the char : "+e.getKeyChar()+"
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
} /*
import java.awt.event.*;
import javax.swing.*;
public class Test3 extends JFrame {
static JFrame f;
static JPanel p;
static JTextField tf1, tf2;
static JLabel l;
Test3(){
f = new JFrame();
p = new JPanel();
tf1 = new JTextField(10);
tf2 = new JTextField(10);
l = new JLabel("输入开始 -输入n结束-输入其他键继续");
l.setFont(new Font("微软雅黑", Font.PLAIN, 13));
Listener listener = new Listener();
tf1.addKeyListener(listener);
p.setLayout(new GridLayout(3,1));
p.add(l);
p.add(tf1);
p.add(tf2);
f.add(p);
f.setTitle("Test");
//f.setSize(300, 300);
f.setBounds(500, 300, 300, 300);//前面两个参数f在屏幕中的位子 后面两个参数f的大小
f.setVisible(true);
}
public static void main(String args[]){
new Test3();
}
static class Listener implements KeyListener {
public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == 's'){
tf2.setText("You put in the char : "+e.getKeyChar()+" Code: "+e.getKeyCode()+" command: Start!");
}else if (e.getKeyChar() =='n'){
tf2.setText("You put in the char : "+e.getKeyChar()+" Code: "+e.getKeyCode()+" command: Stop!");
}else{
tf2.setText("You put in the char : "+e.getKeyChar()+"
Code: "+e.getKeyCode()+" command: Go on!");
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
} /*