java - JTextField 不接受输入

标签 java jtextfield

所以我制作了一个带有 JTextField 的测试窗口。我不知道出了什么问题。主要代码在下面。问题是无论我做什么,我都无法编辑文本字段,也无法编辑我制作的第二个文本字段。我有一个带有文本字段的示例程序,它也可以正常工作,但它根本不起作用。

我不确定是否需要发布它,但我可以在此处获得完整程序的示例 jar。我只发布了处理文本字段的区域

编辑:完整的资源可在此处获得:GITHUB

我删除了一些东西并且它起作用了,我放弃了...\

EDIT2:事实证明我正在调用一个扩展 JPanel 的类,只需调用一个新的 JPanel 而不是扩展它就可以了

EDIT3:好的,问题出在关键事件调度器上,我标记为答案的帖子对此进行了深入解释

 public class Main {
    private static JPanel mainPanel = new JPanel();
    private static JFrame frame;
    public static JTextField textField1 = new JTextField();
    public static JTextField textField2 = new JTextField();
    private static GroupLayout layout = new GroupLayout(mainPanel);

    public static void main(String[] Args) throws InterruptedException{
        frame = new JFrame("Test Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPanel.setLayout(layout);
        layout.setAutoCreateContainerGaps(false);
        layout.setAutoCreateGaps(false);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.addGap(200).addGroup(layout.createParallelGroup().addComponent(textField1, 25, 25, 25).addComponent(textField2, 25, 25, 25).addGap(350));
        hGroup.addGap(300)
        .addGroup(layout.createParallelGroup().addComponent(textField1, 200, 200, 200).
                addComponent(textField2, 200, 200, 200)).addGap(300);
        layout.setVerticalGroup(vGroup);
        layout.setHorizontalGroup(hGroup);
        frame.add(mainPanel);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        textField1.setText("I am a simple uneditable testbox");
    }
}

最佳答案

我怀疑您的问题是 KeyEventDispatcher。当您将它添加回去并让它返回 true 时,JTextField 不起作用。根据 KeyEventDispatcher API :

If an implementation of this method returns false, then the KeyEvent is passed to the next KeyEventDispatcher in the chain, ending with the current KeyboardFocusManager. If an implementation returns true, the KeyEvent is assumed to have been dispatched (although this need not be the case), and the current KeyboardFocusManager will take no further action with regard to the KeyEvent.

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Main {
   private static JPanel mainPanel = new JPanel();
   private static JFrame frame;
   public static JTextField textField1 = new JTextField(20);
   public static JTextField textField2 = new JTextField(20);

   public static void main(String[] Args) throws InterruptedException {
      frame = new JFrame("Test Window");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      final JCheckBox dispatchKeyEventReturnCheckBox = 
            new JCheckBox("Dispatch Key Event Return Value", true);

      mainPanel.add(textField1);
      mainPanel.add(textField2);
      mainPanel.add(dispatchKeyEventReturnCheckBox);
      frame.add(mainPanel);

      KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addKeyEventDispatcher(new KeyEventDispatcher() {

               @Override
               public boolean dispatchKeyEvent(KeyEvent evt) {
                  // TODO Fix this!!!
                  // !! return false;
                  return dispatchKeyEventReturnCheckBox.isSelected();
               }
            });

      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      textField1.setText("I am a simple uneditable testbox");
   }
}

解决方案:1) 不要让您的 dispatchKeyEvent(KeyEvent e) 返回 true,除非您不希望 GUI 处理击键。或者 2) 甚至更好,不要使用此类。相反,请告诉我们您为什么需要它,让我们帮助您找到更好的方法。

1+ 尝试创建和发布 MCVE 的问题。

关于java - JTextField 不接受输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24811142/

相关文章:

java - 无法将数据从 Activity C 通过 Activity B 移动到 Activity A

java - 无法为GUI更改计算器创建“重置”按钮

java - JDialog 未针对新输入进行更新

java - 未经检查从 X 转换为扩展 X 的泛型类型

JavaFX 未找到所有系统字体

java - 使用paintComponent在JFrame中镜像对象

java - 在 jtextfield 中验证 'price' 的正则表达式是什么

java - 制作更改程序GUI

java - 如何将 JTextField 添加到具有 JButton 的 Jpanel

java - 如何将 swing JTextField 组件转换为扩展它的类型的对象