2012-02-23 15 views
5

我想使基於JDialog的窗口處於非活動狀態,因此所有控件都禁用(灰色)。 setEnabled(false)只是不可能點擊任何控件,甚至關閉窗口。但沒有變成灰色。請幫助。如何使JDialog無效

編輯:這裏是示例代碼。

import javax.swing.JButton; 
import javax.swing.JDialog; 


public class Analyzer extends JDialog{ 

public Analyzer() { 
    JButton but = new JButton("test"); 
    setLayout(null); 
    but.setBounds(10,10,100,100); 

    add(but); 
    setSize(200, 200); 
    setVisible(true); 
    setEnabled(false); 
} 

public static void main(String[] args) { 
    new Analyzer(); 
} 

} 
+0

代碼示例添加。你是否說我需要迭代所有控件並手動禁用它們? – 2012-02-23 14:45:13

+1

這不是我們要找的樣本。我們希望看到您嘗試解決這個問題。您是否考慮過使用遞歸來遍歷JDialog的容器/組件樹,啓用或禁用找到的所有組件? – 2012-02-23 14:47:26

+0

我同意Hialcraft ... setDisabled對每個JDialog包含的組件。 – John 2012-02-23 14:50:23

回答

8

兩個方法,我知道要做到這一點,在那裏你禁用對話框的組件遞歸,並在您禁用整個對話(包括拖動對話框能力)第二:

import java.awt.Component; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class DisableEg extends JPanel { 
    public static final String DISABLE_DIALOG_COMPONENTS = "Disable Dialog Components"; 
    public static final String ENABLE_DIALOG_COMPONENTS = "Enable Dialog Components"; 
    public static final String DISABLE_DIALOG = "Disable Dialog"; 
    public static final String ENABLE_DIALOG = "Enable Dialog"; 
    private static final int LOC_SHIFT = 150; 
    private Analyzer analyzer; 

    public DisableEg(JFrame frame) { 
     analyzer = new Analyzer(frame); 
     analyzer.pack(); 
     analyzer.setLocationRelativeTo(frame); 
     Point location = analyzer.getLocation(); 
     location = new Point(location.x - LOC_SHIFT, location.y - LOC_SHIFT); 
     analyzer.setLocation(location); 
     analyzer.setVisible(true); 

     add(new JButton(new AbstractAction(DISABLE_DIALOG_COMPONENTS) { 

     @Override 
     public void actionPerformed(ActionEvent evt) { 
      AbstractButton btn = (AbstractButton) evt.getSource(); 
      if (btn.getText().equals(DISABLE_DIALOG_COMPONENTS)) { 
       btn.setText(ENABLE_DIALOG_COMPONENTS); 
       analyzer.setComponentEnabled(false); 
      } else { 
       btn.setText(DISABLE_DIALOG_COMPONENTS); 
       analyzer.setComponentEnabled(true); 
      } 
     } 
     })); 
     add(new JButton(new AbstractAction(DISABLE_DIALOG) { 

     @Override 
     public void actionPerformed(ActionEvent evt) { 
      AbstractButton btn = (AbstractButton) evt.getSource(); 
      if (btn.getText().equals(DISABLE_DIALOG)) { 
       btn.setText(ENABLE_DIALOG); 
       analyzer.setEnabled(false); 
      } else { 
       btn.setText(DISABLE_DIALOG); 
       analyzer.setEnabled(true); 
      } 
     } 
     })); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Disable Example"); 
     DisableEg mainPanel = new DisableEg(frame); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

@SuppressWarnings("serial") 
class Analyzer extends JDialog { 

    public Analyzer(JFrame frame) { 
     super(frame, "Analyzer Dialog", false); 
     JButton but = new JButton("test"); 
     setLayout(new FlowLayout()); 

     add(but); 
     setPreferredSize(new Dimension(200, 200)); 
    } 

    public void setComponentEnabled(boolean enabled) { 
     setComponentEnabled(enabled, getContentPane()); 

     // !! if you have menus to disable, you may need instead 
     // setComponentEnabled(enabled, this); // !! 
    } 

    private void setComponentEnabled(boolean enabled, Component component) { 
     component.setEnabled(enabled); 
     if (component instanceof Container) { 
     Component[] components = ((Container) component).getComponents(); 
     if (components != null && components.length > 0) { 
      for (Component heldComponent : components) { 
       setComponentEnabled(enabled, heldComponent); 
      } 
     } 
     } 
    } 

} 
+0

好,很好的工作。 +1 – Jasonw 2012-02-23 15:38:08

4

這樣做的典型方法是使用glassPane,但引入了Java 7的JLayer也應該這樣做。

+0

如果我正確地閱讀OP的問題,這將無法正常工作,因爲他希望組件顯示禁用的外觀。 AFAIK,這樣做的典型方法是使用遞歸。 – 2012-02-23 14:59:06

+1

這種方式比舒適,GlassPane可以是彩色的,這麼簡單,不能通過鼠標訪問,兩者都很好的線程(@Pete偉大的代碼++++++++) – mKorbel 2012-02-23 18:37:32