Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Swing Tutorial
#1
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
Reply
#2
Thats a pretty good tutorial man, thanks!

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Java Tutorial United States of America 0 682 01-23-2012, 03:39 PM
Last Post: United States of America
  why java haphazard 8 1,751 12-12-2011, 03:23 AM
Last Post: RDCA
  Need Java help from java expert. Blazin 2 2,091 09-07-2011, 02:43 PM
Last Post: AceInfinity
  Java help php 1 1,031 04-06-2010, 06:41 AM
Last Post: php
  Java Problem for real java programmer Testgamma1 10 5,140 03-14-2010, 09:08 AM
Last Post: uber1337

Forum Jump:


Users browsing this thread: 1 Guest(s)