Add to Google Reader or Homepage

jFrame Size

How to size a frame?


The size of the frame also plays an important role in a good user-interface design.By default the frame that you would create will be of size 0x0 pixels.So you need to set the size of the frame manually.

There are two methods available for this purpose 

 1.setSize(int width,int height)    
 2.setBounds(int x,int y,in width,int height)

If you call the setSize() method with the desired width and height as arguments
then the frame will be generated with the given specifications.

Program:


Myframe.java

import javax.swing.*;
import java.util.*;
class Myframe
{
public static void main(String args[])
{
Firstframe f1=new Firstframe();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
}
}
 

 Firstframe.java

import javax.swing.*;
import java.util.*;
import java.awt.*;
class Firstframe extends JFrame
{
Firstframe()
{
setSize(200,200);// frame of width 200 pixels and height 200 pixels
setLocation(50,50);
setTitle("My FirstFrame");
}
}
 

If you execute this program with the java launcher then you would see a frame of 200x200 pixels.

By using the setBounds() method you could be able to define the size of the frame along with its position.So there is no need of a separate setLocation() method to specify the location of the frame.Here i have edited the program shown above as follows.

Program:


Firstframe.java

import javax.swing.*;
import java.util.*;
import java.awt.*;
class Firstframe extends JFrame
{
Firstframe()
{
setBounds(50,50,200,200);//Locates the frame at the desired location                                                                               with the given size.
setTitle("My FirstFrame");
}
}
 

0 comments:

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top