JavaSwing:如何停止不需要的shift键击键操作
当我在一个JPanel中有一个JTextField并且它有焦点时,按“tab”并没有任何作用。。。但按下“shift tab”会导致焦点丢失(FocusEvent.GetOperativeComponent()为空) 如果JPanel上有其他可聚焦组件(或者更确切地说,在“聚焦循环根目录”下),则不会发生这种情况:相反,它们会在shift选项卡上获得聚焦 在下面的SSCCE中,我演示了这一点。。。每次在搜索框中按Return键时,都会向JTable中添加一行,从而使其成为可聚焦的。您还可以取消注释使JRadioButtons不可聚焦的行 我也查看了输入图,看看是否有shift tab参与其中。。。一点也不 我还尝试过聚焦策略,看看我是否能理解这个问题。没有快乐 我的目标是:当焦点循环根的范围内有一个可聚焦组件时,停止“shift tab”导致焦点丢失(焦点消失) 后来 解决方法是添加行JavaSwing:如何停止不需要的shift键击键操作,java,swing,focus,Java,Swing,Focus,当我在一个JPanel中有一个JTextField并且它有焦点时,按“tab”并没有任何作用。。。但按下“shift tab”会导致焦点丢失(FocusEvent.GetOperativeComponent()为空) 如果JPanel上有其他可聚焦组件(或者更确切地说,在“聚焦循环根目录”下),则不会发生这种情况:相反,它们会在shift选项卡上获得聚焦 在下面的SSCCE中,我演示了这一点。。。每次在搜索框中按Return键时,都会向JTable中添加一行,从而使其成为可聚焦的。您还可以取消注
if( oppComp == null ){ impl.searchBox.requestFocus(); }
在搜索框的FocusListener的focusLost方法的末尾。。。但对我来说,这只是一个解决办法。。。1) 它没有通过理解焦点遍历机制来解决问题;2) 当你需要从搜索框中丢失焦点时,可能会出现这样的情况
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
class BackTabProb {
JFrame mainFrame;
JTextField searchBox;
JTable resultsTable;
public static void print(String msg) {
System.out.println(msg);
}
public static void main(String[] a_args) throws InvocationTargetException, InterruptedException {
final BackTabProb impl = new BackTabProb();
class Show implements Runnable {
public void run() {
impl.mainFrame = new JFrame("Back Tab problem");
impl.mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
impl.mainFrame.dispose();
}
});
impl.resultsTable = new JTable();
impl.resultsTable.setFocusable(false);
Vector<Object> dataVector = new Vector<Object>();
Vector<Object> colIdentifiers = new Vector<Object>(Arrays.asList(new String[] { "ONE", "TWO" }));
((DefaultTableModel) impl.resultsTable.getModel()).setDataVector(dataVector, colIdentifiers);
JScrollPane jsp = new JScrollPane(impl.resultsTable);
JPanel northPanel = new JPanel();
northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
impl.searchBox = new JTextField("Enter search text", 10);
JLabel label = new JLabel("Search:");
label.setLabelFor(impl.searchBox);
northPanel.add(label);
northPanel.add(impl.searchBox);
ButtonGroup buttonGroup = new ButtonGroup();
ArrayList<JRadioButton> indexButtons = new ArrayList<JRadioButton>();
for (int i = 0; i < 2; i++) {
JRadioButton indexButton = new JRadioButton(i == 0 ? "Stemmer" : "Simple");
// commenting this out means back-tabbing from search box does not result
// in focus going "nowhere"
indexButton.setFocusable(false);
buttonGroup.add(indexButton);
northPanel.add(indexButton);
}
impl.mainFrame.getContentPane().setLayout(new BorderLayout());
impl.mainFrame.getContentPane().add(northPanel, BorderLayout.NORTH);
impl.mainFrame.getContentPane().add(jsp, BorderLayout.CENTER);
impl.mainFrame.pack();
impl.mainFrame.setVisible(true);
print("=== visible");
}
}
EventQueue.invokeAndWait(new Show());
class AddMore implements Runnable {
public void run() {
impl.mainFrame.setFocusTraversalPolicyProvider(true);
class SearchBoxFocusListener implements FocusListener {
public void focusGained(FocusEvent focusEvent) {
print("=== search box got focus");
impl.searchBox.selectAll();
}
public void focusLost(FocusEvent focusEvent) {
Component oppComp = focusEvent.getOppositeComponent();
print(String.format("=== search box lost focus to %s",
oppComp == null ? "nowhere" : oppComp.getClass()));
}
}
impl.searchBox.addFocusListener(new SearchBoxFocusListener());
class SearchBoxActionListener implements ActionListener {
public void actionPerformed( ActionEvent actionEvent ){
if( actionEvent.getSource() != null ){
((DefaultTableModel)impl.resultsTable.getModel()).insertRow( 0, new Object[]{ "blip", "blap" });
// as soon as the table has at least one row it is set to "focusable"
// commenting this out means back-tabbing from search box results
// in focus going "nowhere"
impl.resultsTable.setFocusable( true );
}
}
}
impl.searchBox.addActionListener( new SearchBoxActionListener() );
ActionMap am = impl.searchBox.getActionMap();
print("=== ActionMap");
for (Object key : am.allKeys()) {
print(String.format(" === action key %s", key));
}
for (int i = 0; i < 3; i++) {
print(String.format("=== InputMap type %d", i));
InputMap im = impl.searchBox.getInputMap(i);
KeyStroke[] allKeys = im.allKeys();
if (allKeys != null) {
for (KeyStroke ks : allKeys) {
print(String.format(" === keystroke %s object %s", ks, im.get(ks)));
}
}
}
// various experiments with FocusTraversalPolicy... NB LayoutTraversalPolicy
// is what the framework uses here by default
class MainFrameFocusTraversalPolicy extends LayoutTraversalPolicy {
public Component getComponentAfter(Container arg0, Component arg1) {
Component comp = super.getComponentAfter(arg0, arg1);
print(String.format("=== comp after %s", comp == null ? "Null" : comp.getClass()));
return comp;
}
public Component getComponentBefore(Container arg0, Component arg1) {
Component comp = super.getComponentBefore(arg0, arg1);
print(String.format("=== comp before %s", comp == null ? "Null" : comp.getClass()));
return comp;
}
public Component getDefaultComponent(Container arg0) {
Component comp = super.getDefaultComponent(arg0);
print(String.format("=== default comp %s", comp == null ? "Null" : comp.getClass()));
return comp;
}
public Component getFirstComponent(Container arg0) {
Component comp = super.getFirstComponent(arg0);
print(String.format("=== first comp %s", comp == null ? "Null" : comp.getClass()));
return comp;
// return impl.searchBox;
}
public Component getLastComponent(Container arg0) {
Component comp = super.getLastComponent(arg0);
print(String.format("=== last comp %s", comp == null ? "Null" : comp.getClass()));
return comp;
}
protected boolean accept(Component comp) {
boolean accept = super.accept(comp);
print(String.format("=== accept %s? %s", comp == null ? "Null" : comp.getClass(), accept));
return accept;
}
}
impl.mainFrame.setFocusTraversalPolicy(new MainFrameFocusTraversalPolicy());
}
}
EventQueue.invokeAndWait(new AddMore());
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.lang.*;
导入java.lang.reflect.InvocationTargetException;
导入java.util.*;
导入javax.swing.*;
导入javax.swing.table.*;
类BackTabProb{
JFrame主机;
JTextField搜索框;
JTable结果表;
公共静态无效打印(字符串消息){
System.out.println(msg);
}
公共静态void main(字符串[]a_args)抛出InvocationTargetException、InterruptedException{
final BackTabProb impl=新BackTabProb();
类Show实现了Runnable{
公开募捐{
impl.mainFrame=新JFrame(“后选项卡问题”);
impl.mainFrame.addWindowListener(新的WindowAdapter(){
公共作废窗口关闭(WindowEvent WindowEvent){
impl.mainFrame.dispose();
}
});
impl.resultsTable=new JTable();
impl.resultsTable.setFocusable(false);
向量数据向量=新向量();
Vector colIdentifiers=新向量(Arrays.asList(新字符串[]{“一”,“二”}));
((DefaultTableModel)impl.ResultTable.getModel()).setDataVector(dataVector,共标识符);
JScrollPane jsp=新的JScrollPane(impl.resultsTable);
JPanel northPanel=新的JPanel();
设置布局(新的BoxLayout(northPanel,BoxLayout.X_轴));
impl.searchBox=新的JTextField(“输入搜索文本”,10);
JLabel标签=新JLabel(“搜索:”);
label.setLabelFor(impl.searchBox);
添加(标签);
northPanel.add(impl.searchBox);
ButtonGroup ButtonGroup=新建ButtonGroup();
ArrayList indexButtons=新的ArrayList();
对于(int i=0;i<2;i++){
JRadioButton indexButton=newjradiobutton(i==0?“词干分析器”:“简单”);
//注释掉这一点意味着从搜索框返回标签不会产生结果
//聚焦“无处可去”
indexButton.setFocusable(假);
按钮组添加(indexButton);
添加(索引按钮);
}
impl.mainFrame.getContentPane().setLayout(新的BorderLayout());
impl.mainFrame.getContentPane().add(northPanel,BorderLayout.NORTH);
impl.mainFrame.getContentPane().add(jsp,BorderLayout.CENTER);
impl.mainFrame.pack();
impl.mainFrame.setVisible(true);
打印(“==可见”);
}
}
invokeAndWait(newshow());
类AddMore实现可运行{
公开募捐{
impl.mainFrame.setFocustraversalPolicProvider(true);
类SearchBoxFocusListener实现FocusListener{
获得公共无效焦点(焦点事件焦点事件){
打印(“==搜索框获得焦点”);
impl.searchBox.selectAll();
}
公共无效焦点丢失(FocusEvent FocusEvent){
Component oppComp=focusEvent.getOppositeComponent();
打印(String.format(“==搜索框失去了对%s的焦点),
oppComp==null?“无处”:oppComp.getClass());
}
}
impl.searchBox.addFocusListener(新的SearchBoxFocusListener());
类SearchBoxActionListener实现ActionListener{
已执行的公共无效操作(ActionEvent ActionEvent){
if(actionEvent.getSource()!=null){
((DefaultTableModel)impl.ResultTable.getModel()).insertRow(0,新对象[]{“blip”,“blap”});
//一旦表格至少有一行,它就被设置为“可聚焦”
//对此进行注释意味着从搜索框结果中返回选项卡
//聚焦“无处可去”
impl.resultsTable.setFocusable(true);
}
}
}
impl.searchBox.addActionListener(新的SearchBoxActionListener());
ActionMap am=impl.searchBox.getActionMap();
打印(“==ActionMap”);
for(对象键:am.allKeys()){
打印(字符串格式(“==操作键%s”,键));
Set<AWTKeyStroke> backwardKeys = Collections.emptySet();
//alone JTextField(pointed out by @mKorbel): impl.mainFrame.setFocusTraversalKeys(
impl.searchBox.setFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardKeys);
if( oppComp == null ){
final Component srcComp = (Component)focusEvent.getSource();
FocusTraversalPolicy ftp = impl.mainFrame.getFocusTraversalPolicy();
Component lastComp = ftp.getLastComponent( impl.mainFrame );
Component beforeComp = ftp.getComponentBefore( impl.mainFrame, srcComp );
if( lastComp == beforeComp ){
EventQueue.invokeLater( new Runnable(){
public void run() {
if( impl.mainFrame.isFocused()){
srcComp.requestFocus();
};
}});
}
}