this.setExtendedState(0);
this.requestFocus();
addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
int ch = e.getKeyCode();
System.out.println(ch+"dd");
}
});为什么不能响应事件呢?当窗口最小化后,在恢复窗口,就能响应事件了
this.requestFocus();
addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
int ch = e.getKeyCode();
System.out.println(ch+"dd");
}
});为什么不能响应事件呢?当窗口最小化后,在恢复窗口,就能响应事件了
解决方案 »
- 在Netbeans 中导入了TableExample.jar这个文件 但是要怎么调用啊
- 怎么用eclipse查看java jdk 里的内部类
- 谁帮我看看下面几道题,帮忙解决下英语不好
- JAVA中为什么没有多继承?
- setFileFilter 的问题?--求救呀~~~~
- Applet 与 Application ?
- 怎样设置GUI运行时,窗体跟WIN32标准窗体一样啊?不会让人一看标题栏就知道是java的应用程序!
- 遇到一个棘手的算法问题,多边形(可能凸也可能是凹)顶点的顺序问题
- 什么叫“藏着手臂的维纳斯”?
- java开发好去处
- 帮忙看看,关于FileOutputStream
- 用eclipse访问oracel数据库时出现以下问题,请各位大哥大姐帮忙解决一下,谢谢!
下面是JDK6中对requestFocus()的解释:public void requestFocus()请求此 Component 获取输入焦点,并且此 Component 的顶层祖先成为获得焦点的 Window。此 Component 对于所要许可的请求而言必须是不可显示的、可聚焦的和可见的并且其所有祖先(除了顶层 Window 以外)必须是可见的。此方法会尽力完成该请求;但是在某些情况下可能无法完成。在此 Component 接收 FOCUS_GAINED 事件前,开发人员永远不能假定此 Component 是焦点所有者。
这不管用
public class Key extends JFrame { private JButton button; public static void main(String args[]) {
Key frame = new Key();
frame.setVisible(true);
}
public Key() {
super();
getContentPane().setLayout(null);
this.requestFocus(true);
this.requestFocus();
this.repaint();
addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
System.out.println("事件发生");
}
});
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(getButton()); // 这里注释掉 就能响应事件了
} private JButton getButton() {
if (button == null) {
button = new JButton();
button.setBounds(251, 164, 113, 34);
button.setText("New JButton");
}
return button;
}}
第一种 索性把事件加到button上public class Key extends JFrame { private JButton button; public static void main(String args[]) {
Key frame = new Key();
frame.setVisible(true);
}
public Key() {
super();
getContentPane().setLayout(null);
this.requestFocus(true);
this.requestFocus();
this.repaint();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(getButton()); // 这里注释掉 就能响应事件了
getButton().addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
System.out.println("事件发生");
}
});
} private JButton getButton() {
if (button == null) {
button = new JButton();
button.setBounds(251, 164, 113, 34);
button.setText("New JButton");
}
return button;
}}
第二种 加了button之后 在把焦点移回frame上
public class Key extends JFrame { private JButton button; public static void main(String args[]) {
Key frame = new Key();
frame.setVisible(true);
}
public Key() {
super();
getContentPane().setLayout(null);
this.requestFocus(true);
this.requestFocus();
this.repaint();
addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
System.out.println("事件发生");
}
});
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(getButton()); // 这里注释掉 就能响应事件了
this.setFocusable(true);
} private JButton getButton() {
if (button == null) {
button = new JButton();
button.setBounds(251, 164, 113, 34);
button.setText("New JButton");
}
return button;
}}