Programming & Scripting Tutorials

Java: JFrame



Ok.. So in this lesson we are going to use JFrame to have a proper window with stuff on(in this case a label).
Ok so first of all lets create a new class (and lets create it in a new class file, so we dont confuse things).. Name it Class(.java)

Ok. So first of all in our new file Class.java lets import some important things:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

FlowLayout gives us our normal sort of layout so things go next to each other until they are at the end of the window in which case they start a new line.
JFrame is basically our whole window and everything.
JLabel allows us to put labels onto the window.
At the moment we have a basic public class in our Class.java file, however for the whole form to work properly we need to make the class 'Class' be derived from JFrame, we do this using the extends keyword (as you know):

public class Class extends JFrame{

Ok now we can really get started, so firstly lets just create a JLabel variable:

private JLabel LabelOne;

Now lets create a constructor to do everything:

public Class(){

}

Now we can start specifying things about the window, firstly lets specify the title:

super("App Title");

Ok, now we need to have a line of code which makes the program exit nicely when the cross off button is clicked:

setDefaultCloseOperation(EXIT_ON_CLOSE);


Next we have to set a size for the window:

setSize(300, 200);


Ok so thats all good, now lets set the layout:

setLayout(new FlowLayout());

All this does is specify the layout to the flow one we imported.
Now lets start using that JLabel:

LabelOne = new JLabel("This is a label!");

Now we can simply add the item in the window:

add(LabelOne);

Finally the last thing we have to do in this class is set the window to be visible:

Object.setVisible(true);

Ok so now lets go back to out main file (having saved the class one), lets just import JFrame:

import javax.swing.JFrame;

Now lets create our Class object:

Class Object = new Class();

You can now run your program successfully!

There is just one last thing I want to show you in this lesson, and that is a tooltip, these are just little boxes which show up when you hover over something, so we are just going to quickly create a tooltip for our label.
Go back to your Class.java file, and in the constructor below where you set your label contents insert this line to create a tooltip:

LabelOne.setToolTipText("Tooltip Text!");

You can now rerun your program and you'll see that when you hover over the label "Tooltip Text!" will come up in a little hoverbox.

The full code for this lesson is:

Main.java:

import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
       Class Object = new Class();
    }

}


Class.java:


import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Class extends JFrame{

    private JLabel LabelOne;

    public Class(){
super("App Title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 200);
setLayout(new FlowLayout());
LabelOne = new JLabel("This is a label!");
LabelOne.setToolTipText("Tooltip Text!");
add(LabelOne);
setVisible(true);
}

}




This Java tutorial was written by


Back to Java

Advertisement: