Support Forums

Full Version: Java Swing Tutorial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

I'm going to attempt to teach you how to make your first Java Swing Application. I may not be good at explaining, nor is my English perfected. So I'll have to excuse myself for that. Anyways, let's get started:


Legend:
Explination:
Suggestion:
Notice:

Step 1:

Make a new Class file in your desired location, call it "FirstApplication.java" and input the base for it:

Code:
public class FirstApplication {
}

Step 2:
Now since this is a Java Swing Application, that means we're gonna have to Import it:

Code:
import javax.swing.JFrame;

So it should now look something similar to:

Code:
import javax.swing.JFrame;
public class FirstApplication extends JFrame {
}

Step 3:
Ok, now that we have the Imports ready, we can start the settings of the JFrame:

Code:
public FirstApplication() {
setSize(300, 200);
setTitle("My First Application");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

setSize(int, int) is the method that determines the size of your application, quite easy to understand. You can play around with this method.

setTitle("String") sets the Title of your Application.

setDefaultCloseOperation(EXIT_ON_CLOSE) makes your client close after an action like clicking 'X' for example.

Step 4:
It's time we make the Application actually Run.
In order to do this, we will have to use the Main method, of course:

Code:
public static void main(String[] args) {
FirstApplication firstapplication = new FirstApplication();
firstapplication.setVisible(true);
}

firstapplicaiton.setVisible(boolean) Makes the Application either visible or invisible (true/false)

Step 5:
It's almost time to run the Application! But first, make sure you've done everything correctly:

Code:
package com.naxx0.firstapplication;

import javax.swing.JFrame;

public class FirstApplication extends JFrame {

    public FirstApplication() {

        setSize(300, 200);
        setTitle("My First Application");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
   }

    public static void main(String[] args) {
        FirstApplication firstapplication = new FirstApplication();
        firstapplication.setVisible(true);
    }
}

Ignore the 'package com.naxx0.firstapplication' that's just my packaging for this Application.

However, I do suggest you learn how to package stuff, since it will come in handy later on in your Java experience.

Step 6:
It's time to run the Application! I'm not going to teach you how to Compile your project, because this you should already know if you're attempting to learn Java at the level of JFrame's.

Compile it, Run it, and you should get results likely to this:

[Image: 169ri0x.jpg]

Congratulations, you have successfully created your first JFrame ! Thumbsup


What's Next?
In the next tutorial, I am going to help you develope this even farther including JMenus, JButtons, Images, and more. Stay tuned! Yeye


Cheers! Sleep
Thats a pretty good tutorial man, thanks!

But you could probably fit a classic "Hello World!" in there!
Can you do a tutorial on NetBeans?