How to set JFrame size?

How to set JFrame Size


Many times we need to set the JFrame Size , to set JFrame size we need to use setPreferredSize to set the size.
To set JFrame size we will need the dimension, JFrame takes the Dimension object as an argument using which we can set the height and width as shown below :

Example How to set JFrame size


// How to set JFrame Size example
tFrame.setPreferredSize(new Dimension(420, 350));


Other way of setting the JFrame size is use setSize method which takes height and width as an argument.


Here is an example which sets the size of JFrame:

          // create a new jframe, and pack it
          JFrame frame = new JFrame("JFrame : Width 420 Height 320 ");
          

          //Set the frame size
          //frame.setSize( 500,400); // Setting size using setSize
           
          // Setting size using setPreferredSize
          frame.setPreferredSize(new Dimension(420,320));
          frame.pack();
          frame.setVisible(true);


Output :

                                                                           

Thus we have seen how to set JFrame Size. It can be set by setSize and setPreferredSize methods.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment