在 JTabbedPane 中的选项卡之间切换时通过 KeyListener 更改焦点

发布于 2025-01-02 00:41:45 字数 1367 浏览 0 评论 0原文

我计划做的是,当我按 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();
            }
    }

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

What I planned to do is when I press Enter key, the application will fire up the button btn_teach, and switch to another tab with textfield focused. Now, when I implement it, it works perfectly if I press the button manually (not pressing Enter). When I actually press Enter, the tab switches back but the text field on that tab is not focused.

tp being JTabbedPane.
My plan is to switch from tab index 1 to tab index 0 and set txt_send focused

    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();
            }
    }

What is the correct way to change focus via KeyListener when switching between tabs in JTabbedPane?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-01-09 00:41:45

要使 JButton 在按下 ENTER 键时起作用,您可以将该 JButton 设为框架上的DEFAULT Button ,而不是使用KeyEvents。您可以通过编写以下内容来完成此操作:

yourFrameObject.getRootPane().setDefaultButton(btn_teach);

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

不要将 KeyEvents 与 Swing 一起使用,它属于 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();
            }
        });
    }
}

For JButton to work on press of the ENTER key you can make that JButton to be your DEFAULT Button on frame, instead of using KeyEvents. You can do this by writing :

yourFrameObject.getRootPane().setDefaultButton(btn_teach);

Writing this line will also make this JButton work on pressing the ENTER key, without you writing the whole KeyListener part for it. Remove the KeyListeners from this JButton. Once this JButton is the DEFAULT Button, now on Pressing the ENTER key, it will do the work that is written inside it's actionPerformed() method.

Don't use KeyEvents with Swing, that belongs to awt, KeyBinding is what we use with Swing.

Here is one sample program to help your cause :

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();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文