[英]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.