sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-12-04 21:27:56 26 4
mua khóa gpt4 Nike

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

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

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

}
});

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

1 Câu trả lời

theo 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() {
@Ghi đè
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);
}
};

công khai void run() {
if (frame.getExtendedState() == Frame.ICONIFIED) {
// Add listener and await callback once window has been deiconified.
frame.addWindowListener(l);
frame.setExtendedState(Frame.NORMAL);
} khác {
// 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
Bài viết được đề xuất: java - oracle add_months 函数不同于 Java
Bài viết được đề xuất: .net - 如何跟踪和消除 session 滥用?
Bài viết được đề xuất: java - 是否需要接口(interface)方法的实现
Bài viết được đề xuất: java - 如果方法不适合当前对象,则抛出哪个异常
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com