0

这个问题看起来很简单,但我似乎无法解决它。

我在我的框架中使用 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();
}

}

4

2 回答 2

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 的不透明度,否则 GlassPane 保持不透明。(您可以在调用上述方法之前或之后设置的嵌套面板)

于 2014-11-24T16:46:01.940 回答
0

您正在使用BorderLayoutBorderLayout.NORTH您的glassPanel. 它占据了北方的整个空间,并与您的整个菜单重叠。因此,您再也看不到任何东西了。例如,将您的面板创建更改为:

this.glassPanel = new JPanel();

然后您的面板将调整大小以仅适合您的按钮,您将看到后面的菜单。您可以尝试一些布局,看看哪一个适合。但请记住,玻璃板始终位于一切之上。只是一点点说明:当您将按钮直接添加到'glassPanel'(不使用'buttonPanel2')时,您可以删除小“边框”。否则,您可以调整它的大小以完全适合您的按钮。两者都是可能的,但是如果您只想拥有一个组件(例如您的按钮),那么我会直接添加它。

于 2014-11-24T12:55:34.357 回答