saviok
saviok

Reputation: 497

Adding multiple JPanel to a JFrame

Alright so right now I have a JFrame which contains a JLayeredPane. So this runs a game GUI right now. I want to add buttons to side of this frame so that I can take in the inputs from the user through the buttons. Any ideas?

class mapGUI extends JFrame{
        layeredPane = new JLayeredPane();
    mapSize = new Dimension(mapColumn * 16 , mapRow * 16);

    layeredPane.setPreferredSize(mapSize);
    //Adding the layeredPane to the frame.
    getContentPane().add(layeredPane);

    //Adds the appropriate labels to the panels and adds them to the layeredPane.
    addPlayerPos();
    addDungeonItems();
    addDungeonFloor();

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
}

This is the part of the code where I create the JFrame and add the JLayeredPane to it.

Upvotes: 0

Views: 521

Answers (3)

mKorbel
mKorbel

Reputation: 109823

1) don't use JLayeredPane, because has limitation up to 6. layers, use JLayer (Java7) based on JXLayer (Java6)

2) using JLayeredPane is about setSize / setBounds, that not confortable for resiziable Container

3) use CardLayout instead of JLayeredPane,

4) or use JTabbedPane

Upvotes: 4

Nitin Chhajer
Nitin Chhajer

Reputation: 2339

If you want to use the Buttons through out the game you can use a BorderLayout and put the buttons on East/West of the layout, and add a LayeredPane to the Centre. If the buttons needs to overlap with the game screen you can use layeredPaneObj.add(buttonPanel,new Integer(0)) this will keep the panel on the top.

Upvotes: 1

moeTi
moeTi

Reputation: 3904

You should learn basic stuff about layout managers first. As mentioned before, have a special look at the BorderLayout for your requirements

Upvotes: 1

Related Questions