简体   繁体   English

Java Swing表单,不能在新添加的jtextfield中键入文本

[英]Java Swing form and cannot type text in newly added jtextfield

I was asked to change a previously made java swing form to add a JTextField which would respond to keystrokes. 我被要求更改以前制作的java swing表单以添加一个响应键击的JTextField I added the text field to the form, tried to force focus on it with no success and even if I click on it, it does not get the focus nor lets me input any text on it. 我将文本字段添加到表单中,尝试强制关注它而没有成功,即使我点击它,它也没有获得焦点,也没有让我输入任何文本。 I tryed to add a key listener to the JPanel (which is the base of the form) so that when I type text, it would keep it in a string variable and dump it to the method as soon as I pressed return. 我尝试向JPanel (它是表单的基础)添加一个键监听器,这样当我输入文本时,它会将它保存在一个字符串变量中,并在我按下return后立即将其转储到方法中。

I need help to sort out what's happening. 我需要帮助来理清正在发生的事情。 Did not post any code, because the form is copyrighted to its fullest. 没有发布任何代码,因为该表格受版权保护。 Really need some ideas on how to get around this problem. 真的需要一些关于如何解决这个问题的想法。

Here's the part of the code where I try to add the listener. 这是我尝试添加监听器的代码部分。

public void actionPerformed(ActionEvent e) {
    MyButton jb = (MyButton) e.getSource();
    String buttonGetName = jb.getName();

    /* MÉTODO PARA CAPTURA DE ENTRADAS POR TECLADO / LEITOR DE COD BARRAS */
    back.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                barcode.setText(sBarcode);
                System.out.println("sBarcode");
                sBarcode = null;
            } else {
                System.out.println(e.getKeyChar());
                sBarcode = sBarcode + e.getKeyChar();
            }
        }
    }); 

... some more code here that reads which button is pressed in an array of buttons. ...这里有一些代码可以读取按钮数组中按下的按钮。

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

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