首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

Java GUI 键盘响应事件 与 swing组件焦点有关问题

2013-04-21 
Java GUI 键盘响应事件 与 swing组件焦点问题这个一个简单画图板界面,JFrame.java(类名随意取的)this.setJ

Java GUI 键盘响应事件 与 swing组件焦点问题
Java GUI 键盘响应事件 与 swing组件焦点有关问题

这个一个简单画图板界面,

JFrame.java(类名随意取的)

this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());

contentpanel用的是BorderLayout,  North是toolbar,Center是画图的Panel,South是再下面提到的rootpanel.java

初始化的时候我使得JButtonDraw.requestFocusInWindow(); //工具栏倒数第二个Button获得焦点



rootpanel.java   键盘响应事件在这个类中比较方便
然后,下面是响应键盘输入的
(
WHEN_IN_FOCUSED_WINDOW    当组件处于焦点窗口中时侦听到的键盘事件;
WHEN_FOCUSED              当组件获得焦点后侦听到的键盘事件(系统默认);
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT      组件祖先获得焦点。)

InputMap inputmap = this.getInputMap(WHEN_IN_FOCUSED_WINDOW);

inputmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "r");
inputmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "l");
inputmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "f");
inputmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "e");

ActionMap actionmap = this.getActionMap();
actionmap.put("f", new MyAction('f'));   //first
actionmap.put("e", new MyAction('e'));  //end
actionmap.put("l", new MyAction('l'));   //left
actionmap.put("r", new MyAction('r'));   //right
MyAction类 键盘响应事件应该没错

问题是不管我怎么试试,键盘响应不了,
但如果我在初始化界面的时候,JFrame.requestFocus();键盘事件就能响应
是因为我的Focus设置在了toolbar上的按钮上了?


swing focus 键盘响应?GUI
[解决办法]
楼主你不是自己解决问题了吗

引用
To fire keyboard events, a component must have the keyboard focus.

http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
[解决办法]
不用单独绑
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;


public class KeyStrokeTest extends JFrame{
public static void main(String[] args) {
new KeyStrokeTest();
}
public KeyStrokeTest(){
setVisible(true);
setSize(300,300);

JToolBar jToolBar=new JToolBar();
JButton jb1=new JButton("aa");
JButton jb2=new JButton("bb");
jToolBar.add(jb1);
jToolBar.add(jb2);
add(jToolBar);

ActionMap actionmap = jToolBar.getActionMap();
InputMap iMap=jToolBar.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );

actionmap.put("r", new MyAction('r'));   //right

iMap.put(KeyStroke.getKeyStroke("F2"), "r");
}
}
 class MyAction implements Action{
 char r;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println(r);
}



@Override
public Object getValue(String key) {
// TODO Auto-generated method stub
return null;
}

@Override
public void putValue(String key, Object value) {
// TODO Auto-generated method stub

}

@Override
public void setEnabled(boolean b) {
// TODO Auto-generated method stub

}

@Override
public boolean isEnabled() {
// TODO Auto-generated method stub
return true;
}

@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
// TODO Auto-generated method stub

}

@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
// TODO Auto-generated method stub

}
public MyAction(char r){
this.r=r;
}
 
 }



看看这段代码吧
关键要有JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT


Keystroke processing checks these maps in the following order:
1. Check the WHEN_FOCUSED map of the component with input focus. If the keystroke exists,
execute the corresponding action. If the action is enabled, stop processing.
2. Starting from the component with input focus, check the WHEN_ANCESTOR_OF_FOCUSED_
COMPONENT maps of its parent components. As soon as a map with the keystroke is
found, execute the corresponding action. If the action is enabled, stop processing.
3. Look at all visible and enabled components in the window with input focus that
have this keystroke registered in a WHEN_IN_FOCUSED_WINDOW map. Give these compo-
nents (in the order of their keystroke registration) a chance to execute the corre-
sponding action. As soon as the first enabled action is executed, stop processing.
This part of the process is somewhat fragile if a keystroke appears in more than
one WHEN_IN_FOCUSED_WINDOW map.
Table 8–2 Input Map Conditions
Flag Invoke Action
WHEN_FOCUSED :When this component has keyboard focus
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT :When this component contains the component that has keyboard focus
WHEN_IN_FOCUSED_WINDOW: When this component is contained in the same window
as the component that has keyboard focus
以上摘自core JAVA

搞了我半天,累死了。。。

热点排行