带有最大化按钮的 Java 模态窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3897927/
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
Java modal window with maximize button
提问by user
How could I create a window which is modal and has a maximize button?
So is it possible to create a modal JFrame
or create a JDialog
with maximize button?
如何创建一个模态窗口并具有最大化按钮?
那么是否可以创建模态JFrame
或创建JDialog
带有最大化按钮的按钮?
回答by Colin Hebert
On most look and feels, modal windows (such as JDialog
) do not have a maximise button simply because they're not supposed to be maximised (or minimised) at all.
在大多数外观上,模态窗口(例如JDialog
)没有最大化按钮,仅仅是因为它们根本不应该被最大化(或最小化)。
It's possible with some tricks to add a maximise
button, but it would be completly against the way JDialog
is supposed to work.
If you need a maximise button, the best solution would be using a JWindow
or a JFrame
instead of a JDialog
. Those windows support maximisation and minimisation.
可以通过一些技巧来添加maximise
按钮,但这将完全违背JDialog
应该的工作方式。如果您需要最大化按钮,最好的解决方案是使用 aJWindow
或 aJFrame
而不是 a JDialog
。这些窗口支持最大化和最小化。
WARNING:You shouldn't do that, no matter what.
警告:无论如何,您都不应该这样做。
A trick to do this in JDialog
:
一个技巧来做到这一点JDialog
:
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
回答by sillo01
Solution 1: Tested on Windows
解决方案 1:在 Windows 上测试
I used a JFrame for the modal window
我为模态窗口使用了 JFrame
JFrame mainWindow = new JFrame;
mainWindow.setVisible(true);
JFrame modalWindow = new JFrame();
// The next two sentences gives modalWindow modal beahaviour
mainWindow.setEnabled(false);
mainWindow.setFocusable(false);
modalWindow.setVisible(true);
Solution 2: Tested on Ubuntu
解决方案 2:在 Ubuntu 上测试
I added a WindowFocusListener
我添加了一个 WindowFocusListener
addWindowFocusListener(new java.awt.event.WindowFocusListener() {
public void windowGainedFocus(java.awt.event.WindowEvent evt) {}
public void windowLostFocus(java.awt.event.WindowEvent evt) {
formWindowLostFocus(evt);}
private void formWindowLostFocus(java.awt.event.WindowEvent evt) {
this.requestFocus();
this.toFront();}
回答by Pixel
Here is an alternate / slightly more detailed answer.
这是一个替代/稍微更详细的答案。
Try Are You Missing Maximize Button?(formerly here). This is a github archive of blog articles and code by Santhosh Kumar Tekturi from the now defunct JRoller site.
试试你是否缺少最大化按钮?(以前在这里)。这是 Santhosh Kumar Tekturi 来自现已不存在的 JRoller 站点的博客文章和代码的 github 存档。
It is a complete utility class that makes a Frame mimic a Dialog, similar to the other answers. It involves adding a WindowListener
to the Frame to keep the frame on top of its owner and keep its owner frame disabled (warning: in the windowClosed
method it should probably be frame.removeWindowListener(this);
, and a WindowListener
to the owner to keep the frame on top of it and to remove the listener. It also uses its own EventQueue
to process events. Note this is an old post, so as mentioned in the code there may be newer APIs to deal with this code better.
它是一个完整的实用程序类,它使 Frame 模仿 Dialog,类似于其他答案。它涉及向WindowListener
框架添加 a以将框架保持在其所有者的顶部并保持其所有者框架处于禁用状态(警告:在windowClosed
方法中它可能应该是frame.removeWindowListener(this);
,并向WindowListener
所有者添加一个以将框架保持在其顶部并移除监听器。它也使用自己的EventQueue
来处理事件。注意这是一个旧帖子,因此正如代码中提到的那样,可能会有更新的API来更好地处理此代码。
Here is the core function. See the link for the rest. Note: the code in the repository differs from the article; I believe the repository is more developed.
这里是核心功能。其余的见链接。注意:存储库中的代码与文章不同;我相信存储库更发达。
// show the given frame as modal to the specified owner.
// NOTE: this method returns only after the modal frame is closed.
public static void showAsModal(final Frame frame, final Frame owner){
frame.addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
owner.setEnabled(false);
}
public void windowClosing(WindowEvent e) {
owner.setEnabled(true);
}
public void windowClosed(WindowEvent e){
frame.removeWindowListener(this); // originally called on owner
}
});
owner.addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent e){
if(frame.isShowing()){
frame.setExtendedState(JFrame.NORMAL);
frame.toFront();
}else
owner.removeWindowListener(this);
}
});
owner.toFront();
frame.setVisible(true);
try{
new EventPump(frame).start();
} catch(Throwable throwable){
throw new RuntimeException(throwable);
}
}