Panel Size Setting

I have 3 panels. One of them is the main panel, on which two small panels are located.

For the main panel, I used

setPreferredSize(new Dimension(350, 190)); 

For the smaller left panel I used

 setPreferredSize(new Dimension(100, 190)); 

For the smaller right pane, I used

 setPreferredSize(new Dimension(250, 190)); 

but smaller panels remain the same size. How can i fix this? alt text

This is the code that I have in my main panel.

 import model.*; import java.awt.*; import javax.swing.*; public class Panel extends JPanel { public Panel(Prison prison) { setup(); build(prison); } private void setup() { setBorder(BorderFactory.createLineBorder(Color.blue)); setLayout(new BorderLayout(1, 1)); setPreferredSize(new Dimension(350, 190)); } private void build(Prison prison) { JTabbedPane tab = new JTabbedPane(); tab.addTab("Input", null, new InputPanel(), "Input"); tab.addTab("Display", null, new DisplayPanel(), "Display"); add(tab); } } 
+4
source share
4 answers

Do not do this.

The whole point of layout managers is to allow dynamic resizing, which is necessary not only for user-modifiable windows, but also changing texts (internationalization) and different font sizes and default fonts.

If you just use layout managers correctly , they will take care of the panel sizes. To avoid stretching your components across the screen when the user enlarges the window, you have the outermost panel with the FlowLayout left-aligned and the rest of the user interface as its one child - this will give the UI its preferred size and any excess will be filled with the background color.

+6
source

It looks like you are using a GridLayout or maybe FlowLayout is not what you want. You probably want to use BoxLayout , which respects its preferred dimensions for its components.

 final JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); p.add(leftPanel); p.add(mainPanel); p.add(rightPanel); 
+1
source

also is the preferred size. if you do not want to resize, you can also set maximum sizes.

if you are able, you can check out the MIG layout, but BoxLayout is also easy to use and in Java toolkit already.

0
source

This is one thing.

 buttonPanel = new JPanel(); buttonPanel.setSize(new Dimension(30, 100)); 

Do not let the layout manager resize.

-2
source

All Articles