2014-11-23 83 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