java 创建时如何将焦点设置在 JOptionPane 内的特定 JTextfield 上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6478394/
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-10-30 16:02:21  来源:igfitidea点击:

How to set focus on specific JTextfield inside JOptionPane when created?

javaswingjtextfieldsetfocus

提问by Eng.Fouad

I want to set the focus on a specific JTextField which is passed to the JOptionPane as Object Message. Here is my code (I want the focus on txt2 but the focus always is on txt1):

我想将焦点设置在作为对象消息传递给 JOptionPane 的特定 JTextField 上。这是我的代码(我希望将重点放在 txt2 上,但重点始终放在 txt1 上):

import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TextArea extends JPanel
{
    private JTextArea txt1 = new JTextArea();
    private JTextArea txt2 = new JTextArea();
    public TextArea()
    {
        setLayout(null);
        setPreferredSize(new Dimension(200,100));
        txt1.setBounds (20, 20, 220, 20);
        txt2.setBounds (20, 45, 220, 20);
        txt1.setText("Text Field #1");
        txt2.setText("Text Field #2");
        add(txt1);
        add(txt2);
        txt2.requestFocus();
    }
    private void display()
    {
        Object[] options = {this};
        JOptionPane pane = new JOptionPane();
        pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, txt2);
    }
    public static void main(String[] args)
    {
        new TextArea().display();
    }
}

回答by aioobe

You could let the txt2component request focus once it is added by overriding addNotify. Like this:

你可以让txt2组件请求焦点一旦被覆盖加入addNotify。像这样:

private JTextArea txt2 = new JTextArea() {
    public void addNotify() {
        super.addNotify();
        requestFocus();
    }
};


Here's a fully functional / tested version of your program:

这是您程序的功能齐全/经过测试的版本:

import java.awt.Dimension;
import javax.swing.*;
public class Test extends JPanel {
    private JTextArea txt1 = new JTextArea();
    private JTextArea txt2 = new JTextArea() {
        public void addNotify() {
            super.addNotify();
            requestFocus();
        }
    };

    public Test() {
        setLayout(null);
        setPreferredSize(new Dimension(200, 100));
        txt1.setBounds(20, 20, 220, 20);
        txt2.setBounds(20, 45, 220, 20);
        txt1.setText("Text Field #1");
        txt2.setText("Text Field #2");
        add(txt1);
        add(txt2);
    }

    private void display() {
        Object[] options = { this };
        JOptionPane pane = new JOptionPane();
        pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE, null, options, txt2);
    }

    public static void main(String[] args) {
        new Test().display();
    }
}

回答by camickr

I gave you the answer in your last question (http://stackoverflow.com/questions/6475320/how-to-set-the-orientation-of-jtextarea-from-right-to-left-inside-joptionpane). The concept is the same. Think about the solution given and understand how it works so you can apply it in different situations.

我在上一个问题中给了你答案(http://stackoverflow.com/questions/6475320/how-to-set-the-orientation-of-jtextarea-from-right-to-left-inside-joptionpane)。这个概念是一样的。考虑给出的解决方案并了解它的工作原理,以便您可以将其应用于不同的情况。

If you still don't understand the suggestion then see DialogFocusfor reusable code.

如果您仍然不理解该建议,请参阅DialogFocus以获取可重用代码。

回答by Hovercraft Full Of Eels

Why not use a JDialog or JFrame for this purpose?

为什么不为此目的使用 JDialog 或 JFrame?

   public void display2() {
      JDialog dialog = new JDialog(null, "Title", ModalityType.APPLICATION_MODAL);
      dialog.getContentPane().add(this);
      dialog.pack();
      dialog.setLocationRelativeTo(null);
      txt2.requestFocusInWindow();
      dialog.setVisible(true);
   }

回答by Genhis

You could use a workaround proposed in JDK-5018574 bug report.

您可以使用JDK-5018574 错误报告中提出的解决方法。

Instead of

代替

txt2.requestFocus();

use

利用

txt2.addHierarchyListener(e -> {
    if(e.getComponent().isShowing() && (e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0)
        SwingUtilities.invokeLater(e.getComponent()::requestFocusInWindow);
});

I modified the solution to use Java 8 features. For older versions of Java, see the original workaround.

我修改了解决方案以使用 Java 8 功能。对于旧版本的 Java,请参阅原始解决方法