简体   繁体   English

JTextField不接受输入

[英]JTextField Won't accept Input

So I made a test window with a JTextField in it. 所以我在其中创建了一个带有JTextField的测试窗口。 And I cannot tell whats going wrong. 我不知道什么是错的。 The main code is down below. 主要代码如下。 The problem is that no matter what I do, I cannot edit the text field nor the second one I made. 问题在于,无论我做什么,我都无法编辑文本字段,也无法编辑第二个文本字段。 I have a sample program with a text field that works as well, yet it doesn't work at all. 我有一个带有文本字段的示例程序,但它根本不起作用。

I'm not sure if I need to post it, but I can get a sample jar of the complete program up here. 我不确定是否需要发布它,但我可以在这里获得完整程序的示例jar。 I only posted the area that handles the text Fields 我只发布了处理文本字段的区域

EDIT: The full source is available here: GITHUB 编辑:这里有完整的资源: GITHUB

I removed something and it worked, I give up...\\ 我删除了一些东西,它有效,我放弃......

EDIT2: It turns out it was the fact I was calling a class that extended JPanel, simply calling a new JPanel instead of extending it worked 编辑2:事实证明,我正在调用一个扩展JPanel的类,只是调用一个新的JPanel而不是扩展它的工作

EDIT3: Ok, the problem was the key event dispatcher, the post I marked as the answer explains it in-depth 编辑3:好的,问题是关键事件调度员,我标记为答案的帖子深入解释了它

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

Your problem is as I suspected the KeyEventDispatcher. 您的问题是因为我怀疑KeyEventDispatcher。 When you add it back, and have it return true the JTextField does not work. 当您将其添加回来并让它返回true ,JTextField不起作用。 Per the KeyEventDispatcher API : 根据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. 如果此方法的实现返回false,则KeyEvent将传递给链中的下一个KeyEventDispatcher,以当前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. 如果实现返回true,则假定已调度KeyEvent(尽管不一定是这种情况),并且当前的KeyboardFocusManager将不会对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");
   }
}

Solution: 1) don't have your dispatchKeyEvent(KeyEvent e) return true, unless you do not want the GUI to handle the key stroke. 解决方案:1)除非您不希望GUI处理键击,否则不要让dispatchKeyEvent(KeyEvent e)返回true。 Or 2) even better , don't use this class. 或者2)甚至更好 ,不要使用这个类。 Instead tell us why you feel you need it, and let's help you find a better way. 而是告诉我们为什么你觉得你需要它,让我们帮你找到更好的方法。

1+ to your question for trying to create and post an MCVE. 尝试创建和发布MCVE的问题的1+。

Don't use Thread.sleep() that sometime hangs the whole swing application instead try with Swing Timer that is most suitable for swing application. 不要使用Thread.sleep() ,有时挂起整个swing应用程序而不是尝试使用最适合swing应用程序的Swing Timer

Read more How to Use Swing Timers 阅读更多如何使用摆动计时器

Sample code: 示例代码:

private Timer timer;
...

// wait for 10 milli-seconds
timer = new javax.swing.Timer(10, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // next call
    }
});
timer.setRepeats(true); // you can turn off reputation
timer.start();

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

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