importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava...
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FrameLintener extends JFrame{
JPanel jp1=new JPanel();
JButton button1=new JButton("一个面板的按钮");
public static void main(String[] args) {
FrameLintener f=new FrameLintener();
}FrameLintener(){
//===========================设置窗体
this.setSize(300, 100);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(400, 200);
//============================键盘添加监听
this.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
System.out.println("dsaf");
}
});
//=============================添加组件
jp1.add(button1);
this.add(jp1,BorderLayout.SOUTH);
this.setFocusable(true);
//===============================添加按键监听
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("1");
}
});
}
}
/**
* 问题是这样的,启动程序后,按下键盘会的输出。
* 按下按钮,也有输出
* 但是按下按键之后无论怎么按键盘都没有输出了
* 如何解决这个问题?
* */
展开