Layout Manager PreferSize Java

I'm still trying to find out how layout managers work. I made a frame with two JPanels. The first contains textArea with boxLayout. The second contains the layout of the stream using the button.

I set the preferred size of each panel accordingly, packed them, but got unexpected results.

import java.awt.*; import javax.swing.*; public class LayoutMgrTest { public static void main(String[] args) { TableBasic frame = new TableBasic(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.setVisible(true); frame.getContentPane().setLayout(new GridLayout(2,1)); JPanel controlPane = new JPanel(); JPanel buttonPane = new JPanel(); controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS)); controlPane.setPreferredSize(new Dimension(200, 200)); controlPane.add(new JScrollPane(new JTextArea())); buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT)); buttonPane.setPreferredSize(new Dimension(100,20)); buttonPane.add(new JButton("Button1")); buttonPane.add(new JButton("Button2")); frame.getContentPane().add(controlPane, BorderLayout.NORTH); frame.getContentPane().add(buttonPane, BorderLayout.SOUTH); frame.setSize(new Dimension(500,500)); frame.pack(); } } 

Whatever I do, if I use a grid layout, it always allocates half the available space for each control. I was told that:

The height of each row depends on the height of each component added to each row.

The height of the button is 20. She sets aside much more for her:

Wasted space

What is wrong with this code? I would like to leave two JPanels intact, please. It's easy to just add a text box and buttons directly to the frame, but I need to do this with JPanels (because I will add borders and other things).

+6
source share
3 answers

This is the result of using GridLayout as a layout manager. Change it to BorderLayout:

 frame.getContentPane().setLayout(new BorderLayout()); 

For example, this code (I changed a little from the original):

 import java.awt.*; import javax.swing.*; public class LayoutMgrTest { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); //frame.setVisible(true); //frame.getContentPane().setLayout(new BorderLayout()); JPanel controlPane = new JPanel(); JPanel buttonPane = new JPanel(); controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS)); controlPane.setPreferredSize(new Dimension(200, 200)); controlPane.add(new JScrollPane(new JTextArea())); buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT)); buttonPane.setPreferredSize(new Dimension(100,40)); buttonPane.add(new JButton("Button1")); buttonPane.add(new JButton("Button2")); frame.add(controlPane, BorderLayout.NORTH); frame.add(buttonPane, BorderLayout.SOUTH); //frame.setSize(new Dimension(500,500)); frame.pack(); frame.setVisible(true); } } 

Creates this frame:

enter image description here

+7
source

I set the preferred size of each panel accordingly,

This is another problem. Do not set the preferred size. This is the task of the layout manager. Just add your components to the panel and let the layout manager do the work.

Most components have a preferred default size. For some, you need to give a little clue. For example, when using a text area, you should specify the β€œsuggested” preferred size using:

 JTextArea textArea = new JTextArea(rows, columns); 
+6
source

If you are using LayoutManager, you should not set the size for the component other than the frame.

The size of components is calculated from different layout managers.

You will find more information at http://download.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html

in your code you can add a panel with a text box in BorderLayout.CENTER. this should solve your problem. a component in BorderLayout.CENTER occupies all the space except for the space required for components in NORTH, EAST, SOUTH, and WEST.

+3
source

Source: https://habr.com/ru/post/894978/


All Articles