2

我试图在下面调用这个事件;我使用 TabBuilder 创建框架(因为它是我的应用程序的一部分),然后它调用弹出的搜索屏幕;但是使用键绑定或简单单击按钮的搜索事件不起作用,当然我做错了什么,但我不知道是什么,因为我对 Java 有点陌生。请问有人可以帮我吗?


搜索屏幕:


public class SearchScreen extends EventSearch{
    
    public static void main (String[] args){
    
        SearchScreen s= new SearchScreen();
    }
    
    public void SearchScreen(){
    
        TabBuilder tb = new TabBuilder();
          tb.searchTab();       
    }
}

事件搜索:


public class EventSearch extends TabBuilder{
    String userQuery;
    String key = "ENTER";
    KeyStroke keyStroke = KeyStroke.getKeyStroke(key);
    
    public EventSearch(){
    
        btSearch.addActionListener(this);
        txtSearch.getInputMap().put(keyStroke, key);
        txtSearch.getActionMap().put(key, enterAction);
    }
    
    Action enterAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
        try{
            System.out.println("worked");
                } catch (IOException e1) {
                    e1.printStackTrace(); //print failure
                    JOptionPane.showMessageDialog(null, "HTTP request failure.");
                }   
            }
        };
     }

选项卡生成器:


public class TabBuilder implements ActionListener {

    protected JButton btSearch;
    JMenuItem close, search;
    protected JTextField txtSearch;
    protected JFrame searchFrame = new JFrame();
    
    public void TabBuilder(){
    }
    
    public void searchTab(){
    
        JLabel lbSearch;
        JPanel searchPane;
        
        btSearch= new JButton("Search");
        lbSearch= new JLabel("Type Keywords in english to be searched below:");
        lbSearch.setHorizontalAlignment(SwingConstants.CENTER);
        txtSearch= new JTextField();
        searchPane=new JPanel();
        searchPane.setBackground(Color.gray);
        searchPane.add(lbSearch);
        searchPane.add(txtSearch);
        searchPane.add(btSearch);
        searchPane.setLayout(new GridLayout(3,3));
        btSearch.setEnabled(true);
        searchFrame.add(searchPane);
        searchFrame.setTitle("SHST");
        searchFrame.setSize(400, 400);
        searchFrame.setVisible(true);
        searchFrame.setDefaultCloseOperation(1);
    }
    
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==close){
            System.exit(0);
        }
        if(e.getSource()==search){
            SearchScreen s = new SearchSreen();
        }
    }
}
4

1 回答 1

3

你写这个actionListener

public void actionPerformed(ActionEvent e){
            if(e.getSource()==close){
                System.exit(0);
        }

            if(e.getSource()==search){
                TabBuilder tb = new TabBuilder();
                tb.searchTab();

            }
        }

并且您添加到btnSearch.addActionListener(this),您的 actionListener 永远不会做任何事情。

对于您 KeyBinding发生类似的事情,您将动作添加到txtSearch然后您询问来源是否是e.getSource()==btSearch

并且KeyBindings您可以使用常量来指定何时必须绑定它们。 JComponent.WHEN_FOCUSED, JComponent.WHEN_IN_FOCUSED_WINDOW,JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

例如 :

txtSearch.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, key);

如何使用键绑定

于 2013-07-10T14:39:47.867 回答