0

So I set the setSize(500,500).. add some panels, the sum of panels Y is 500 like the JFrame but executing it shows an count Y of 525 am I missing something?

JPanel panel = new JPanel();
    panel.setLayout(null);
    getContentPane().add(panel);
//--------------------

    JPanel top_panel = new JPanel();
    top_panel.setLayout(null);
    top_panel.setBackground(Color.blue);
    top_panel.setBounds(0, 0, 500, 40);
    panel.add(top_panel);

    //------------------------------

    JPanel middle_panel = new JPanel();
    middle_panel.setLayout(null);
    middle_panel.setBackground(Color.yellow);
    middle_panel.setBounds(0, 40, 500, 385);
    panel.add(middle_panel);

    //-----------------------------

    JPanel bottom_panel = new JPanel();
    bottom_panel.setLayout(null);
    bottom_panel.setBackground(Color.black);
    bottom_panel.setBounds(0, 425, 500, 75);
    panel.add(bottom_panel);

    setSize(500,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setLocationRelativeTo(null);

40+385+75 = 500 but to show all the panels i must

setSize(500,525);

then it fits

here's an image: enter image description here

Lucas Bertollo
  • 373
  • 2
  • 5
  • 19

2 Answers2

3

The frame size is the light blue rectangle outside bounds including the title bar. Your panels are appearing in the inner bounds which is frame size less than the frame border and frame title bar. Do you see how your marked space at the bottom is strangely the same height as the title bar?

After adding your panels/component to the frame and just before calling frame.setVisible(true), call frame.pack().

It would be also preferable if you embrace a layout manager (such as FlowLayout) and when necessary call setPreferredSize and let the layout manager do the layout. Normally one would call setPreferredSize over setBound, setSize, setMininumSize, setMaximumSize.

import javax.swing.*;
import java.awt.*;

public class FrameSize {

    private JFrame frame;

    FrameSize create() {

        frame = createFrame();
        frame.getContentPane().add(createContent());

        return this;
    }

    private JFrame createFrame() {
        JFrame frame = new JFrame(getClass().getName());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        return frame;
    }

    void show() {
        //   frame.setSize(500, 500);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    private Component createContent() {
        JPanel panel = new JPanel(null);

        JPanel topPanel = new JPanel(null);
        topPanel.setBackground(Color.blue);
        topPanel.setBounds(0, 0, 500, 40);
        panel.add(topPanel);

        JPanel middlePanel = new JPanel(null);
        middlePanel.setBackground(Color.yellow);
        middlePanel.setBounds(0, 40, 500, 385);
        panel.add(middlePanel);

        JPanel bottomPanel = new JPanel(null);
        bottomPanel.setBackground(Color.black);
        bottomPanel.setBounds(0, 425, 500, 75);
        panel.add(bottomPanel);

        panel.setPreferredSize(new Dimension(500, topPanel.getBounds().height + middlePanel.getBounds().height + bottomPanel.getBounds().height));
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FrameSize().create().show();
            }
        });
    }
}
javajon
  • 1,660
  • 16
  • 18
  • i call the pack() but now it appears an strange frame with like 100x25 ... are you sure the frame size count the title bar? – Lucas Bertollo Jul 05 '14 at 06:01
  • Yes. frame.setSize sets the entire frame size, not the inner contentpane size. See modified code above. Layout managers and setPreferredSize is the way to go. Same advice applies for JavaFX. – javajon Jul 05 '14 at 06:38
3

You shouldn't be setting size or calling setSize(...) or setBounds(...) as that's setting you up for similar problems in the future, or worse problems when you try to show your GUI on a different platform. Instead let the preferredSizes of your components and the layout managers do this work for you. If you absolutely must set the size of a component, then override getPreferredSize() and return a Dimension that is calculated to work for you. And yes, as per javajon, you should call pack() on the JFrame before displaying it.

For more discussions on the null layout, please read what one of the best Swing experts on this site, MadProgrammer, has to say in his answer here.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373