Thursday, 16 January 2014

After our short introduction about what Swing / JFC is, it is time to code our first Swing GUI app. It is a simple HelloWorld app being nothing more than a window containing a "Hello World" text. But what sounds simple, is already the basic for all GUI apps you will code in the future.
The Java Tutorial introduces the "Hello World" app in the chapter "Compiling and Running Swing Programs" and explains the used Swing component "JFrame" in "How to Make Frames (Main Windows)".

The "Hello World" app's code

File "":
public class HelloWorldSwing {
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");

        //Display the window.

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

Compile the Java app's code

From within the directory where the Java-file is, issue the following command to compile the app:
This results in a bytecode file "HelloWorldSwing.class".

Run the Java app

Execute your app with
java HelloWorldSwing
It may start in the screen's corner and too small sized, but you can enlarge the window by dragging the right lower corner and move the window with drag and drop.
Here it is (resized to see all decorations, the title and the content):
Depending on your platform the style of the window decorations may vary.

That leads us to the first question: How to center the window on the screen?".

1 comment:

  1. Thanks for these help. Hope so that this will help us in making our work more amazing. People must also try to use the after effects templates that are specially designed according to the purpose that will make your blog more impressive and effective.