如果我有一个方法需要返回一些东西,并且它需要在 UI 上显示一些东西来做到这一点(例如问用户一个问题),我java.awt.EventQueue.invokeLater(new Runnable() {...});
会在 UI 线程上运行它(Android 使用类似的东西runOnUiThread()
) . 我如何从中返回一些东西Runnable
?例如:
public String askQuestion()
{
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
String response = JOptionPane.showInputDialog(null, "What is 2+2?");
//how do I return this response object to the method?
}
});
}
我不能使用最终的局部变量,因为编译不允许我分配给它。我可以使用一个类变量并使用它来访问它,Class.this.<variable>
但这是实现这一目标的最佳/唯一方法吗?
编辑:我需要返回其他东西String
,这只是一个例子。