简体   繁体   English

当我在JTextArea中按Enter时,我想使用Key Listener进行监听,但是它不起作用

[英]I want to use Key Listener to listen for when I press enter in a JTextArea, but it isn't working

I made a JTextArea with a new Key Listener with this line in my class SetUp: 我在类SetUp中的这一行创建了一个带有新键监听器的JTextArea:

startWin is from my Window class, and text is the JTextArea object in that class startWin来自我的Window类,文本是该类中的JTextArea对象

StrToInt is a method that converts a string to an int without using the parse methods StrToInt是一种将字符串转换为int而不使用parse方法的方法

        startWin.text.addKeyListener(new KeyListener() {

        int s;
        @Override
        public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyTyped(KeyEvent e) {

            if(e.getKeyCode() == KeyEvent.VK_ENTER){
            start = setup.StrToInt(startWin.text.getText()); //start is static
        }

        }
    });
    s = start;
    return s;

My goal was to input a value in text, and press enter to return it and store it to start. 我的目标是在文本中输入一个值,然后按Enter键将其返回并存储以开始。

Yet when I do this: 但是当我这样做时:

public void Maze(Window w) {
    int start = 0;

    w = setup.StartWindow(w);
    while (start == 0) {
        start = setup.Start(w);
    }
}

I get a runtime error. 我收到运行时错误。 What am I doing wrong? 我究竟做错了什么? thanks 谢谢

I looked and saw one problem with your "int s;" 我看了看,发现您的“整数”有一个问题; (was outside of method before) (在方法之前)

  int s;
  startWin.text.addKeyListener(new KeyListener() {


    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {

        if(e.getKeyCode() == KeyEvent.VK_ENTER){
        start = setup.StrToInt(startWin.text.getText()); //start is static
        s = start;
    }

    }
});
  return s;

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

相关问题 当我按下一个键时,不会调用keyPressed()方法吗? - The keyPressed() method isn't being called when I press a key? 当我按下按钮时,动作监听器不起作用 - Action Listener is not working when I press a Button Java BufferedReader-输出的最后一行不会自动读取,仅当我按Enter键时才读取 - Java BufferedReader - the last line of output isn't read automatically, it reads only when i press enter 关键侦听器无法正常工作 - Key Listener isn't working 当我按Enter键时如何隐藏JWindow? - How to hide the JWindow when I press Enter key? 当用户按Enter时禁用JTextArea调整大小 - Disabling JTextArea resizing when user press Enter Java Listener无法正常运行,除非我按下Enter键,一旦文本框更改,如何启用它 - Java Listener not working unless i press enter how to enable it working once text box changed 为什么当我按Enter键时程序不会停止 - Why won't my program stop when I press enter 当我按下按钮时,如何更改JTextArea中的文本? - How do I change the text in a JTextArea when I press a button? 为什么当我输入号码时我的编程不起作用:600851475143 - Why isn't my programming working when I enter the number: 600851475143
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM