Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JMenuBar不会出现在JFrame中_Java_Swing_Frame_Menubar - Fatal编程技术网

Java JMenuBar不会出现在JFrame中

Java JMenuBar不会出现在JFrame中,java,swing,frame,menubar,Java,Swing,Frame,Menubar,已解决忘记实例化我的菜单栏 我扩展了类JMenuBar,还扩展了类JFrame。但是,当IsetJMenuBar(菜单栏)时,测试时菜单栏不会出现在帧中 这是我的代码: 类框架: import javax.swing.JFrame; public class Frame extends JFrame{ public static MenuBar menubar; public Frame(){ setTitle("LoL Teamcomps"); setLocationRe

已解决忘记实例化我的菜单栏

我扩展了类
JMenuBar
,还扩展了类
JFrame
。但是,当I
setJMenuBar(菜单栏)
时,测试时菜单栏不会出现在
帧中

这是我的代码:
类框架:

import javax.swing.JFrame;

public class Frame extends JFrame{

public static MenuBar menubar;

public Frame(){
    setTitle("LoL Teamcomps");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setJMenuBar(menubar);

}   

}
类菜单栏:

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import com.lolteamcomps.code.components.MenuItem;

public class MenuBar extends JMenuBar{

public MenuBar(){
    JMenuItem players = new JMenuItem("Players");
    JMenuItem champions = new JMenuItem("Champions");
    JMenuItem newPlayer = new JMenuItem("Add new");
    JMenuItem newChampion = new JMenuItem("Add new");

    players.add(newPlayer);
    champions.add(newChampion);

    add(players);
    add(champions);


}

}
在我的main中调用的函数中:

private static Frame frame = new Frame(); 

public static void CreateWindow(){
    frame.pack();
    frame.setVisible(true);



}
仅显示一个空的
,其中没有任何内容。我做错了什么


编辑:我自己的想法是
setJMenuBar(menubar)
不起作用,因为
menubar
不是
JMenuBar
,而是
menubar扩展了JMenuBar
,所以实际上它是
JMenuBar
,对吗?另外,我在这行没有得到错误,所以我认为情况并非如此。只是一个想法。

您需要实例化
MenuBar

public  MenuBar menubar=new MenuBar();

public Frame(){
 ....
 setJMenuBar(menubar);
} 

您需要实例化
MenuBar

public  MenuBar menubar=new MenuBar();

public Frame(){
 ....
 setJMenuBar(menubar);
} 

您需要实例化
MenuBar

public  MenuBar menubar=new MenuBar();

public Frame(){
 ....
 setJMenuBar(menubar);
} 

您需要实例化
MenuBar

public  MenuBar menubar=new MenuBar();

public Frame(){
 ....
 setJMenuBar(menubar);
} 

举个简单的例子,人们经常使用继承(即:
扩展
)来减少人们必须阅读的代码量;然而,除非绝对有意义,否则使用继承不是一个好的实践。在爪哇中,只能使用继承一次,因此,您应该仔细考虑是否适当扩展JFrm,或者扩展J菜单。您真的在扩展JFrame或JMenu的功能吗?如果答案是否定的,那么应该使用实例变量,并在构造函数中实例化它

例如:

public class MyApplication {
    private JFrame frame;
    private JMenuBar menuBar;
    private JMenu menu;

public MyApplication() {
    frame = new JFrame();

    ... // setup JFrame with layout and other elements

    menuBar = new JMenuBar();
    menu = new JMenu("File");

    menuBar.add(menu);

    ... // other setup

    frame.setVisible(true);
}

举个简单的例子,人们经常使用继承(即:
扩展
)来减少人们必须阅读的代码量;然而,除非绝对有意义,否则使用继承不是一个好的实践。在爪哇中,只能使用继承一次,因此,您应该仔细考虑是否适当扩展JFrm,或者扩展J菜单。您真的在扩展JFrame或JMenu的功能吗?如果答案是否定的,那么应该使用实例变量,并在构造函数中实例化它

例如:

public class MyApplication {
    private JFrame frame;
    private JMenuBar menuBar;
    private JMenu menu;

public MyApplication() {
    frame = new JFrame();

    ... // setup JFrame with layout and other elements

    menuBar = new JMenuBar();
    menu = new JMenu("File");

    menuBar.add(menu);

    ... // other setup

    frame.setVisible(true);
}

举个简单的例子,人们经常使用继承(即:
扩展
)来减少人们必须阅读的代码量;然而,除非绝对有意义,否则使用继承不是一个好的实践。在爪哇中,只能使用继承一次,因此,您应该仔细考虑是否适当扩展JFrm,或者扩展J菜单。您真的在扩展JFrame或JMenu的功能吗?如果答案是否定的,那么应该使用实例变量,并在构造函数中实例化它

例如:

public class MyApplication {
    private JFrame frame;
    private JMenuBar menuBar;
    private JMenu menu;

public MyApplication() {
    frame = new JFrame();

    ... // setup JFrame with layout and other elements

    menuBar = new JMenuBar();
    menu = new JMenu("File");

    menuBar.add(menu);

    ... // other setup

    frame.setVisible(true);
}

举个简单的例子,人们经常使用继承(即:
扩展
)来减少人们必须阅读的代码量;然而,除非绝对有意义,否则使用继承不是一个好的实践。在爪哇中,只能使用继承一次,因此,您应该仔细考虑是否适当扩展JFrm,或者扩展J菜单。您真的在扩展JFrame或JMenu的功能吗?如果答案是否定的,那么应该使用实例变量,并在构造函数中实例化它

例如:

public class MyApplication {
    private JFrame frame;
    private JMenuBar menuBar;
    private JMenu menu;

public MyApplication() {
    frame = new JFrame();

    ... // setup JFrame with layout and other elements

    menuBar = new JMenuBar();
    menu = new JMenu("File");

    menuBar.add(menu);

    ... // other setup

    frame.setVisible(true);
}

我现在明白了,你似乎没有实例化你的菜单。关于那件事,很蠢。。。谢谢你@或者你似乎没有实例化你的菜单,我现在明白了。关于那件事,很蠢。。。谢谢你@或者你似乎没有实例化你的菜单,我现在明白了。关于那件事,很蠢。。。谢谢你@或者你似乎没有实例化你的菜单,我现在明白了。关于那件事,很蠢。。。谢谢你@严酷的