gpt4 book ai didi

Java - 无法将窗口置于最前面

转载 作者:行者123 更新时间:2023-12-04 21:27:56 26 4
gpt4 key购买 nike

我正在尝试执行以下代码:

SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (frame.getExtendedState() == Frame.ICONIFIED)
frame.setExtendedState(Frame.NORMAL);
frame.getGlassPane().setVisible(!frame.getGlassPane().isVisible());

frame.toFront();
frame.repaint();

}
});

不幸的是,这并没有将它从其他窗口后面带到前面......有什么解决办法吗?

最佳答案

根据 setExtendedState 的 API 文档:

If the frame is currently visible on the screen (the Window.isShowing() method returns true), the developer should examine the return value of the WindowEvent.getNewState() method of the WindowEvent received through the WindowStateListener to determine that the state has actually been changed.

If the frame is not visible on the screen, the events may or may not be generated. In this case the developer may assume that the state changes immediately after this method returns. Later, when the setVisible(true) method is invoked, the frame will attempt to apply this state. Receiving any WindowEvent.WINDOW_STATE_CHANGED events is not guaranteed in this case also.

但是,还有一个 windowDeiconified 回调,您可以挂接到 WindowListener 上:

SwingUtilities.invokeLater(new Runnable() {
private final WindowListener l = new WindowAdapter() {
@Override
public void void windowDeiconified(WindowEvent e) {
// Window now deiconified so bring it to the front.
bringToFront();

// Remove "one-shot" WindowListener to prevent memory leak.
frame.removeWindowListener(this);
}
};

public void run() {
if (frame.getExtendedState() == Frame.ICONIFIED) {
// Add listener and await callback once window has been deiconified.
frame.addWindowListener(l);
frame.setExtendedState(Frame.NORMAL);
} else {
// Bring to front synchronously.
bringToFront();
}
}

private void bringToFront() {
frame.getGlassPane().setVisible(!frame.getGlassPane().isVisible());
frame.toFront();
// Note: Calling repaint explicitly should not be necessary.
}
});

关于Java - 无法将窗口置于最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11970570/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com