简体   繁体   English

"在 for 循环中不会读取来自 jtextfield 的输入"

[英]Input from jtextfield doesnt get read while in a for loop

My problem is that I have a for loop and while this loop is running I still want inputs from JTextField to get read and not interrupt the loop or timing when the line gets printed.我的问题是我有一个 for 循环,当这个循环正在运行时,我仍然希望 JTextField 的输入被读取,而不是在打印行时中断循环或计时。

public class test {

    public void Play() {

        JTextField textField = new JTextField();
        textField.addKeyListener(new KeyChecker());
        JFrame jframe = new JFrame();
        jframe.add(textField);
        jframe.setSize(200, 30);
        jframe.setVisible(true);
    }
}

class KeyChecker extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent event)
    {
        for(int i = 0; i < 1; i = 0)
        {
            char Input = event.getKeyChar();
            if(Input == 'a')
            {
                System.out.println("working");
            } else {
                System.out.println("not working");
            }
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
            }
        }
    }
}

In your code, the keyPressed method gets called by the Event Dispatch Thread ( https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html ) every time you press a key in the textfield.在您的代码中,每次按下文本字段中的键时,事件调度线程 ( https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html ) 都会调用keyPressed方法。

In this context the KeyEvent event parameter represents a sing key press event, so it makes no sense to create a loop inside an event method, as maloomeister correctly pointed out.在这种情况下, KeyEvent event参数表示一个按键按下事件,因此正如 maloomeister 正确指出的那样,在事件方法中创建循环是没有意义的。

Instead you should perform your logic using the state of your classes, either KeyChecker or test .相反,您应该使用类的状态( KeyCheckertest )执行您的逻辑。 I don't really understand why you should regularly check for a key being pressed (and the purpose of a textfield BTW), anyway the following is a possible solution using a separate thread.我真的不明白为什么你应该定期检查被按下的键(以及文本字段 BTW 的目的),无论如何,以下是使用单独线程的可能解决方案。 Of course you can also do the loop in the main thread (which is not the EDT), but in a non-trivial application you should never do that.当然,您也可以在主线程(不是EDT)中执行循环,但在非平凡的应用程序中,您永远不应该这样做。

import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Test {
    public static void main(String[] args) {
        KeyChecker keyChecker = new KeyChecker();
        JTextField textField = new JTextField(40);
        textField.addKeyListener(keyChecker);
        JFrame jframe = new JFrame();
        jframe.add(textField);
        jframe.pack();
        jframe.setVisible(true);

        Thread t = new Thread(() -> {
            while(true) {
                char currentKey = keyChecker.getCurrentPressedKey();
                switch (currentKey) {
                    case 'a':
                        System.out.println("working");
                        break;
                    default:
                        System.out.println("not working");
                }
                    
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    break;
                }
            }
        });
        
        t.start();
    }
}

class KeyChecker extends KeyAdapter {
    private char currentPressedKey = 0;

    @Override
    public void keyPressed(KeyEvent event) {
        currentPressedKey = event.getKeyChar();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        currentPressedKey=0;
    }
    
    public char getCurrentPressedKey() {
        return currentPressedKey;
    }
}

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

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