2014-11-23 90 views
0

該問題看起來很簡單,但我似乎無法解決它。Java - 使用玻璃面板隱藏JMenuBar

我在我的框架(也是ContentPane)中使用GlassPane。所以當我將JMenuBar添加到框架中時,它不會顯示出來。如果/當我在其他時間使用GlassPane時,一切都可以正常工作。我做了一些研究,我的理解是JMenuBar顯示在RootPane上,我相信GlassPane以某種方式隱藏它。

我需要知道在使用glassPane時是否有任何方法獲得JMenuBar?

感謝

更新: 我設置glassPane.setOpaque(假)

UPDATE:

的代碼中實際的線條更加但是這裏是相對於問題的那些。 (mainPanel中和notificationPanel從JPanel的擴展自構造類)和

public class Demo extends JFrame { 

///////////////////////////////////////////////////////////////////////// 
// JMenuBar 
private final JMenuBar mainMenuBar; 
    private final JMenu fileMenu; 
     private final JMenuItem exitFileMenu; 
///////////////////////////////////////////////////////////////////////// 
// CONTENT PANE & COMPONENTS 
private final JPanel contentPanel; 
    private final JPanel buttonPanel; 
     private final JButton button1; 

///////////////////////////////////////////////////////////////////////// 
// GLASSPANE AND COMPONENTS 
private final JPanel glassPanel; 
    private final JPanel buttonPanel2; 
    private final JButton button2; 

public Demo() { 
    super(); 

    this.mainMenuBar = new JMenuBar(); 
     this.fileMenu = new JMenu("File"); 
      this.exitFileMenu = new JMenuItem("EXIT"); 

    this.contentPanel = new JPanel(new BorderLayout()); 
     this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
      this.button1 = new JButton("Button 1"); 

    this.glassPanel = new JPanel(new BorderLayout()); 
     this.buttonPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
      this.button2 = new JButton("Button 2"); 
} 

public void initGUI() { 
     this.fileMenu.add(this.exitFileMenu); 
    this.mainMenuBar.add(this.fileMenu); 

     this.buttonPanel.add(this.button1); 
    this.contentPanel.add(this.buttonPanel, BorderLayout.NORTH); 


     this.buttonPanel2.add(this.button2); 
    this.glassPanel.add(this.buttonPanel2, BorderLayout.NORTH); 

    super.setContentPane(this.contentPanel); 
    super.setGlassPane(this.glassPanel); 

    this.glassPanel.setOpaque(false); 
    this.glassPanel.setVisible(true); 

    super.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    super.setJMenuBar(mainMenuBar); 
    super.setVisible(true); 
} 

public static void main(String[] args) { 
    Demo obj = new Demo(); 
    obj.initGUI(); 
} 

}

+0

請提供任何代碼來重現您的問題 – 2014-11-23 22:31:04

+0

@SergiyMedvynskyy我添加了一些代碼行和屏幕快照。 – Abbas 2014-11-23 22:54:50

+0

@ AbbasA.Ali *「一些代碼行」*不能生成可運行的示例。如果我們無法複製您的問題,我們不太可能解決它。一切都是猜測工作。考慮提供一個[可運行的示例](https://stackoverflow.com/help/mcve),它可以證明你的問題。這會減少混淆和更好的反應 – MadProgrammer 2014-11-23 23:03:29

回答

0

好男人我不小心找到了解決問題的辦法。事實上,這很簡單,如果您在'glassPane'內使用嵌套面板,那麼只需將每個嵌套面板的不透明度設置爲false即可。如果您沒有嵌套面板將顯示其每個邊界的背景並覆蓋任何底層。

以上是Demo的工作代碼。

public class Demo extends JFrame { 

///////////////////////////////////////////////////////////////////////// 
// JMenuBar 
private final JMenuBar mainMenuBar; 
    private final JMenu fileMenu; 
     private final JMenuItem exitFileMenu; 
///////////////////////////////////////////////////////////////////////// 
// CONTENT PANE & COMPONENTS 
private final JPanel contentPanel; 
    private final JPanel buttonPanel; 
     private final JButton button1; 

///////////////////////////////////////////////////////////////////////// 
// GLASSPANE AND COMPONENTS 
private final JPanel glassPanel; 
    private final JPanel buttonPanel2; 
    private final JButton button2; 

public Demo() { 
    super(); 

    this.mainMenuBar = new JMenuBar(); 
     this.fileMenu = new JMenu("File"); 
      this.exitFileMenu = new JMenuItem("EXIT"); 

    this.contentPanel = new JPanel(new BorderLayout()); 
     this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
      this.button1 = new JButton("Button 1"); 

    this.glassPanel = new JPanel(new BorderLayout()); 
     this.buttonPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
      this.button2 = new JButton("Button 2"); 
} 

public void initGUI() { 
     this.fileMenu.add(this.exitFileMenu); 
    this.mainMenuBar.add(this.fileMenu); 

     this.buttonPanel.add(this.button1); 
    this.contentPanel.add(this.buttonPanel, BorderLayout.NORTH); 


     this.buttonPanel2.add(this.button2); 
     this.buttonPanel2.setOpaque(false); 
    this.glassPanel.add(this.buttonPanel2, BorderLayout.NORTH); 

    super.setContentPane(this.contentPanel); 
    super.setGlassPane(this.glassPanel); 

    this.glassPanel.setOpaque(false); 
    this.glassPanel.setVisible(true); 

    super.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    super.setJMenuBar(mainMenuBar); 
    super.setVisible(true); 
} 

public static void main(String[] args) { 
    Demo obj = new Demo(); 
    obj.initGUI(); 
} 

}

現在還記得你總是稱之爲「setGlassPane(JPanel)」否則玻璃面板保持不透明後設置的glassPane的不透明度。 (您可以在調用所述方法之前或之後設置嵌套面板)

0

您正在使用BorderLayoutglassPanel選項BorderLayout.NORTH。它佔用了北部的整個空間,並且與整個菜單重疊。因此你再也看不到任何東西了。更改例如您的面板創建到:

this.glassPanel = new JPanel(); 

然後您的面板將被調整大小,只適合您的按鈕,你會看到你的菜單後面。你可以玩一些佈局,看看哪一個適合。但請記住,玻璃窗格始終處於最佳狀態。請注意:當您直接將按鈕添加到'glassPanel'(不使用'buttonPanel2')時,您可以刪除小「邊框」。否則,您可以調整它以完全適合您的按鈕。兩者都是可能的,但如果你只想要一個組件(比如你的按鈕),那麼我會直接添加它。

+0

好了,所以我按照建議嘗試了,但直接將button2添加到面板(不使用任何佈局),但隨後contentpanel完全不顯示。請說明你是如何克服這個問題的。 – Abbas 2014-11-24 13:19:05

+0

我剛剛更改了一行(this.glassPanel = new JPanel()),然後再將行更改爲:this.glassPanel.add(this.button2),而不是先將其添加到其他面板。其餘的都是一樣的。結果顯示了兩個按鈕和菜單欄。 – BluesSolo 2014-11-24 15:13:25

+0

所以我發現有什麼問題...... 如果/當你使用額外的嵌套面板,你必須設置每個面板的不透明度爲false。否則,默認情況下它被設置爲true。 雖然感謝您的幫助! – Abbas 2014-11-24 16:30:27