簡體   English   中英

使Java Swing模態對話框的行為類似於Mac OSX對話框

[英]Make Java Swing Modal Dialog Behave Like Mac OSX Dialogs

我正在編寫一個小型應用程序,它要求ProgressBar出現在框架的TitleBar的中央,這在Mac OSX應用程序中很常見。 我有兩個問題:

1 我已經完成了定位,但是我不得不對父框架的TitleBar高度進行硬編碼。 有沒有一種“軟”的方法來獲取TitleBar的高度?

在對話框的構造函數中:

 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 即使對話框是模式對話框,我仍然可以單擊父框架並將其移動。 如何使對話框“貼”到父框架? 就是說,當父框架移動時,對話框也隨之移動,就像附加的一樣。

任何幫助/指針將不勝感激。

這是代碼:



    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;

即使對話框是模式對話框,我仍然可以單擊父框架並將其移動。

然后您在做錯事,因為這不應該發生。

發布證明問題的SSCCE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM