1 回答
TA贡献1804条经验 获得超7个赞
您可以使用EventQueue.invokeLater来确保您的 updateUiChanged 调用发生在 WindowEvent 之后。您可以在私有实例字段中跟踪 WindowEvent 的发生,因此延迟的 ComponentListener 代码可以知道调整大小是否与 WindowEvent 配对:
private boolean iconifyStateChanged;
// ...
wndMinMax = new WindowAdapter(){
public void windowIconified(WindowEvent e){
if(debug)debug("Window Iconfied!!");
iconifyStateChanged = true;
}
public void windowDeiconified(WindowEvent e){
if(debug)debug("Window DeIconfied!!");
iconifyStateChanged = true;
}
};
this.addWindowListener(wndMinMax);
addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e) //V.43
{
if (e.getSource() instanceof JFrame) //V.43
{
// This will run after any pending WindowEvents.
EventQueue.invokeLater(() -> {
if (!iconifyStateChanged)
{
updateUiChanged(e.getComponent().getWidth(),
e.getComponent().getHeight()));
}
iconifyStateChanged = false;
});
}
}
});
(getWidth()和getHeight()已经返回 int 值,因此不需要强制转换为 int。)
添加回答
举报