Swing is a graphical user interface library for the J2SE platform. It's possible to give excellent look and feel through the pluggable look and feel system of Swing. Where prior implementations of these looks and feels may have been considered lacks behind, Swing in Java SE 6 addresses this problem by using more native GUI widget drawing routines of the underlying platforms.
This example Swing application creates a single window with "Hello, world!" inside:
// Hello.java (Java SE 5)
import javax.swing.*;
public class Hello extends JFrame {
public Hello() {
super("hello");
super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
super.add(new JLabel("Hello, world!"));
super.pack();
super.setVisible(true);
}
public static void main(final String[] args) {
new Hello();
}
}
The first import includes all of the public classes and interfaces from javax.swing package.
The Hello class, as above ,extends the JFrame class; the JFrame class implements a window with a title bar and a close control.
The Hello() constructor initializes the frame by first calling the superclass constructor, passing the parameter "hello", which is used as the window's title.
Then it calls setDefaultCloseOperation(int) method inherited from JFrame to set the default operation when the close control on the title bar is selected to WindowConstants.EXIT_ON_CLOSE — this causes the JFrame to be disposed of when the frame is closed, which enables the JVM to exit and the program to terminate.
Next,JLabel is created for the string "Hello, world!" and the add(Component) method inherited from the Container superclass is called to add the label to the frame.
The pack() method which is inherited from the Window superclass is called to size the window and lay out its contents.
The main() method is called by the Java virtual machine when the program starts.
This instantiates a new Hello frame and causes it to be displayed by calling the setVisible(boolean) method inherited from the Component superclass with the boolean parameter true.
If once the frame is displayed, exiting the main method does not cause the program to terminate because the AWT event dispatching thread remains active until all of the Swing top-level windows have been disposed.
Swing also consists of thousand of classes and its controls are consistent in look and feel.
This example Swing application creates a single window with "Hello, world!" inside:
// Hello.java (Java SE 5)
import javax.swing.*;
public class Hello extends JFrame {
public Hello() {
super("hello");
super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
super.add(new JLabel("Hello, world!"));
super.pack();
super.setVisible(true);
}
public static void main(final String[] args) {
new Hello();
}
}
The first import includes all of the public classes and interfaces from javax.swing package.
The Hello class, as above ,extends the JFrame class; the JFrame class implements a window with a title bar and a close control.
The Hello() constructor initializes the frame by first calling the superclass constructor, passing the parameter "hello", which is used as the window's title.
Then it calls setDefaultCloseOperation(int) method inherited from JFrame to set the default operation when the close control on the title bar is selected to WindowConstants.EXIT_ON_CLOSE — this causes the JFrame to be disposed of when the frame is closed, which enables the JVM to exit and the program to terminate.
Next,JLabel is created for the string "Hello, world!" and the add(Component) method inherited from the Container superclass is called to add the label to the frame.
The pack() method which is inherited from the Window superclass is called to size the window and lay out its contents.
The main() method is called by the Java virtual machine when the program starts.
This instantiates a new Hello frame and causes it to be displayed by calling the setVisible(boolean) method inherited from the Component superclass with the boolean parameter true.
If once the frame is displayed, exiting the main method does not cause the program to terminate because the AWT event dispatching thread remains active until all of the Swing top-level windows have been disposed.
Swing also consists of thousand of classes and its controls are consistent in look and feel.







No comments:
Post a Comment