只是一个关于 SwingUtilities.InvokeLater() 的问题。
据我了解,每次更新 Swing 界面时,我都需要调用 SwingUtilities.InvokeLater 才能进入 EDT。如果我尝试从按钮监听器更新 GUI,是否需要执行此操作,因为按钮事件已在 EDT 上?
也就是说,我必须......
public void mouseClicked(MouseEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//GUI updates
}
});
}
或者我是否能够...
public void mouseClicked(MouseEvent e) {
//GUI updates
}
此外,如果我在对象上调用将更新 GUI 的方法,是否应用相同的逻辑?
最佳答案
any time I update my Swing interface I need to call SwingUtilities.InvokeLater to get onto the EDT
正确。这包括您更新组件模型的任何时间,因为这也会导致组件的重新绘制。
Does this need to be done if I am attempting to update my GUI from a button listener, as they button events are already on the EDT?
再次正确。由于代码会在 EDT 上自动调用,因此您无需使用 invokeLater() 手动将其添加到 EDT。
如果您的代码在单独的线程
上执行并且部分逻辑需要更新 GUI 组件,则通常使用 SwingUtilities.invokeLater()。
关于java - SwingUtilities.InvokeLater 和 ButtonListeners,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53002147/