在加载面板时关注组件

问题描述 投票:0回答:2

我有一个框架,我加载一个面板。它工作正常,但加载时没有任何焦点。按下选项卡没有帮助。我必须使用鼠标按文本字段。我试过:jtextfield1.requestFocus();jtextfiel1.requestFocusInWindow();但它不起作用。

我究竟做错了什么?

JPanel中的构造函数:

public OpretOpdater(BrugerHandler brugerHandler, ReklamationHandler reklamationsHandler) {
    initComponents();
    jTextFieldOrdnr.requestFocusInWindow();
    this.brugerHandler = brugerHandler;
    this.rekH = reklamationsHandler;
    startUp();
}

将面板放在GUI中的框架中:

public static void opret(ReklamationHandler reklamationHandler) {
    rHandler = reklamationHandler;
    SwingUtilities.invokeLater(opret);
}

static Runnable opret = new Runnable() {
    @Override
    public void run() {
        JFrame f = jframe;
        f.getContentPane().removeAll();
        JPanel opret = new OpretOpdater(bHandler, rHandler);
        f.getContentPane().add(opret);
        f.pack();
        f.setLocationRelativeTo(null);
    }
};
java swing focus jpanel
2个回答
2
投票

只有当组件可见/显示在容器上或调用requestFocusInWindow()并且所有组件都添加到容器中或者它不起作用时,才应调用pack()

另外请务必在Event Dispatch Thread上创建Swing组件。如果你还没有读过Concurrency in Swing

我之所以提到上述原因,不是在EDT上创建和操作Swing组件会导致代码中的随机伪像。即焦点没有给予等。

下面的代码是为了展示如何在组件可见之前调用requestFocusInWindow不起作用,但在其可见的工作符合预期后调用它。

另请注意,删除SwingUtilities块将导致requestFocusInWindow无法按预期工作(即我们可能会根据我们的运气给予关注或不关注:P):

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Test {

    public Test() {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JTextField f1 = new JTextField(10);

        JTextField f2 = new JTextField(10);

        //f2.requestFocusInWindow(); //wont work (if uncomment this remember to comment the one after setVisible or you wont see the reults)

        JButton b = new JButton("Button");

        JPanel p = new JPanel();

        p.add(f1);//by default first added component will have focus
        p.add(f2);
        p.add(b);

        frame.add(p);

        //f2.requestFocusInWindow();//wont work
        frame.pack();//Realize the components.
        //f2.requestFocusInWindow();//will work
        frame.setVisible(true);

        f2.requestFocusInWindow();//will work
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {//if we remove this block it wont work also (no matter when we call requestFocusInWindow)
            @Override
            public void run() {
                new Test();
            }
        });
    }
}

我建议读一下How to Use the Focus Subsystem


1
投票

通常,在创建字段时指示要聚焦的字段并且在框架变为可见时通过添加请求焦点而不分隔代码是很好的。

看看Dialog Focus,它有一个适用于这种情况的解决方案。使用此方法,您的代码将如下所示:

JTextField f2 = new JTextField(10);
f2.addAncestorListener( new RequestFocusListener() );
© www.soinside.com 2019 - 2024. All rights reserved.