2013-01-15 36 views
3

最初我在桌面Swing應用程序中使用了以下代碼。 MyDialog是內部類,frame是JFrame。JDialog modal = true與ModalityType.APPLICATION_MODAL

private class MyDialog extends JDialog { 
    public MyDialog (String title) { 
     super(frame, title, true); 
     ... 
    } 

然後我修改了這段代碼以支持桌面和applet。所以它變成這樣。 owber也是JFrameJApplet

private class MyDialog extends JDialog { 
    public MyDialog (String title) { 
     super(SwingUtilities.windowForComponent(owner), title, ModalityType.APPLICATION_MODAL); 
     ... 
    } 

的問題是,我運行代碼桌面,但模式的行爲是不同的。應用程序啓動後,我在任務欄中單擊Eclipse,以便將應用程序隱藏在Eclipse後面。現在,在任務欄我點擊應用程序圖標:

  1. JFrameJDialog被立即顯示在Eclipse之上
  2. 在任務欄上有兩個選項JFrameJDialog,但都只有JDialog的出現在Eclipse之上和JFrame沒有。

而且JDialod沒有下面的構造這將是最適合我:

JDialog(Window owner, String title, boolean modal) 

我從ModalityType嘗試不同的領域,但他們沒有給出相同的期望的結果片斷1#。我的方法有什麼問題,爲什麼行爲不同?

UPD爲mKorbel:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class WindowForComp { 
    private JFrame mainwindow; 
    private CustomDialog customDialog; 

    private void displayGUI() { 
     mainwindow = new JFrame("MyFrame"); 
     customDialog = new CustomDialog(mainwindow, "Modal Dialog", true); 
     mainwindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     JButton mainButton = new JButton("Just a button"); 
     mainButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       EventQueue.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
         customDialog.setVisible(true); 
        } 
       }); 
      } 
     }); 

     contentPane.add(mainButton); 
     mainwindow.setContentPane(contentPane); 
     mainwindow.pack(); 
     mainwindow.setLocationByPlatform(true); 
     mainwindow.setVisible(true); 
    } 

    public static void main(String... args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new WindowForComp().displayGUI(); 
      } 
     }); 
    } 
} 


class CustomDialog extends JDialog { 
    public CustomDialog(JFrame owner, String title, boolean modal) { 
     super(SwingUtilities.windowForComponent(owner), title, ModalityType.APPLICATION_MODAL); 
     System.out.println(SwingUtilities.windowForComponent(owner)); 

     JPanel contentPane = new JPanel(); 
     JLabel dialogLabel = new JLabel("I am a Label on JDialog.", JLabel.CENTER); 
     contentPane.add(dialogLabel); 
     setContentPane(contentPane); 
     pack(); 
    } 
} 
+0

[這裏](http://docs.oracle.com/javase/tutorial/uiswing/misc /modality.html)或[here](http://www.oracle.com/technetwork/articles/javase/modality-137604.html),必須小心使用'setModal'&'JOptionPane',因爲'JOptionPane' (例如從'SwingWorker'例外的初始化==)如果存在'Modal JDialog',然後'JOptionPane'在'modal JDialog'後面,只有'taskmanager'可以終止這個應用程序 – mKorbel

+0

@mKorbel,s eems like'SwingUtilities.windowForComponent(frame);'returns' null' –

+0

hmmm,可以請你發佈SSCCE,重要的JDK版本..... – mKorbel

回答

1

看來,SwingUtilities.windowForComponent(JFrame)被返回null,所以該對話框沒有父。

SwingUtilities.windowForComponent(JFrame) returns null

現在我用這個方法,而不是和它完美的作品(模式):

public static Window windowForComponent (Component c) { 
    if (c instanceof Window) return (Window)c; 
    return SwingUtilities.windowForComponent(c); 
} 
+0

約定的JComponent必須是可顯式的,然後才能返回父項 – mKorbel