【发布时间】:2014-11-23 21:44:28
【问题描述】:
这个问题看起来很简单,但我似乎无法绕过它。
我在框架中使用 GlassPane(也是 ContentPane)。因此,当我将 JMenuBar 添加到框架时,它不会显示出来。如果/当我在其他时间使用 GlassPane 时,一切正常。我做了一些研究,我知道 JMenuBar 显示在 RootPane 上,我相信 GlassPane 以某种方式隐藏了它。
我想知道在使用 glassPane 时是否有任何方法可以获取 JMenuBar?
谢谢
更新: 我正在设置 glassPane.setOpaque(false)
更新:
实际的代码行数要多得多,但这里是与问题相关的代码行。 (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();
}
}
【问题讨论】:
-
请提供任何重现您的问题的代码
-
@SergiyMedvynskyy 我添加了一些代码行和屏幕截图。
-
@AbbasA.Ali "some lines of code" 并不是一个可运行的示例。如果我们无法复制您的问题,我们就不可能解决它。其他一切都是猜测工作。考虑提供一个runnable example 来证明您的问题。这将导致更少的混乱和更好的响应
-
@SergiyMedvynskyy 我已经编写并添加了演示代码 请检查是否可以解决问题。
标签: java swing glasspane contentpane