1

我计划做的是当我按下Enter键时,应用程序将启动按钮btn_teach,并切换到另一个具有textfield焦点的选项卡。现在,当我实现它时,如果我手动按下按钮(而不是按下Enter),它就可以完美地工作。当我实际按下Enter时,选项卡会切换回来,但该选项卡上的文本字段没有聚焦。

tpJTabbedPane. 我的计划是从标签索引 1 切换到标签索引 0 并设置txt_send焦点

    public void actionPerformed(ActionEvent arg0) 
{
    // TODO Auto-generated method stub
        String say = txt_saypane.getText();
        String ans = txt_anspane.getText();
        //this.clear();

        say = say.replace("\n","");
        ans = ans.replace("\n","");

        this.talk(this.botTeach(say,ans), false);

        tp.setSelectedIndex(0);
}

    public void stateChanged(ChangeEvent arg0) 
{
    // TODO Auto-generated method stub
    int sel = tp.getSelectedIndex();
    if(sel == 0)
        txt_send.requestFocusInWindow();
    if(sel == 1)
        txt_saypane.requestFocusInWindow();
}

public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
            {

                if(txt_saypane.isFocusOwner() || txt_anspane.isFocusOwner())
                    btn_teach.doClick();
            }
    }

KeyListener在选项卡之间切换时更改焦点的正确方法是什么JTabbedPane

4

1 回答 1

5

JButton在按键上工作,ENTER您可以将其JButton作为您DEFAULT Button的框架,而不是使用KeyEvents. 你可以通过写来做到这一点:

yourFrameObject.getRootPane().setDefaultButton(btn_teach);

编写此行还将使此 JButton 在按下 ENTER 键时工作,而无需为它编写整个 KeyListener 部分。KeyListeners从中删除JButton. 一旦这JButtonDEFAULT Button,现在按下ENTER键,它将完成其 actionPerformed() 方法中写入的工作。

不要在 Swing 中使用 KeyEvents,它属于 awt,KeyBinding是我们在 Swing 中使用的。

这是一个示例程序来帮助您的事业:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonTest extends JFrame implements ActionListener
{
    private JTabbedPane tabbedPane;
    private JButton focusButton;
    private JPanel contentPane, tab1, tab2;
    private JTextField textField1, textField2, textField3;

    public ButtonTest()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        tab1 = new JPanel();
        textField1 = new JTextField(10);
        textField1.requestFocusInWindow();
        tab1.add(textField1);

        tab2 = new JPanel();
        textField2 = new JTextField(10);
        textField3 = new JTextField(10);
        tab2.add(textField2);
        tab2.add(textField3);

        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());

        tabbedPane = new JTabbedPane(JTabbedPane.TOP,
                                                JTabbedPane.WRAP_TAB_LAYOUT);
        tabbedPane.addTab("TAB 1", null, tab1, "I am TAB 1");
        tabbedPane.addTab("TAB 2", null, tab2, "I am TAB 2");

        focusButton = new JButton("CHANGE FOCUS");
        //focusButton.addMnemonic(KeyEvent.VK_ENTER); /* You can Add this Line too
                                                      /* , to make it work. But here
                                                       * you have to press ALT + ENTER.
                                                       */
        getRootPane().setDefaultButton(focusButton);
        focusButton.addActionListener(this);

        contentPane.add(tabbedPane, BorderLayout.CENTER);
        contentPane.add(focusButton, BorderLayout.PAGE_END);
        setContentPane(contentPane);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if (tabbedPane.getSelectedIndex() == 0)
        {
            tabbedPane.setSelectedIndex(1);
            textField3.requestFocusInWindow();
        }
        else if (tabbedPane.getSelectedIndex() == 1)
        {
            tabbedPane.setSelectedIndex(0);
            textField1.requestFocusInWindow();
        }
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonTest();
            }
        });
    }
}
于 2012-02-04T12:31:37.257 回答