Select Page

We are going to be taking baby steps, and the first thing we are going to talk about when creating desktop applications is the frame. Let me start by explaining a little about what the frame I am talking about is, so the frame is actually a very simple concept which because it is like a piece of paper where the programmer is going to be drawing out lots of cool things. Since we are programming our GUI’s using the Swing library in Java we will need to create an instance of the JFrame class and assign it whatever name you want.

Example:

public static void main(String[] args) { 
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE_);
frame.setVisibility(true);
}

 

In the example we instantiate the JFrame object and call it frame. After we instantiate the JFrame object we must set the size of the frame with the method setSize(x,y); x is the number of pixels used for the horizontal measurement of the frame, and y is the number of pixels used for the vertical measurement of the frame. The SetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) is the method which closes the window, and the argument the method takes is the action you want the program to perform once the program closes(Wow, you have complete control!). In this case we tell the program to end once the window is closed. Finally, we must tell the program to actually display the window by using the setVisibility(true) method, and wala you should get something on your computer that looks like this:

 

 

 

 

 

 

 

 

 

 

 

 

This is the JFrame! This JFrame looks very simple and boring. Fortunately we can customize the frame a bit to meet the needs of your specific application. You probably already know we can customize the size of the JFrame with the setSize() method. If you look back at the picture of the JFrame model you will notice the little picture on the left corner; that is the default java logo, and that is the icon that shows up on the task bar when your application is running; however, we don’t have to keep that logo there because we can actually change it to whatever we like. Another thing we can add to the JFrame is a title.

Lets go into a little more detail on displaying those features. Everyone wants to create a unique name for their application, so I will begin be showing you how to do that. For this example I will name my application “Nerock!”. It sounded like a good idea at the moment.

Custom Title Example:

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setTitle(“Nerock”); // *See Description below

frame.setSize(400, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

 

This is the code for displaying a custom title of the application.

*The setTitle(“Nerock”) method is the method which allows us to customize the title of the JFrame. It takes a string parameter as an argument.

 

 

 

 

 

 

 

 

 

 

 

Wala, there is the JFrame with the custom title! That looks a little better, but I bet you might be thinking “Hmm that’s nice, but what about the icon.” Don’t you worry because guess what I am going to talk about next. Ya, you’re right. I am going to teach you how to change the little java icon.

Custom Java Icon Example:

public static void main(String[] args) {

JFrame frame = new JFrame();

ImageIcon icon = new ImageIcon("D:\\Programming\\Java Projects\\JFrame_Example\\src\\sourceInformation\\Smiley_Face.png"); // * See Description

frame.setTitle(“Nerock”);

frame.setIconImage(icon.getImage()); // ** See Description

frame.setSize(400, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

 

* We must create an instance of the ImageIcon class. In this case we called it icon; when we created this instance of the ImageIcon we used a string as an argument of the ImageIcon class. The string is the location of the image you want to use. Oh ya, make sure the image is in .png or in .jpg format.

** The setIconImage() method sets the icon. You probably could’ve guessed that by the name of the method. The method takes an image object as a parameter. The ImageIcon object we created named icon has a method called getImage which returns an image back; therefore, we can use that as the parameter of the setIconImage() method.

 

 

 

 

 

 

 

 

 

 

 

Those are the basic customization’s of the JFrame. Now you are on the right track to creating awesome desktop applications. I hope you enjoyed this, and I will continue to help guide you as much as I can on your programming journey.