Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set specific window (frame) size in java swing?

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?

like image 411
J.Olufsen Avatar asked Nov 19 '11 11:11

J.Olufsen


People also ask

How do I set the size of a JFrame window?

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.

How do I increase the size of a JFrame?

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.

How do you change screen size in Java?

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.


1 Answers

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.

like image 83
gprathour Avatar answered Sep 28 '22 09:09

gprathour