I want to set the size of a JFrame such that the contentPane is the desired size. JFrame.setSize() doesn't take the window decorations into account, so the contentPane is slightly too small. The size of the window decorations are platform and theme specific, so it's bad news to try to manually account for them.
JFrame.getContentPane().setSize() fails because it's managed.
Ideas?
Thanks!
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.
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.
First a JFrame is created by invoking its constuctor. The argument to the constructor sets the title of the frame. The setSize(200,100) method makes the rectangular area 200 pixels wide by 100 pixels high. The default size of a frame is 0 by 0 pixels.
JFrames have a content pane, which holds the components. These components are sized and positioned by the layout manager when JFrame's pack() is called. Content pane border. There are several ways to handle the content pane, but most of them fail to provide one basic requirement -- ability to set a border.
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.pack();
frame.setVisible(true);
As always the above should be executed in the EDT.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With