2015-12-09 48 views
0

在我的項目代碼中,我們使用自定義對話框。我正面臨着一些奇怪的問題。 我有一個孩子對話和父母對話。 理想情況下,當我們調用parent.setVisible(false)時,兒童對話將變爲隱形狀態。父對話狀態更改爲不可見,但子對話仍然可見

但我看到一些奇怪的行爲。 當我製作parent.setVisible(false)時,仍然可以看到我的子對話框,但是當我嘗試獲取child.isVisible()時:它讓我錯誤。 此外,當我嘗試調用child.setVisible(false)時,它再次對父對話框的可見性沒有影響。

注意:由於太複雜,長度和其他外部API問題,我無法顯示任何代碼示例。此外,我試圖使用外部程序複製這個,但它按預期工作,沒有看到問題。

我只想知道有沒有人知道我們製作parent.setVisible(false)時兒童對話框控件鬆動的任何場景?

回答

1

我只想知道有沒有人知道任何一種情況,當我們使parent.setVisible(false)時,對話框控件的對話框控件變得鬆動?

  • 父被稱爲調用setVisible(假)也

  • 確保您重用數量減少孩子的,要設置DefaultCloseOparation隱藏或DISPOSE_ON_CLOSE(由defasult最後一個容器關閉光,但如果是不影響存在容器之間的模態)

例如

import java.awt.BorderLayout; 
import java.awt.Dialog.ModalityType; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

/** 
* 
* @author user 
*/ 
public class ModalityAndJDialog { 

    private JFrame frame = new JFrame(); 
    private JDialog dialog1; 
    private JDialog dialog2; 
    private JButton button = new JButton("Start Swing Timer"); 
    private JButton button1 = new JButton(); 
    private JButton button01 = new JButton(); 
    private JButton button02 = new JButton(); 
    private Timer timer; 

    public ModalityAndJDialog() { 
     button.setAction(updateCol()); 
     frame.setTitle("JFrame"); 
     frame.add(button, BorderLayout.SOUTH); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setSize(400, 300); 
     frame.setLocation(150, 150); 
     frame.setVisible(true); 
     timer = new javax.swing.Timer(500, updateCol()); 
     timer.setRepeats(false); 
     timer.start(); 
    } 

    private Action updateCol() { 
     return new AbstractAction("Show JDialog") { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (dialog1 == null) { 
        dialog1 = new JDialog(dialog1, ModalityType.APPLICATION_MODAL); 
        dialog1.setTitle("1st. JDialog"); 
        dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
        button1.setAction(updateCol1()); 
        button01.setAction(updateCol01()); 
        dialog1.add(button1, BorderLayout.SOUTH); 
        dialog1.add(button01, BorderLayout.NORTH); 
        dialog1.pack(); 
        dialog1.setSize(400, 300); 
        dialog1.setLocation(250, 250); 
        dialog1.setVisible(true); 
       } else { 
        EventQueue.invokeLater(() -> { 
         dialog1.setVisible(true); 
        }); 
       } 
      } 
     }; 
    } 

    private Action updateCol01() { 
     return new AbstractAction("Hide JDialog") { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (dialog1 != null) { 
        dialog1.setVisible(false); 
       } 
      } 
     }; 
    } 

    private Action updateCol1() { 
     return new AbstractAction("Show Child JDialog") { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (dialog2 == null) { 
        dialog1.setTitle("2nd. JDialog"); 
        dialog2 = new JDialog(frame, ModalityType.APPLICATION_MODAL); 
        dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
        button02.setAction(updateCol02()); 
        dialog2.add(button02, BorderLayout.SOUTH); 
        dialog2.pack(); 
        dialog2.setSize(400, 300); 
        dialog2.setLocation(350, 350); 
        dialog2.setVisible(true); 
       } else { 
        EventQueue.invokeLater(() -> { 
         dialog2.setVisible(true); 
         if (!frame.isVisible()) { 
          frame.setVisible(true); 
         } 
        }); 
       } 
      } 
     }; 
    } 

    private Action updateCol02() { 
     return new AbstractAction("Hide JDialog") { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (frame != null) { 
        frame.setVisible(false); 
       } 
       /*if (dialog1 != null) { 
       dialog1.setVisible(false); 
       }*/ 
      } 
     }; 
    } 

    public static void main(String[] args) { 
     try { 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
      /*UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());*/ 
     } catch (UnsupportedLookAndFeelException | ClassNotFoundException | IllegalAccessException | InstantiationException ex) { 
      System.out.println("[L&F][Exception] " + ex.getMessage()); 
     } 
     EventQueue.invokeLater(() -> { 
      new ModalityAndJDialog(); 
     }); 
    } 
} 
相關問題