Can't choose a JFrame for JFrame . The task is to arrange the elements (panels) in the form as here

alt text

The panels will be different in height but aligned in width. I tried GridLayout , GridBagLayout and FlowLayout while there was a continuous dance of components.

    4 answers 4

    I think that GridBagLayout is right for you ... you just have to deal with it,
    Here is the link , here is a description and an example.

    • Thanks, I try. - erofeev
    • GridBagLayout is what you need, thanks. - erofeev
    • Well, that's great, that figured out!) - Kobayashi_Maru

    judging by the picture, you need a grid layout, just to create 2 columns (2 panels), and then you can already "shove it into a mess." unfortunately, if you use FlowLayout and do not set the width of the components, they can crawl into one line, so it’s better to use a specialized layout manager right away. The code creates a window, 2 columns with 3 panels each. each panel has a different height:

     /// add random height text area private static void updateLabel(JPanel panel) { Random r = new Random(); int random = r.nextInt(15) + 1; TextArea textArea = new TextArea(random, 15); panel.add(textArea); } public static void main(String[] args) { // create and customize frame JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setLayout(new GridLayout(1, 2)); f.setSize(400, 400); // create panels JPanel left = new JPanel(); left.setBackground(Color.YELLOW); JPanel right = new JPanel(); right.setBackground(Color.BLUE); // add main panels to the frame f.add(left); f.add(right); // set layout managers left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS)); right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS)); // add components JPanel black = new JPanel(); black.setBackground(Color.BLACK); JPanel red = new JPanel(); red.setBackground(Color.RED); JPanel green = new JPanel(); green.setBackground(Color.GREEN); JPanel cyan = new JPanel(); cyan.setBackground(Color.CYAN); JPanel gray = new JPanel(); gray.setBackground(Color.GRAY); JPanel orange = new JPanel(); orange.setBackground(Color.ORANGE); left.add(black); left.add(red); left.add(green); right.add(cyan); right.add(gray); right.add(orange); updateLabel(black); updateLabel(red); updateLabel(green); updateLabel(cyan); updateLabel(gray); updateLabel(orange); f.setVisible(true); } 

      TableLayout with 1st TableLayout and two columns (actually 2 horizontal cells) Next, inside each of the cells we make FlowLayout

        you need BoxLayout

        First you do the panel and insert BoxLayout

         JPanel pnelBig=new JPanel(); panelBig.addLayout(new BoxLayout(panelBig, BoxLayout.X_AXIS)); 

        then you insert there two panels with BoxLayout and Y_AXIS , and in them already your panels