【问题标题】:How to set focus on specific JTextfield inside JOptionPane when created?创建时如何将焦点设置在 JOptionPane 内的特定 JTextfield 上?
【发布时间】:2011-06-25 14:31:52
【问题描述】:

我想将焦点设置在作为对象消息传递给 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();
    }
}

【问题讨论】:

  • 我没有见过这种类型的 JOptionPane 以这种方式使用,我不确定它是否是正确的方法。为什么不使用 JDialog?
  • 这种方法没有错。它通过完全创建自定义对话框来节省重新发明轮子。您需要做的就是创建自定义面板。
  • @camickr:Rob,我不同意,因为如果你仔细查看他的代码,他将他的 JPanel 添加到对象中似乎 非常 奇怪(至少对我来说) [] JOptionPane showOptionDialog方法的options变量(第7个参数),没有放入Object变量(第2个变量)。你以前这样做过吗?我知道我没有。
  • @Eng Fouad,我很失望你根据发布的代码采取了解决方案。它表明您只是复制解决方案,而不是花时间了解给出的其他解决方案。我为您提供了一个简单的单行解决方案,您无需对组件进行子类化即可使用该解决方案。可重复使用的解决方案通常比定制解决方案更受欢迎。您不是通过复制解决方案来学习,而是通过理解给出的建议来学习。
  • @Hovercraft & @camickr:对不起,我只关心如何将焦点放在某些 JTextField 上,我很快就写了这个(SSCCE)

标签: java swing jtextfield setfocus


【解决方案1】:

您可以通过覆盖addNotify 来让txt2 组件请求焦点。像这样:

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

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

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

【讨论】:

  • 并且您还将 JPanel 添加到 JOptionPane showOptionDialog 方法的选项参数中。你不觉得这很奇怪吗?
  • 同意。这可能不是实现对话的最佳方式。将其视为快速的“最小”修复。
【解决方案2】:

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

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

【讨论】:

  • +1 这也有效,但 aioobe 先回答问题,所以我接受他的回答。无论如何谢谢:)
  • @Eng Fouad,你是什么意思?我提前3分钟给出了答案!另外,我昨天也给了你这个概念。无论如何,应该根据可重用性和其他设计考虑因素来选择答案。您不只是因为有人发布代码而接受答案。您需要花时间了解代码的含义,以便在其他情况下使用代码或其概念。
【解决方案3】:

为什么不为此目的使用 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);
   }

【讨论】:

    【解决方案4】:

    您可以使用JDK-5018574 bug report 中提出的解决方法。

    而不是

    txt2.requestFocus();
    

    使用

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多