• <bdo id='9DN6M'></bdo><ul id='9DN6M'></ul>

  • <legend id='9DN6M'><style id='9DN6M'><dir id='9DN6M'><q id='9DN6M'></q></dir></style></legend>

    <small id='9DN6M'></small><noframes id='9DN6M'>

    1. <tfoot id='9DN6M'></tfoot>
      <i id='9DN6M'><tr id='9DN6M'><dt id='9DN6M'><q id='9DN6M'><span id='9DN6M'><b id='9DN6M'><form id='9DN6M'><ins id='9DN6M'></ins><ul id='9DN6M'></ul><sub id='9DN6M'></sub></form><legend id='9DN6M'></legend><bdo id='9DN6M'><pre id='9DN6M'><center id='9DN6M'></center></pre></bdo></b><th id='9DN6M'></th></span></q></dt></tr></i><div id='9DN6M'><tfoot id='9DN6M'></tfoot><dl id='9DN6M'><fieldset id='9DN6M'></fieldset></dl></div>

        使 Java Swing 模态对话框的行为类似于 Mac OSX 对话框

        时间:2023-10-14

        <tfoot id='zG7CJ'></tfoot>
      1. <small id='zG7CJ'></small><noframes id='zG7CJ'>

        • <legend id='zG7CJ'><style id='zG7CJ'><dir id='zG7CJ'><q id='zG7CJ'></q></dir></style></legend>

              <tbody id='zG7CJ'></tbody>
                • <bdo id='zG7CJ'></bdo><ul id='zG7CJ'></ul>
                • <i id='zG7CJ'><tr id='zG7CJ'><dt id='zG7CJ'><q id='zG7CJ'><span id='zG7CJ'><b id='zG7CJ'><form id='zG7CJ'><ins id='zG7CJ'></ins><ul id='zG7CJ'></ul><sub id='zG7CJ'></sub></form><legend id='zG7CJ'></legend><bdo id='zG7CJ'><pre id='zG7CJ'><center id='zG7CJ'></center></pre></bdo></b><th id='zG7CJ'></th></span></q></dt></tr></i><div id='zG7CJ'><tfoot id='zG7CJ'></tfoot><dl id='zG7CJ'><fieldset id='zG7CJ'></fieldset></dl></div>
                  本文介绍了使 Java Swing 模态对话框的行为类似于 Mac OSX 对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个小型应用程序,它需要 ProgressBar 显示在框架的 TitleBar 下方,这在 Mac OSX 应用程序中很常见.我有两个问题:

                  1.我已经管理了定位,但我不得不硬编码父 Frame 的 TitleBar 高度.是否有一种软"方法来获取 TitleBar 的高度?

                  在Dialog的构造函数中:

                   维度 dimensionParentFrame = parent.getSize();维度 dimensionDialog = getSize();int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width)/2);setLocation(x, parent.getY() + 22);//TODO 硬代码警告标题高度

                  2.即使对话框是模态的,我仍然可以单击父框架并移动它.如何使对话框粘"到父框架?也就是说,当父 Frame 移动时,Dialog 会随之移动,就好像附加了一样.

                  任何帮助/指针将不胜感激.

                  代码如下:

                  <代码>导入 javax.swing.JFrame;导入 javax.swing.JButton;导入 java.awt.BorderLayout;导入 java.awt.EventQueue;导入 java.awt.event.ActionListener;导入 java.awt.event.ActionEvent;公共类 ModalDialogDemoFrame 扩展了 JFrame{ModalDialogDemoFrame modalDialogDemo;公共 ModalDialogDemoFrame(){modalDialogDemo = 这个;setBounds(100, 100, 400, 400);设置默认关闭操作(JFrame.EXIT_ON_CLOSE);JButton buttonDialog = new JButton("打开对话框");buttonDialog.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent arg0){//创建一个以此框架为父的模态对话框.ModalDialog modalDialog = new ModalDialog(modalDialogDemo, true);modalDialog.setVisible(true);}});getContentPane().add(buttonDialog, BorderLayout.CENTER);}/*** @param 参数*/公共静态无效主要(字符串 [] 参数){EventQueue.invokeLater(new Runnable(){公共无效运行(){尝试{ModalDialogDemoFrame window = new ModalDialogDemoFrame();window.setVisible(true);}捕获(异常 e){e.printStackTrace();}}});}}导入 java.awt.Dimension;导入 javax.swing.JDialog;导入 javax.swing.JFrame;导入 javax.swing.JButton;导入 java.awt.BorderLayout;导入 java.awt.event.ActionListener;导入 java.awt.event.ActionEvent;公共类 ModalDialog 扩展了 JDialog{公共模态对话框(JFrame 父级,布尔模态){超级(父,模态);维度 dimensionParentFrame = parent.getSize();setSize(new Dimension((parent == null) ? 300 : dimensionParentFrame.width/2, 75));维度 dimensionDialog = getSize();int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width)/2);setLocation(x, parent.getY() + parent.getInsets().top);设置未装饰(真);设置模态(模态);setModalityType(ModalityType.APPLICATION_MODAL);设置默认关闭操作(DISPOSE_ON_CLOSE);JButton buttonClose = new JButton("关闭");buttonClose.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){处置();}});getContentPane().add(buttonClose, BorderLayout.CENTER);}}

                  解决方案

                  int titleBarHeight = frame.getInsets().top;

                  <块引用>

                  即使对话框是模态的,我仍然可以单击父框架并移动它.

                  那么你做错了,因为这不应该发生.

                  发布您的 SSCCE 来证明问题.

                  I am writing a small app that requires a ProgressBar to appear centred under the frame's TitleBar as is often seen in Mac OSX apps. I have two problems:

                  1. I have managed the positioning but I had to hard code the parent Frame's TitleBar height. Is there a 'soft' way to get the TitleBar's height?

                  In the Dialog's constructor:

                   Dimension dimensionParentFrame = parent.getSize();
                   Dimension dimensionDialog = getSize();
                   int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width)/2);
                   setLocation(x, parent.getY() + 22);              // TODO HARD CODE WARNING TITLE HEIGHT
                  

                  2. Even though the Dialog is modal, I can still click on the parent Frame and move it. How can I make the Dialog 'stick' to the parent Frame? That is, when the parent Frame is moved the Dialog moves with it as if attached.

                  Any help/pointers would be much appreciated.

                  Here is the code:

                  
                  
                      import javax.swing.JFrame;
                      import javax.swing.JButton;
                      import java.awt.BorderLayout;
                      import java.awt.EventQueue;
                      import java.awt.event.ActionListener;
                      import java.awt.event.ActionEvent;
                  
                  
                      public class ModalDialogDemoFrame extends JFrame
                      {
                        ModalDialogDemoFrame modalDialogDemo;
                        public ModalDialogDemoFrame() 
                        {
                          modalDialogDemo = this;
                          setBounds(100, 100, 400, 400);
                          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          JButton buttonDialog = new JButton("Open Dialog");
                          buttonDialog.addActionListener(new ActionListener() 
                          {
                            public void actionPerformed(ActionEvent arg0) 
                            {
                              // Create a Modal Dialog with this Frame as Parent.
                              ModalDialog modalDialog = new ModalDialog(modalDialogDemo, true);
                              modalDialog.setVisible(true);
                            }
                          });
                          getContentPane().add(buttonDialog, BorderLayout.CENTER);
                        }
                  
                        /**
                         * @param args
                         */
                        public static void main(String[] args)
                        {
                          EventQueue.invokeLater(new Runnable()
                          {
                            public void run()
                            {
                              try
                              {
                                ModalDialogDemoFrame window = new ModalDialogDemoFrame();
                                window.setVisible(true);
                              }
                              catch (Exception e)
                              {
                                e.printStackTrace();
                              }
                            }
                          });
                        }
                  
                      }
                  
                      import java.awt.Dimension;
                  
                      import javax.swing.JDialog;
                      import javax.swing.JFrame;
                      import javax.swing.JButton;
                      import java.awt.BorderLayout;
                      import java.awt.event.ActionListener;
                      import java.awt.event.ActionEvent;
                  
                  
                      public class ModalDialog extends JDialog
                      {
                        public ModalDialog(JFrame parent, boolean modal) 
                        {
                          super(parent, modal);
                          Dimension dimensionParentFrame = parent.getSize();
                          setSize(new Dimension((parent == null) ? 300 : dimensionParentFrame.width / 2, 75));
                          Dimension dimensionDialog = getSize();
                          int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width)/2);
                          setLocation(x, parent.getY() + parent.getInsets().top);
                          setUndecorated(true);
                          setModal(modal);
                          setModalityType(ModalityType.APPLICATION_MODAL);
                          setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                  
                          JButton buttonClose = new JButton("Close");
                          buttonClose.addActionListener(new ActionListener() 
                          {
                            public void actionPerformed(ActionEvent e) 
                            {
                              dispose();
                            }
                          });
                          getContentPane().add(buttonClose, BorderLayout.CENTER);
                        }
                  
                      }
                  
                  

                  解决方案

                  int titleBarHeight = frame.getInsets().top;
                  

                  Even though the Dialog is modal, I can still click on the parent Frame and move it.

                  Then you are doing something wrong because this should NOT happen.

                  Post your SSCCE that demonstrates the problem.

                  这篇关于使 Java Swing 模态对话框的行为类似于 Mac OSX 对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在模态 JDialog 之外时光标不正确? 下一篇:使用 SWT 显示父模式对话框

                  相关文章

                • <legend id='ST7on'><style id='ST7on'><dir id='ST7on'><q id='ST7on'></q></dir></style></legend>
                • <tfoot id='ST7on'></tfoot>
                    <bdo id='ST7on'></bdo><ul id='ST7on'></ul>

                • <small id='ST7on'></small><noframes id='ST7on'>

                    <i id='ST7on'><tr id='ST7on'><dt id='ST7on'><q id='ST7on'><span id='ST7on'><b id='ST7on'><form id='ST7on'><ins id='ST7on'></ins><ul id='ST7on'></ul><sub id='ST7on'></sub></form><legend id='ST7on'></legend><bdo id='ST7on'><pre id='ST7on'><center id='ST7on'></center></pre></bdo></b><th id='ST7on'></th></span></q></dt></tr></i><div id='ST7on'><tfoot id='ST7on'></tfoot><dl id='ST7on'><fieldset id='ST7on'></fieldset></dl></div>