Friday, March 18, 2011

How to create GUI in Java

In last post we have created a GUI. The basic to crate a GUI is consist of following steps.......

1.Create a class that extends JFrame/Jpanel/JApplet.

2.Create a default constructor, and a method void initComp() .In this method in first line set the layout of frame to null.

3.Call the initComp method in constructor, and in next line set the title of main window(this.setTitle("Title");) , then in next line set bounds, then set default close operation to EXIT_ON_CLOSE , then in next line setResizable.

4.Now write main method and write..(new <classname>().setVisible(true);).

5.Now your code is ready to run but this will only show a blank window.

6.To add component on this window firstly declare the objects of components. Now create a method for each component like..void myCompnent(). In this method first create object then set text,set bounds, set visible and then add to this frame. Now if you want to handle any event with this component write the event handler for the componet.

7.Now call these methods in the initComp method.

8.Now the code is complete you can use it.

The main problem in this code is to write event handler which I publish in next post.


1 comment: