简体   繁体   English

JTextField焦点

[英]JTextField focus

I have a frame and a panel.Permanently I remove the panel and add another panel.After adding a new panel I need the JTextField to get focused.How can I do this? 我有一个框架和一个面板。永久地我删除面板并添加另一个面板。添加一个新面板后,我需要JTextField来集中注意力。我怎么能这样做?

I tried panel.requestFocus() method but it didnt work. 我尝试了panel.requestFocus()方法,但它没有用。

Example Code: 示例代码:

public class Test{
    public static void main(String[] args){

        JFrame frame = new JFrame();
        // ... frame options

        // MyPanel extends JPanel
        // and has a JTextField
        contentPane.add(new MyPanel());

        // Permanently I need to add another panel
        contentPane.removeAll();
        contentPane.add(new MyPanel());

    }
}

Calling panel.requestFocus() attempts to give focus to the container itself rather than on any of its child components. 调用panel.requestFocus()尝试将焦点放在容器本身而不是其任何子组件上。

Use requestFocusInWindow on the JTextField after the component has been added to the JFrame . 将组件添加到JFrame后,在JTextField上使用requestFocusInWindow Add an public method in MyPanel for calling this method. MyPanel添加一个公共方法来调用此方法。

Avoid using requestFocus . 避免使用requestFocus From the docs : 来自文档

requestFocus, is discouraged because it tries to give the focus to the component's window, which is not always possible. 不鼓励requestFocus,因为它试图将焦点放在组件的窗口上,这并不总是可行的。 As of JDK 1.4, you should instead use the requestFocusInWindow method, which does not attempt to make the component's window focused. 从JDK 1.4开始,您应该使用requestFocusInWindow方法,该方法不会尝试使组件的窗口集中。

public class Test {
    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Focus JTextField");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                MyPanel myPanel = new MyPanel();
                frame.add(myPanel);
                frame.setVisible(true);
                frame.pack();
                myPanel.focusTextField();
            }
        });
    }
}

class MyPanel extends JPanel {
    private JTextField textField;

    public MyPanel() {
        textField = new JTextField(20);
        add(textField);
    }

    public void focusTextField() {
        textField.requestFocusInWindow();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 100);
    }
}

Add a method like this to MyPanel: 将这样的方法添加到MyPanel:

public void gainFocus() {
    tf.requestFocus();
}

Call it from the main method, or elsewhere whenever you need it to be focused. 无论何时需要聚焦,都可以从main方法或其他地方调用它。

You will need a method either to get the TextField from MyPanel , like getTextField , or a method to just directly focus on the TextField. 您将需要一个方法来从MyPanel获取TextField,如getTextField ,或者只是直接关注TextField的方法。 These methods must be inside your MyPanel class. 这些方法必须在MyPanel类中。

Example method: 示例方法:

public class MyPanel extends JPanel {

private JTextField textField;

//your code here

    public void getTextFieldFocus() {
        textField.requestFocus();
    }

}

Then you call this getTextFieldFocus method when you need to focus. 然后在需要聚焦时调用此getTextFieldFocus方法。

Else, if you extract the TextField from the MyPanel class using a getTextField method, you call this when you need the focus: 另外,如果使用getTextField方法从MyPanel类中提取TextField,则在需要焦点时调用它:

panel.getTextField().requestFocus();

Use. 采用。 textfield.setText(""); textfield.setText( “”); when you need to get the focus or try something like you will take your control to your field try 当你需要获得焦点或尝试类似的东西时,你会把你的控制权带到你的领域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM