简体   繁体   English

Java如何在不破坏我的布局的情况下向JFrame添加menuBar

[英]Java How to add menuBar to JFrame without destroying my layout

I need some help with adding a menuBar to my JFrame. 在向我的JFrame添加menuBar时,我需要一些帮助。 I have my Gui class: 我的桂课有:

public class Gui extends JFrame implements KeyListener {
    private PanelMiniMap map;
private CardLayout cardLayout = new CardLayout();

private JPanel cards;

public Gui() {
    this.setSize(1000, 800);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);// center
    this.setResizable(false);

    // bauen();
    this.cards = new JPanel(cardLayout);



    this.addListener();
    this.add(cards);

    this.setVisible(true);

}

public void addListener() {
    this.addKeyListener(this);
}

public void addPanelZuCards(PanelSpielfeld spielfeld, PanelCharakterErschaffung charerschaffung, PanelMiniMap map) {

    this.map = map;

    this.cards.add(charerschaffung, "charerschaffung");
    this.cards.add(spielfeld, "spielfeld");
}

public Dimension getDimension() {
    return new Dimension(this.getWidth(), this.getHeight());
}

/**
 * Neue Charaktererschaffung ausgeben
 * 
 * @param charerschaffung
 *            Das Panel, auf dem die Oberflaeche zur Charaktererschaffung
 *            erstellt wird
 */
public void zeigeCharckterPanel() {
    this.cardLayout.show(cards, "charerschaffung");
}

public void zeigeSpielPanel() {
    this.cardLayout.show(cards, "spielfeld");
    this.requestFocus();

}

@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        map.move(-1, 0);
        map.repaint();
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        map.move(1, 0);
        map.repaint();
    }
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        map.move(0, -1);
        map.repaint();
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        map.move(0, 1);
        map.repaint();

    }
}

} }

where I add my 2 JPanels(PanelSpielfeld,PanelCharakterErschaffung)) to my Gui, and it locks like this: 我在我的Gui中添加了2个JPanels(PanelSpielfeld,PanelCharakterErschaffung)),它的锁定方式如下: 在此处输入图片说明

and the other card, wich is the same principle, I show the other card after pressing the "starten!" 和另一张卡一样,这是相同的原理,我按下“开始”后显示另一张卡。 button: 按钮: 在此处输入图片说明

So, if I add a menuBar to the Jframe: 因此,如果我将menuBar添加到Jframe:

public Gui() {
        this.setSize(1000, 800);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);// center
        this.setResizable(false);

        // bauen();
        this.cards = new JPanel(cardLayout);

        JMenuBar menuBar;
        JMenu menu;
        JMenuItem menuItem;

        menuBar = new JMenuBar();

        menu = new JMenu("A Menu");
        menuItem = new JMenuItem("testitem");

        menu.add(menuItem);
        menuItem = new JMenuItem("3214dx");
        menu.add(menuItem);
        menuItem = new JMenuItem("dwq213m");
        menu.add(menuItem);
        menuBar.add(menu);
        this.add(menuBar);

        this.addListener();     
        this.add(cards);

        this.setVisible(true);
        this.addWindowFocusListener(this);
    }

Nothing is happening, and I don't really understand why. 什么也没发生,我真的不明白为什么。 What am I doing wrong or what should I do, to have the same MenuBar on both "cards"? 在两个“卡”上都具有相同的MenuBar,我在做什么或该怎么办?

Edit: I changed 编辑:我改变了

this.add(menuBar)

to: 至:

this.setJMenuBar(menuBar);

and now it locks like this: 现在它像这样锁定: 在此处输入图片说明

I'm still missing something. 我仍然想念一些东西。 Do I have to set a specific LayoutManager for the JFrame? 我是否需要为JFrame设置特定的LayoutManager?

Okay, I'm sorry for wasting your time (who ever reads this). 好的,很抱歉浪费您的时间(谁曾读过这篇文章)。 My 2 JPanles are set to the frames prefered size, so the problem was, that the menuBar tock the space that the 2 panels needed and that was destroying my gui. 我的2个JPanles设置为首选的框架大小,因此问题是menuBar占用了2个面板所需的空间,这破坏了我的GUI。

If I set the size of the 2 JPanels to frame.height -50 it work's: 如果我将2个JPanel的大小设置为frame.height -50,它的工作原理是: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM