0

I need some help with adding a menuBar to my JFrame. 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: enter image description here

and the other card, which is the same principle, I show the other card after pressing the "starten!" button: enter image description here

So, if I add a menuBar to the 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"?

Edit: I changed

this.add(menuBar)

to:

this.setJMenuBar(menuBar);

and now it locks like this: enter image description here

I'm still missing something. Do I have to set a specific LayoutManager for the JFrame?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user3793935
  • 411
  • 5
  • 22

1 Answers1

-1

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.

If I set the size of the 2 JPanels to frame.height -50 it work's: enter image description here

user3793935
  • 411
  • 5
  • 22
  • 2
    *"If I set the size of the 2 JPanels to frame.height -50 it work's: "* For that PLAF on that OS, maybe. A better solution is not to set the preferred size of things(1) and pack the top level container to size after all components are added and menus are set. 1) instead use layout padding and borders (or combos of borders) for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jan 21 '18 at 11:56
  • Okay, that's pretty interesting. I was locking for something like that for quite some time. Thank you, I will try it out! – user3793935 Jan 21 '18 at 12:04