当面板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();
}
};
----------------解决方案--------------------------------------------------------
下班前,上了下论坛,给你修改了下,不知道是否符合你的要求。下线了
程序代码:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class testjiaodian extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel apanel;
JButton b;
public testjiaodian()
{
setSize(100,100);
apanel=new JPanel();
apanel.addMouseListener(new MouseHandler());
apanel.addKeyListener(new keyhandle());
b =new JButton("点我");
b.addMouseListener(new MouseHandler());
apanel.add(b);
add(apanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
apanel.requestFocus(true);
}
});
}
}
private class keyhandle implements KeyListener
{
public void keyPressed(KeyEvent event){}
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event)
{
char keychar = event.getKeyChar();
if(keychar == 'w')
{
JOptionPane.showMessageDialog(null, "键盘响应", "信息", JOptionPane.DEFAULT_OPTION );
}
}
}
public static void main(String args[])
{
new testjiaodian();
}
}
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class testjiaodian extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel apanel;
JButton b;
public testjiaodian()
{
setSize(100,100);
apanel=new JPanel();
apanel.addMouseListener(new MouseHandler());
apanel.addKeyListener(new keyhandle());
b =new JButton("点我");
b.addMouseListener(new MouseHandler());
apanel.add(b);
add(apanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
apanel.requestFocus(true);
}
});
}
}
private class keyhandle implements KeyListener
{
public void keyPressed(KeyEvent event){}
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event)
{
char keychar = event.getKeyChar();
if(keychar == 'w')
{
JOptionPane.showMessageDialog(null, "键盘响应", "信息", JOptionPane.DEFAULT_OPTION );
}
}
}
public static void main(String args[])
{
new testjiaodian();
}
}
----------------解决方案--------------------------------------------------------
回复 2楼 linjx0123
谢谢大侠~ 很好用,我的毕设就靠他了。。 ----------------解决方案--------------------------------------------------------