[Java] Basic JFrame tutorial

Hello all, this is the first tutorial in a series that I’ve intended to make for a while. In this series, you will learn how to create a basic Java game using Java2D.

This tutorial will teach you how to create a window using JFrame. It’s not really interesting right now, but it is the most important part. Without knowing how to do this, you won’t be able to do anything else in the tutorial series.

Anyways, let’s start. In your fresh, blank project, create a class called “App” or “Aplication”, really whatever the duck you want to call it. You should have a class that looks generally like this:


public class App {

}

Now, add the following code between the curly braces.


public static void main(String[] args) {

}

This is the main function. It is the ‘entrance point’ of a program. It is called when the application is started. As it is currently, if you run this program nothing will happen because there is nothing in the main function.

Now create a class called GUI, or Frame, or anything like that. At the very top of the page, above the class, add the following:

import javax.swing.JFrame;

This code tells Java that it’s using the JFrame library. Speaking of which, you need to make the class extend JFrame now. Make the class header look like this:

public class GUI extends JFrame

This tells the program that the GUI class is a JFrame component. Now let’s define some variables. Between the curly braces, add these variables:


//public members

public final static int HEIGHT = 400;

public final static int WIDTH = 600;

//private members

private static final long serialVersionUID = 1L;

private static final String title = "TEST";

The HEIGHT and WIDTH variables define the size of the window that will be made. The title variable is what will show up in the OS’ GUI(In windows it’s the top left of the window frame). Now, let’s make the GUI class do stuff. The only method in the GUI class will be the constructor. The constructor will make a window pop up with the title in it. I know, fancy, right? Let’s hop to it. Add this code below the variables you defined.


public GUI()

{

this.setSize(WIDTH, HEIGHT); //set the size

this.setResizable(false); //make unresizable

this.setLocationRelativeTo(null); //center it

this.setTitle(title); //set title

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //make it close when it's closed.

this.setVisible(true); //make visible

}

The comments should explain this code pretty well. I would recommend experimenting with some of these, like setLocationRelativeTo or setResizable. What setFocusable does is make it so that you can click on the frame of the window and have it come to the front.

If you were to compile and run this code now, nothing would happen. That’s because the main method is empty. Let’s remedy this. Open up your App class and add a static GUI variable at the top called “gui”. Then call the gui Constructor in the Main method. Your app class should now look like this:


public class App{

static GUI gui;

static Game game;

public static void main(String[] args)

{

gui = new GUI();

}

}

and your GUI class should look like this:


import javax.swing.JFrame;

public class GUI extends JFrame

{

//public members

private static final long serialVersionUID = 1L;

private static final String title = "TEST";

//public members

public final static int HEIGHT = 400;

public final static int WIDTH = 600;

public GUI()

{

this.setSize(WIDTH, HEIGHT); //set the size

this.setResizable(false); //make unresizable

this.setLocationRelativeTo(null); //center it

this.setTitle(title); //set title

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //make it close when it's closed.

this.setVisible(true); //make visible

Game game = new Game();

add(game);

game.start();

}

}

I hope this post helps you understand a little bit of Java, as well as teaching you how to use JFrame to make a window. You can now compile and run it to see the fruits of your labor. I encourage you to experiment with this code, try things and get to understand it.

More tutorials here.

EDIT–

I can’t use tab to make indents. Java coding convention says that everything inside brackets{} should be indented. If you use an IDE, it should indent at the right places for you. If your indents are wrong, it’s no big deal, the code will run fine, but indenting code makes it easier to read and you should always do it.

Wow, even though I have none programme knowledge, this looks fairly easy. Reminds me of Javascript, hta, and some HTML attributes as well.

Do I need some sort of Library or Compiler for this? (I’m a newb).

I’m going to use this tutorial, thanks Eckhart, appreciate it!

Best regards,

Socuine

Yeah, Java is fairly easy.

