【问题标题】:Making a JFrame with a JMenuBar open in fullscreen使用 JMenuBar 制作 JFrame 全屏打开
【发布时间】:2021-03-05 10:15:14
【问题描述】:

我有一个想要添加菜单栏的 JFrame,然后让该 JFrame 自动全屏打开。如果我只是制作一个 JFrame 并使用 f.setExtendedState(f.getExtendedState()|JFrame.MAXIMIZED_BOTH ); 将其设置为全屏,那效果很好:

import javax.swing.*;

public class testframe {
    private static JFrame f;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(testframe::createAndShowGUI);
    }

    private static void createAndShowGUI() {
        f = new JFrame("Test");

        f.setExtendedState( f.getExtendedState()|JFrame.MAXIMIZED_BOTH );
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
        f.pack();
        f.setVisible(true);

    }
}

但是,一旦我将 JMenuBar 添加到该 JFrame 中,它将不再全屏打开:

import javax.swing.*;

public class testframe {
    private static JFrame f;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(testframe::createAndShowGUI);
    }

    private static void createAndShowGUI() {
        f = new JFrame("Test");

    JMenuBar menubar = new JMenuBar();
    JMenu j_menu = new JMenu("Test");
    JMenuItem j_menu_item = new JMenuItem("Test_item");
    j_menu.add(j_menu_item);
    menubar.add(j_menu);
    f.setJMenuBar(menubar);

    f.setExtendedState( f.getExtendedState()|JFrame.MAXIMIZED_BOTH );
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
    f.pack();
    f.setVisible(true);

    }
}

这可能是什么原因?

更新:

切换到 JDK11 解决了这个问题。我之前是15,也试过14,都出问题了。

【问题讨论】:

  • 您使用的是什么操作系统和Java版本?

标签: java swing jframe jmenubar


【解决方案1】:

一旦我将 JMenuBar 添加到该 JFrame,它将不再全屏打开:

对我来说全屏显示。我在 Windows 10 中使用 JDK11。

但是,由于您遇到了编码问题,因此该菜单不起作用。您需要将 JMenu 添加到 JMenubar。

//menubar.add(j_menu_item);
menubar.add(j_menu);

另外,我一直只是用:

 f.setExtendedState( JFrame.MAXIMIZED_BOTH );

【讨论】:

  • 哈哈,java 和 swing 标签仍然可以得到最博学的窥视者的最快答案! +1。很高兴看到你还在。
  • @ChristianJohann,令人惊讶的是修复了它,我从未注意到早期版本有问题,但很高兴它有效。
【解决方案2】:

您需要以正确的顺序调用JFrame 方法。

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class TestFrame {
    private static JFrame f;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TestFrame::createAndShowGUI);
    }

    private static void createAndShowGUI() {
        f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar menubar = new JMenuBar();
        JMenu j_menu = new JMenu("Test");
        JMenuItem j_menu_item = new JMenuItem("Test_item");
        j_menu.add(j_menu_item);
        menubar.add(j_menu);
        f.setJMenuBar(menubar);

        f.pack();
        f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        f.setVisible(true);
    }

}

【讨论】:

  • 感谢您指出这一点,我会在我的问题中更新它。但是,即使修复了该错误,我也不会全屏显示该框架。
  • 哈哈,java 和 swing 标签仍然可以得到最博学的窥视者的最快答案! +1。很高兴看到你还在。
猜你喜欢
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多