Java Swing Tutorials

adplus-dvertising
Swing In Java

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
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
Swing Abstract Window Toolkit( AWT)
Swing classes define in the javax.swing packageAWT classes define in the java.awt package include subpackage java.awt.event
It is totally platform independent It is not totally platform independent
Written entirely in Java programming languageSome AWT component use native code
Build around the number of API that implement various part of the AWTSwing classes are built on top of the AWT architecture
Swing is Light weight component.AWT are heavy weight components
It is "borrows" the screen resource of an ancestor ( means it has no native resource of its own)It is associated with its own native screen resource (commonly known as a peer).