Application.DoEvents() -> java 中的等效函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3310023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application.DoEvents() -> Equivalent function in java?
提问by oopbase
I know that using threads is more efficient than using c# DoEvents(), but I was still wondering whether there is an equivalent function in java. I googled for it, but I couldn't find anything.
我知道使用线程比使用 c#DoEvents() 更有效,但我仍然想知道 java 中是否有等效的函数。我用谷歌搜索,但我找不到任何东西。
采纳答案by user1704124
You can use Thread.yield(), which is the java counterpart to relinquish the control of the processors voluntarily.
您可以使用Thread.yield(),它是 java 的对应物来自愿放弃对处理器的控制。
回答by amotzg
You can use EventQueue.invokeLater()to append a Runnableafter all pending events. This have a result similar to C#'s DoEvents()that comes before the code you put inside the Runnable.run()method.
您可以使用在所有未决事件之后EventQueue.invokeLater()附加一个Runnable。这有一个类似于 C# 的结果DoEvents(),它出现在您放入Runnable.run()方法中的代码之前。
See Java documentation for EventQueue.
For example, if you want to let all GUI controls to lose the focus and their lost focus events to be execute, you can use the following code:
例如,如果要让所有 GUI 控件失去焦点并执行其失去焦点事件,则可以使用以下代码:
@Override
public void windowClosing(WindowEvent e){
// Clear the focus to allow last changes to be noted.
KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
// We want to let other events (e.g. lost focus) run before we start closing.
EventQueue.invokeLater( new Runnable() {
@Override public void run() {
// Do actual closing...
}
});
}

