Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing set "actual" Frame size (inside)

Tags:

java

swing

frame

I have a JFrame which contains just one JPanel. I have tried setting the Panel's size and packing the frame, but that has no effect.

If I set the JFrame's size, it will change the size so it includes the title bar and borders. How do I set the "actual size" so it doesn't include the title bar and borders?

Example:

Example

Thanks in advance, guys

like image 305
CookieMonster Avatar asked May 06 '11 19:05

CookieMonster


People also ask

How do you adjust the size of a Swing frame?

Using setSize() you can give the size of frame you want but if you use pack() , it will automatically change the size of the frames according to the size of components in it. It will not consider the size you have mentioned earlier. Try removing frame.

How do I change the size of a content pane in Java?

In Java 5 and later this is the easiest method: JFrame frame = new JFrame("Content Pane Size Example"); frame. getContentPane(). setPreferredSize(new Dimension(400, 300)); frame.

How do I change the size of a JFrame in Java?

Change window Size of a JFrame To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height.

How do you make a JFrame fit the screen?

Setting a JFrame to fill half the screen For instance, if you want to create a JFrame that is half the width and half the height of the screen, you can just divide the dimensions in half, as shown in this code: Dimension screenSize = Toolkit. getDefaultToolkit(). getScreenSize(); aFrame.


1 Answers

You could set the contentPane's preferredSize and call pack on the JFrame, but in general it's usually best to let components size themselves based on their preferred sizes as determined by their layout managers.

like image 54
Hovercraft Full Of Eels Avatar answered Sep 25 '22 02:09

Hovercraft Full Of Eels