Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding multiple jPanels to jFrame

I want to add two jPanels to a JFrame side by side. the two boxes are jpanels and the outer box is a jframe enter image description here

I have these lines of code. I have one class called seatinPanel that extends JPanel and inside this class I have a constructor and one method called utilityButtons that return a JPanel object. I want the utilityButtons JPanel to be on the right side. the code I have here only displays the utillityButtons JPanel when it runs.

public guiCreator()
    {
        setTitle("Passenger Seats");
        //setSize(500, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = getContentPane();

        seatingPanel seatingPanel1 = new seatingPanel();//need to declare it here separately so we can add the utilityButtons
        contentPane.add(seatingPanel1); //adding the seats
        contentPane.add(seatingPanel1.utilityButtons());//adding the utility buttons

        pack();//Causes this Window to be sized to fit the preferred size and layouts of its subcomponents
        setVisible(true);  
    }
like image 420
dave Avatar asked Jun 12 '11 23:06

dave


People also ask

How do I create multiple Jframes?

java write the actual logic code, in this class write the code which creates JFrame, add JButton for open New Frame. In this class write the code which creates JFrame, add JLabel into the New Frame. Simillarly, you can create multiple frames and navigate from one frame to another.

Can a JFrame contain another JFrame?

You can't put one JFrame inside another. Change your design so that the Game window content is on a JPanel and the Menu is on another JPanel .


2 Answers

The most flexible LayoutManager I would recommend is BoxLayout.

You can do the following :

JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();

//panel1.set[Preferred/Maximum/Minimum]Size()

container.add(panel1);
container.add(panel2);

then add container to object to your frame component.

like image 199
Heisenbug Avatar answered Sep 19 '22 18:09

Heisenbug


You need to read up on and learn about the layout managers that Swing has to offer. In your situation it will help to know that a JFrame's contentPane uses BorderLayout by default and you can add your larger center JPanel BorderLayout.CENTER and the other JPanel BorderLayout.EAST. More can be found here: Laying out Components in a Container

Edit 1
Andrew Thompson has already shown you a bit on layout managers in his code in your previous post here: why are my buttons not showing up?. Again, please read the tutorial to understand them better.

like image 27
Hovercraft Full Of Eels Avatar answered Sep 17 '22 18:09

Hovercraft Full Of Eels