java Swing中requestFocusInWindow()和grabFocus()的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17680817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:39:24  来源:igfitidea点击:

Difference between requestFocusInWindow() and grabFocus() in Swing

javaswingfocus

提问by JavaTechnical

I would like to know the difference between requestFocusInWindow()and grabFocus()methods. Both of them work fine for grabbing the focus for me in this program. Therefore, i couldn't understand the difference.

我想知道requestFocusInWindow()grabFocus()方法之间的区别。他们都很好地为我在这个程序中抓住了焦点。因此,我无法理解其中的区别。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Focus extends JFrame
{
JButton jb;

    public Focus()
    {
        createAndShowGUI();
    }

    private void createAndShowGUI()
    {
        setTitle("grabFocus() vs requestFocusInWindow()");
        setLayout(new FlowLayout());
        setSize(400,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jb=new JButton("Open Dialog");
        jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                showDialog();
            }
        });

        add(jb);
    }

    private void showDialog()
    {
        JDialog jd=new JDialog();
        jd.setLayout(new GridLayout(2,2));
        jd.setVisible(true);


        JLabel l1=new JLabel("Label 1");
        JLabel l2=new JLabel("Label 2");

        JTextField jt1=new JTextField(20);
        JTextField jt2=new JTextField(20);

        jd.add(l1);
        jd.add(jt1);
        jd.add(l2);
        jd.add(jt2);

        // Both of them are doing the thing
        //jt2.grabFocus();
        jt2.requestFocus();

        jd.pack();
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
                new Focus();
            }
        });
    }
}

回答by JavaTechnical

The answer is simple, grabFocus()grabsthe focus, no matter whether the top-level ancestor is the focused window. If the window is not active, then it is made active to let the component get the focus.

答案很简单,grabFocus()抓住焦点,不管顶层祖先是否是焦点窗口。如果窗口未处于活动状态,则将其激活以让组件获得焦点。

Whereas, requestFocusInWindow()gets the focus for the component on which it is called onlywhen its top-level ancestor is the focused window.

然而,只有当它的顶级祖先是聚焦窗口时requestFocusInWindow(),才获得调用它的组件的焦点。

In your example, JDialogis the top level ancestor, it gets focus automatically when the JButtonis clicked. So requestFocusInWindow()and grabFocus()does not make a difference.

在您的示例中,JDialog是顶级祖先,JButton单击时它会自动获得焦点。所以requestFocusInWindow()grabFocus()并没有什么区别。

I have re-written the program to better understand the difference using a pragmatic approach.

我重新编写了程序,以使用务实的方法更好地理解差异。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Focus extends JFrame
{
JButton jb;
JTextField jt;

    public Focus()
    {
        createAndShowGUI();
    }

    private void createAndShowGUI()
    {
        setTitle("grabFocus() vs requestFocusInWindow()");
        setLayout(new FlowLayout());
        setSize(400,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jb=new JButton("Open Dialog");
        jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                showDialog();
            }
        });

        add(jb);

        jt=new JTextField(20);

        add(jt);
    }

    private void showDialog()
    {
        JDialog jd=new JDialog();
        jd.setLayout(new GridLayout(2,2));
        jd.setVisible(true);


        JLabel l1=new JLabel("Label 1");
        JLabel l2=new JLabel("Label 2");

        JTextField jt1=new JTextField(20);
        JTextField jt2=new JTextField(20);

        jd.add(l1);
        jd.add(jt1);
        jd.add(l2);
        jd.add(jt2);

        jt.requestFocusInWindow();
        //jt.grabFocus();

        jd.pack();
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
                new Focus();
            }
        });
    }
}

Here, requestFocusInWindow()is called on jtand it doesn't work (i.e. jtdoesn't get the focus) because the JDialogis made active when the JButtonis clicked and JTextFieldin the JDialoggets the focus.

在这里,requestFocusInWindow()被调用jt并且它不起作用(即jt没有获得焦点),因为JDialogJButton单击 并JTextField在 中JDialog获得焦点时被激活。

Next, grabFocus()works. When the JButtonis clicked, JDialogis displayed, but will not be active. Because upon call to the grabFocus(), immediately the JFramebecomes the active top-level ancestor and jtis forcedto get the focus.

接下来,grabFocus()工作。当JButton被点击时,JDialog显示出来,但不会被激活。因为调用后grabFocus(),立即JFrame成为活动的顶层祖先和jt强迫获取焦点。