-1

我有一个用户按下回车键的事件处理程序,我需要它从另一个方法返回一个值。本质上,如果您return "hi";在方法 b 中有事件处理程序,并且在方法 A 中有事件处理程序,我需要一行代码来返回“hi”。(注意,在代码中,“hi”会有所不同)。有没有办法做到这一点?

4

2 回答 2

5

return只需在语句中调用您的方法

public int method1() {
    return method2(); //return the return value of method2
}

public int method2() {
    return 9;
}
于 2013-09-20T20:10:27.987 回答
0

我不确定您要做什么,但我认为您可以使用Thread.interrupted(). 像这样的东西:

public class KeyHandler extends KeyAdapter {
    private Thread otherThread;

    public KeyHandler(Thread otherThread) {
        this.otherThread = otherThread;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER) otherThread.interrupt();
    }
}

public class OtherClass {
    public void someMethod() {
        while(!Thread.isInterrupted()) {
            // dosomething
        }
        return;
    }
}
于 2013-09-20T20:15:21.520 回答