[英]Java modal window with maximize button
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
?
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. 使用一些技巧可以添加一个maximise
按钮,但它将完全违背JDialog
工作方式。 If you need a maximise button, the best solution would be using a JWindow
or a JFrame
instead of a JDialog
. 如果您需要最大化按钮,最好的解决方案是使用JWindow
或JFrame
而不是JDialog
。 Those windows support maximisation and minimisation. 那些窗口支持最大化和最小化。
WARNING: You shouldn't do that, no matter what. 警告:无论如何,你都不应该这样做。
A trick to do this in JDialog
: 在JDialog
执行此操作的技巧:
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
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();}
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. 这是来自现已解散的JRoller网站的Santhosh Kumar Tekturi撰写的博客文章和代码的github档案。
It is a complete utility class that makes a Frame mimic a Dialog, similar to the other answers. 它是一个完整的实用程序类,它使Frame模仿对话框,类似于其他答案。 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添加一个WindowListener
以保持框架在其所有者之上并保持其所有者框架被禁用(警告:在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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.