简体   繁体   中英

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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