First swing program

You must read first swing

Here's a snapshot of the HelloWorldSwing program:
 
The HelloWorldSwing application.

 And here's the code for HelloWorldSwing:
import javax.swing.*;        



public class HelloWorldSwing {

public static void main(String[] args) {
JFrame frame = new JFrame("HelloWorldSwing");
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}

Explanation

The main function creates an instance of JFrame adds a label to it and then displays the window.

JFrame

JFrame is a window that has decorations, such as a border, a title, and buttons for iconifying and closing the window. Applications with a GUI typically use at least one frame. You can add different components to it.

constructor

The constructor for JFrame is
JFrame()

JFrame(String title)

  
title is shown on the top of the window.

Add components

To add other components to JFrame , we use
frame.getContentPane().add(component);

In our example, the component is a JLabel .

Close windows

To close the window when the close button close is clicked, we include this code in our HelloWorldSwing program:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  
JFrame provides the setDefaultCloseOperation method to configure the default action for when the user clicks the close button. For single-window applications, most likely you want the application to exit. The EXIT_ON_CLOSE constant lets you specify this, as of version 1.3 of the Java 2 Platform.

Set the size

frame.pack();

  
causes this window to be sized to fit the preferred size and layouts of its subcomponents. We can use
frame.setSize(200,100);

  
to set a window with 200 pixels width and 100 pixels height.

Show the window

frame.setVisible(true);

  
is used to set the frame visible.

JLabel

label JLabel can be used to display unselectable text and images. If you need to create a component that displays a string or an image (or both), optionally reacting to user input, you can do so by using or extending JLabel . If the interactive component has state, then you should probably use a button (JButton) instead of a label.

Constructors

some useful constructors are
JLabel()

JLabel(text)

  
text is displayed on the JLabel.

Methods

Some useful methods are
String getText()

void setText(String text)

  
the meaning is obvious.

HTML

You can even show text using simple html. Example

import javax.swing.*;

public class HtmlSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("HtmlSwing");
JLabel label = new JLabel("");
String htmlText=
"<html>\n" +
"<h2> Hi </h2>\n" +
"<font color = red> PIC20 Students </font>," + // red color
"<i><font color = blue> How are you ? </font></i>" + // italic
"</html>";
label.setText(htmlText);
frame.getContentPane().add(label);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}

here is how it looks like
htmlswing

package

import

The first line imports the main Swing package:
import javax.swing.*;

  
This is the only package that HelloWorldSwing needs. However, most Swing programs also need to import two AWT packages:
import java.awt.*;

import java.awt.event.*;

  
These packages are required because Swing components use the AWT infrastructure, including the AWT event model. The event model governs how a component reacts to events, such as button clicks and mouse motion. You'll learn more about events later.

What is package?

A package is a collection of related classes.
The swing classes are under javax.swing package.
We have 2 ways to use the class in a package.

Use full name

The HelloWorldSwing.java can be rewritten as

public class HelloWorldSwing2 {
public static void main(String[] args) {
javax.swing.JFrame frame = new javax.swing.JFrame("HelloWorldSwing");
javax.swing.JLabel label = new javax.swing.JLabel("Hello World");
frame.getContentPane().add(label);

frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.pack();
//frame.setSize(200,200);
frame.setVisible(true);
}
}

Use import

* is a wild character. That means the computer imports everything in the javax.swing package. We can also just import the class you want. that means, the first line can actually be replaced by
import javax.swing.JFrame;

import javax.swing.JLabel;