My code does not work:
JFrame frame = new JFrame("mull"); mull panel = new mull(); // add panel to the center of window frame.getContentPane().add("Center", panel); frame.setSize(500, 300); // << not working!!! frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); // give a suitable size to window automatically frame.setVisible(true); // make window visible
I am getting very small window. How to fix it?
setSize() and frame. pack() . You should use one of them at one time. 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.
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. Below is the code to change the window size of a JFrame.
To change display mode with Java Swings, use the setDisplayMode() method. Here, we have set the Display mode as: new DisplayMode(800, 600, 32, 60)); Now, when you will run the program, the frame would be visible in a different resolution than the actual set resolution of your system.
Well, you are using both frame.setSize()
and frame.pack()
.
You should use one of them at one time.
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.pack()
from your code or putting it before setting size and then run it.
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