当面板JPanel失去焦点时,怎么重新获得焦点
/*下面是我写得一个测试的程序,运行后,不点击按钮的情况下,按小写状态下的'w'时,弹出对话框,
但是当点击按钮后,面板上的监听失去了,我想用鼠标监听把焦点设置回来,可是不好使了
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class testjiaodian extends JFrame
{
JPanel apanel;
public testjiaodian()
{
setSize(100,100);
apanel=new JPanel();
apanel.addMouseListener(new MouseHandler());
apanel.addKeyListener(new keyhandle());
apanel.setFocusable(true);
apanel.add(new JButton("点我"));
add(apanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
apanel.setFocusable(true);
}
}
private class keyhandle implements KeyListener
{
public void keyPressed(KeyEvent event){}
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event)
{
int i;
char keychar = event.getKeyChar();
if(keychar == 'w')
{
JOptionPane.showMessageDialog(null, "键盘响应", "信息", JOptionPane.DEFAULT_OPTION );
}
}
}
public static void main(String args[])
{
new testjiaodian();
}
};