JMenuBar没有露面

时间:2016-06-09 16:48:36

标签: java swing jmenubar

你们有没有人知道为什么我的JMenuBar menuBar没有出现? 我使用的是JFrameJPanel

我的课程延伸JPanel并继承了paint方法(已包含super.paint(g))。

我想在JLabels上显示一些JTextFieldsJMenuBar(我知道这不是它的目的)

这是我的代码:

public void createMenuBar(){

    menuBar = new JMenuBar();
    menuBar.setBounds(0,0,1463,29);
    menuBar.setLayout(null);
    this.add(menuBar);

    ipLbl = new JLabel("IP-Adresse:");
    ipLbl.setBounds(5,2,150,25);
    ipLbl.setLabelFor(ip);
    menuBar.add(ipLbl);

    ip = new JTextField();
    ip.setBounds(150,2,100,25);
    menuBar.add(ip);
}

我在创建JFrame后调用此方法,但在执行setVisible(true)之前。

查找

public IceHockey(){

    try {
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e){}

    fenster = new JFrame("IceHockey");
    fenster.setSize(1479, 941);
    fenster.setLayout(null);
    fenster.addKeyListener(this);
    fenster.addMouseListener(this);
    fenster.setResizable(false);
    fenster.setLocationRelativeTo(null);

    fenster.setContentPane(this);

    this.addKeyListener(this);
    this.addMouseListener(this);

    createMenuBar();

    fenster.setVisible(true);
    fenster.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

2 个答案:

答案 0 :(得分:2)

您可以使用fenster.setJMenuBar(menuBar);

将菜单栏添加到框架中

如果您想将菜单栏添加到JPanel,可以使用以下方法:

add JMenuBar to a JPanel?

答案 1 :(得分:1)

创建MenuBar后,我认为您仍需要将其添加到JFrame中。更改createMenuBar()以返回JMenuBar而不是void,然后您可以编写以下内容:

fenster.setJMenuBar(createMenuBar());

我不清楚 createMenuBar()的代码中 this 是什么,但我猜它不是 fenster 但是允许添加的东西。