As far as IDE goes, I go with Eclipse, but you can use any IDE or even notepad. Setting it up is a bit of work, but if you google for a tutorial it’s pretty easy. The tutorial series has been put on hold, but it shows you enough for you to make a simple simple game. For more complicated 2d game development, I’d recommend slick2D.

edit–

Added more about how to compile/run without Eclipse.

The only tools that are obligatory for this tutorial are the Sun/Oracle JRE (for run-time purposes) and the Sun/Oracle JDK (for development purposes).

Yours faithfully

S.J.R. van Schaik.

The only tools that are obligatory for this tutorial are the Sun/Oracle JRE (for run-time purposes) and the Sun/Oracle JDK (for development purposes).

Yours faithfully

S.J.R. van Schaik.

very true, edited my post giving a few more options on how to compile it. You really shouldn’t need Eclipse for this tutorial, but Eclipse is extremely helpful when you start creating your own code. Other IDEs are also good, Eclipse is just my favorite.

very true, edited my post giving a few more options on how to compile it. You really shouldn’t need Eclipse for this tutorial, but Eclipse is extremely helpful when you start creating your own code. Other IDEs are also good, Eclipse is just my favorite.

The only IDE I actually do use is Microsoft Visual Studio, otherwise I simply favour a shell (at least, if you do have a proper build system such as make, CMake or Ant), a version control system (such as Git or Mercurial), and a text editor that offers syntax high-lighting, line numbering, proper indentaton and guide lines instead. Some people might as well, and some might prefer an IDE.

Yours faithfully

S.J.R. van Schaik.

I have a BizSpark account so I could easily obtain Microsoft Visual Studio.

Is that also a compiler/IDE for Java? …

I have a BizSpark account so I could easily obtain Microsoft Visual Studio.

Is that also a compiler/IDE for Java? …

No, it only supports programming languages such as C++, C#.net, and Visual Basic.net. The two most popular choices amongst Java IDEs are Eclipse and NetBeans.

Yours faithfully

S.J.R. van Schaik.

The only IDE I actually do use is Microsoft Visual Studio, otherwise I simply favour a shell (at least, if you do have a proper build system such as make, CMake or Ant), a version control system (such as Git or Mercurial), and a text editor that offers syntax high-lighting, line numbering, proper indentaton and guide lines instead. Some people might as well, and some might prefer an IDE.

Yours faithfully

S.J.R. van Schaik.

First off you don’t count because you’re a certified genius ;D

In seriousness, I learn by doing and experimenting, and if I have an IDE it’s significantly easier to figure out why my shitty code is so shitty, or why it doesn’t work. Breakpoints are helpful, and Eclipse offers quick fixes that make guessing what the syntax is easier.

I have a BizSpark account so I could easily obtain Microsoft Visual Studio.

Is that also a compiler/IDE for Java? …

No. Eclipse and netbeans are both free though, so obtaining them is easy.

Thanks for the replies.

I’ll get myself NetBean and start compiling like a boss. <_<

Best regards,

Socuine

Iam

Thanks for the replies.

I’ll get myself NetBean and start compiling like a boss. <_<

Best regards,

Socuine

Me too. lol

Thanks for this tutorial eckhart, I’ll be using into help me get started in java. I plan to actually learn to program! *Gasp. Anyways thanks.

I’m actually thinking about using Java to create an RPG, and after reading this I really want to use Java. Would you have any knowledge with Java connecting to a database server of some sort? If I recall correctly Java needs to go through PHP before going connecting to a server. Example: Java > PHP > MySQL > PHP > Java

Anyway if you have any experience with that I would appreciate if you could help out in any way possible!

I’m actually thinking about using Java to create an RPG, and after reading this I really want to use Java. Would you have any knowledge with Java connecting to a database server of some sort? If I recall correctly Java needs to go through PHP before going connecting to a server. Example: Java > PHP > MySQL > PHP > Java

Anyway if you have any experience with that I would appreciate if you could help out in any way possible!

Why do you want to connect to a database using PHP? Use JDBC or JDBC-OBDC connectors. I believe they are given along with Java. Google it for more info.

Log in to reply