Creating a Frame using Java
Hi Guys
In this post we are going to create a simple frame in java.
From the previous post we had created a new project and that's where we are going to begin from.
In Java there is a package known as the swing package which contains all the classes used in Graphical user interface development in java.
One of the classes is the JFrame class which enables us to creates Frames in Java. This is the class we are going to use to create our frame as follows
Locate the statement similar to the above in your code and add extends JFrame. This means that we are inheriting the all the methods available in the JFrame class to our class MyFrame.
Make sure the you add the correct import statements to your code to avoid errors that might be caused due to lack of such statements.These import statements are usually above the class name .
After inheriting from the JFrame class then we create a constructor of our class.
A constructor is just like a method but has no return type and has the same name as the class name Its usually invoked immediately when the class object is instantiated
Inside the constructor is where we are going to start our coding and we will begin by calling some methods from the JFrame class as follows. Remember our goal is to create a simple frame in Java.
Now inside the main method we create an instance of our class which invokes our constructor once we run the program.
The final code will be as follows
In this post we are going to create a simple frame in java.
From the previous post we had created a new project and that's where we are going to begin from.
In Java there is a package known as the swing package which contains all the classes used in Graphical user interface development in java.
One of the classes is the JFrame class which enables us to creates Frames in Java. This is the class we are going to use to create our frame as follows
public class MyFrame {
Locate the statement similar to the above in your code and add extends JFrame. This means that we are inheriting the all the methods available in the JFrame class to our class MyFrame.
Make sure the you add the correct import statements to your code to avoid errors that might be caused due to lack of such statements.These import statements are usually above the class name .
After inheriting from the JFrame class then we create a constructor of our class.
A constructor is just like a method but has no return type and has the same name as the class name Its usually invoked immediately when the class object is instantiated
public MyFrame(){//this is the constructor
}
Inside the constructor is where we are going to start our coding and we will begin by calling some methods from the JFrame class as follows. Remember our goal is to create a simple frame in Java.
public MyFrame(){//this is the constructor
setVisible(true);//this method makes our frame to be visible
setTitle("My Title");//Give a title to our frame
setSize(500,300);//sets the size of the frame
setDefaultCloseOperation(EXIT_ON_CLOSE);//exits the program once you click the exit button
}
Now inside the main method we create an instance of our class which invokes our constructor once we run the program.
public static void main(String[] args) {
MyFrame frame=new MyFrame();
}
The final code will be as follows
import javax.swing.JFrame;
public class MyFrame extends JFrame{
public MyFramet(){//this is the constructor
setVisible(true);//this method makes our frame to be visible
setTitle("My Title");//Give a title to our frame
setSize(500,300);//sets the size of the frame setDefaultCloseOperation(EXIT_ON_CLOSE);//exits the program once you click the exit button
}
public static void main(String[] args) {
MyFrame frame=new MyFrame();
}
}
The output of this code will be as below. Just a simple frame will have been created with the title my Title
0 comments:
Post a Comment