Java 使JFrame的JMenuBar全屏打开

Java 使JFrame的JMenuBar全屏打开,java,swing,jframe,jmenubar,Java,Swing,Jframe,Jmenubar,我有一个JFrame,我想添加一个菜单栏,然后让它在全屏自动打开。如果我只是制作一个JFrame并使用f.setExtendedState(f.getExtendedState()| JFrame.MAXIMIZED_两者)将其设置为全屏工作正常: import javax.swing.*; public class testframe { private static JFrame f; public static void main(String[] args) {

我有一个JFrame,我想添加一个菜单栏,然后让它在全屏自动打开。如果我只是制作一个JFrame并使用
f.setExtendedState(f.getExtendedState()| JFrame.MAXIMIZED_两者)将其设置为全屏工作正常:

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,两者都有问题。

您需要以正确的顺序调用
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);
    }

}
一旦我将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);

    }
}
为我全屏显示。我在Windows10中使用JDK11

但是,菜单不起作用,因为您有编码问题。您需要将JMenu添加到JMenubar

//menubar.add(j_menu_item);
menubar.add(j_menu);
此外,我一直使用:

 f.setExtendedState( JFrame.MAXIMIZED_BOTH );

谢谢你指出这一点,我将在我的问题中更新它。然而,即使在纠正了这个错误之后,这个框架对我来说也不会出现在全屏上+1.很高兴看到你还在。哈哈,java和swing标签仍然可以通过最有知识的窥视得到最快的答案+1.很高兴看到你还在身边。@ChristianJohann,很惊讶修复了它,我从未注意到早期版本有问题,但很高兴它能工作。你使用的是什么操作系统和Java版本?