【问题标题】:Java - JMenuBar Hidden When Using Glass PaneJava - 使用玻璃窗格时隐藏的 JMenuBar
【发布时间】: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


【解决方案1】:

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

this.glassPanel = new JPanel();

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

【讨论】:

  • 好的,所以我按照建议尝试了,但将 button2 直接添加到面板(不使用任何布局),然后 contentpanel 根本不显示。请说明您是如何克服这个问题的。
  • 我刚刚更改了一行(this.glassPanel = new JPanel()),然后进一步向下更改为:this.glassPanel.add(this.button2),而不是先添加它到一个额外的面板。其余的都是一样的。结果显示了两个按钮和菜单栏。
  • 所以我发现出了什么问题...如果/当您使用其他嵌套面板时,您必须将每个面板的不透明度设置为 false。否则默认设置为true。不过感谢您的帮助!
  • 没问题。是的,这也是一个解决方案。你是对的
【解决方案2】:

好吧,伙计们,我不小心找到了问题的解决方案。实际上很简单,如果您在“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 保持不透明。 (您可以在调用上述方法之前或之后设置的嵌套面板)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    相关资源
    最近更新 更多