• 1/29/14 6:44 pm
- 623 views • 0 today
- 4
- 1
64
Hey guys! I'm finally back with another tutorial! Now we will be taking a look at actually creating a window in Java. What we want to start off by doing is creating a new project, package, and a Class called “FrameTest”, like we did in “Hello”. If you don’t know how to do this, I suggest taking a look in previous tutorials. What we first want to do first is import “javax.swing.JFrame”. This will allow us to use Java’s window building tools. We can import this like we imported our Scanner. Under where you imported “java.util.Scanner” we will type the following:
import javax.swing.JFrame;
Now, we will create a window. First, make your main method like in your “Hello” application. Now, we will write the following at the very top of our main method:
JFrame frame = new JFrame(“Hello”);
This is telling the program to create a new JFrame (window) called frame and make the title “Hello”. But this doesn’t appear on the screen yet. First we have to set a couple of its values. Right under where we made “frame”, we will write these few lines:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 100);
What the first line will do, is basicly tell the program that’s if it’s closed, to actually close and not just run in the background. The second line is pretty self-explanatory. All it does is set the window’s size. Feel free to change the 100s to any number you want.
To write text into our window, we will have to make a JLabel. And of course, to use it, we will have to import it like JFrame by typing the following under where we imported JFrame:
import javax.swing.JLabel;
Now, let’s add it to our window. We will do this by creating a label called testlabel. Type this under the other stuff of your main method:
JLabel testlabel = new JLabel();
This will make a label called “testlabel”. Now we want to make the label, and text, appear on the screen. So under where we created testlabel, lets write the following:
testlabel.setText("Hello, world!");
frame.add(testlabel);
The first line simply sets the text inside your label to “Hello, world!” and the second adds it to your frame (window).
The last thing we are going to do, is make the window show up on your screen. To do this we will write the following under what we just did:
frame.setVisible(true);
Your final product should look somewhat like this:

Congrats! You can now export this program and run it just like we did with “Hello” and it will make a window appear! We will do more dynamic stuff with this program in the next tutorial! Thanks for reading, see you soon!
import javax.swing.JFrame;
Now, we will create a window. First, make your main method like in your “Hello” application. Now, we will write the following at the very top of our main method:
JFrame frame = new JFrame(“Hello”);
This is telling the program to create a new JFrame (window) called frame and make the title “Hello”. But this doesn’t appear on the screen yet. First we have to set a couple of its values. Right under where we made “frame”, we will write these few lines:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 100);
What the first line will do, is basicly tell the program that’s if it’s closed, to actually close and not just run in the background. The second line is pretty self-explanatory. All it does is set the window’s size. Feel free to change the 100s to any number you want.
To write text into our window, we will have to make a JLabel. And of course, to use it, we will have to import it like JFrame by typing the following under where we imported JFrame:
import javax.swing.JLabel;
Now, let’s add it to our window. We will do this by creating a label called testlabel. Type this under the other stuff of your main method:
JLabel testlabel = new JLabel();
This will make a label called “testlabel”. Now we want to make the label, and text, appear on the screen. So under where we created testlabel, lets write the following:
testlabel.setText("Hello, world!");
frame.add(testlabel);
The first line simply sets the text inside your label to “Hello, world!” and the second adds it to your frame (window).
The last thing we are going to do, is make the window show up on your screen. To do this we will write the following under what we just did:
frame.setVisible(true);
Your final product should look somewhat like this:

Congrats! You can now export this program and run it just like we did with “Hello” and it will make a window appear! We will do more dynamic stuff with this program in the next tutorial! Thanks for reading, see you soon!
2722101
6



Have something to say?