Graham Carling
Graham Carling

Reputation: 563

How to add many components to a JFrame in a format

I have a JPanel, 4 ComboBoxes, and a button. I want to have a 700 x 500 JFrame, with the panel taking up the left 500 x 500. The right side I want, vertically, 2 combo boxes, another 2 combo boxes, and then the button. Hopefully this makes sense: I just want to have them all visible and I want the boxes paired in groups of 2. Example code of what I've tried is here:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;

public class Test extends JFrame{

public Test () {
    super();
    //setLayout(new FlowLayout());
    JPanel canvas = new JPanel();
    canvas.setBackground(Color.red);
    canvas.setSize(500, 500);
    JComboBox field1 = new JComboBox();
    JComboBox field2 = new JComboBox();
    JComboBox field3 = new JComboBox();
    JComboBox field4 = new JComboBox();      
    JButton button = new JButton();
    JPanel info = new JPanel();
    info.setBackground(Color.blue);
    info.add(field1, BorderLayout.NORTH);
    info.add(field2, BorderLayout.EAST);
    info.add(field3, BorderLayout.CENTER);
    info.add(field4, BorderLayout.WEST);
    info.add(button, BorderLayout.SOUTH);
    add(info, BorderLayout.EAST);
    add(canvas, BorderLayout.WEST);
    setTitle("TEST");
setSize(700, 500);
}

public static void main (String[] args) {
    JFrame testFrame = new Test();
    testFrame.setVisible(true);
}

}

Any help or suggestions about how to go about laying this out would be great.

Upvotes: 0

Views: 1113

Answers (1)

golfguru
golfguru

Reputation: 13

If you want your combo buttons and the button vertically stacked on top of each other, I would use a grid layout instead of a border layout. Just make the border layout have 1 column and 5 rows.

Upvotes: 1

Related Questions