Swing Examples
2.1 Swing Basic Example
Swings
Swing is also part of Sun Microsystems' Java Foundation Classes
(JFC) — "an Application Programming Interface (API) for providing a graphical
user interface (GUI) for Java programs" , introduce to overcome the limitation
of AWT. The Swing classes are built on top of the AWT architecture. The JFC also provide some other important features to a GUI program, like
the ability to add rich graphics functionality, ability to create a program that
can work in different languages/platform, and by users with different input
devices.
Write a program that "display the component in Jframe".
Use the following program
/*
* Save as a FirstFrame.java
* Program that display All the Component.
*/
package r4r.co.in;
import java.awt.Color;
import javax.swing.*;
public class FirstFrame extends java.awt.Frame {
//Define the Component and used it into the program
private JLabel label;
private JButton button;
private JTextField field;
private JTextArea area;
public FirstFrame() {
init();
}
private void init() {
JFrame frame = new JFrame("Frame in Component of AWT ");
JPanel panel = new JPanel();
frame.setVisible(true);
frame.setBounds(300, 400, 300, 400);
panel.setBackground(Color.orange);
panel.setLayout(null);
//Initilizad the Component
label = new JLabel("R4R Tech Soft! Welcome");
field = new JTextField("This is TextField", 20);
button = new JButton("Click Me");
area = new JTextArea(null, "This is TextArea", 4, 5);
//Add the component into panel to display inside the JFrame
label.setBounds(30, 20, 300, 30);
panel.add(label);
field.setBounds(25, 50, 200, 30);
panel.add(field);
button.setBounds(30, 100, 150, 50);
panel.add(button);
area.setBounds(20, 180, 200, 100);
panel.add(area);
//Add panel to the Frame alongwith its component
frame.getContentPane().add(panel);
//Free all the resource and unload the JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
FirstFrame firstFrame = new FirstFrame();
}
}
OUTPUT:

Difference Between Swing And AWT.
Difference between them-
|
Swing |
Abstract Window Toolkit( AWT) |
- Swing classes define in the
javax.swing package.
- It is totally platform independent.
- Written entirely in Java programming language.
- Build around the number of API that implement various part of
the AWT.
- Swing is Light weight component.
- It is "borrows" the screen resource of an ancestor ( means it has no
native resource of its own).
|
- AWT classes define in the
java.awt package include subpackage java.awt.event
- It is not totally platform independent.
- Some AWT component use native code.
- Swing classes are built on top of the AWT architecture.
- AWT are heavy weight components.
- It is associated with its own native screen resource
(commonly known as a peer).
|
Tolal:0 Click:
Show All Comments