Awt-Swing-Java
Awt-Swing-Java
Study material on building Graphical User Interfaces (GUIs) in Java using AWT and
Swing, and how to handle events in these applications.
import java.awt.*;
public class SimpleAWTExample extends Frame {
public SimpleAWTExample() {
// Set the title of the frame
setTitle("My AWT Window");
// Create a button
Button button = new Button("Click Me");
// Add the button to the frame (using default FlowLayout)
add(button);
// Set the size of the frame
setSize(300, 200);
// Make the frame visible
setVisible(true);
// Handle window closing (using an anonymous inner class for a WindowListener)
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new SimpleAWTExample();
}
}
● Limitations of AWT:
○ Heavyweight components lead to platform dependency.
○ Limited set of components compared to Swing.
○ More complex event handling model initially (though the delegation model
improved this).
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleSwingExample extends JFrame implements ActionListener {
private JButton button;
private JLabel label;
public SimpleSwingExample() {
// Set the title of the frame
setTitle("My Swing Window");
// Set the default close operation (exit the application when closed)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the layout manager for the frame's content pane
setLayout(new FlowLayout());
// Create a label
label = new JLabel("Hello, Swing!");
add(label);
// Create a button
button = new JButton("Click Me");
// Add an ActionListener to the button (for event handling)
button.addActionListener(this);
add(button);
// Set the size of the frame
setSize(300, 200);
// Make the frame visible
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// This method is called when the button is clicked
if (e.getSource() == button) {
label.setText("Button Clicked!");
}
}
public static void main(String[] args) {
// Run the GUI creation on the Event Dispatch Thread (EDT)
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SimpleSwingExample();
}
});
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EventHandlingAnonymousExample extends JFrame {
public EventHandlingAnonymousExample() {
setTitle("Anonymous Listener");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JButton button = new JButton("Click Me");
JLabel label = new JLabel("Not clicked yet");
// Add an ActionListener using an anonymous inner class
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Button was clicked!");
}
});
add(button);
add(label);
setSize(300, 150);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new EventHandlingAnonymousExample();
}
});
}
}
● Example Code (Using a Lambda Expression for a Button Click - Java 8+):
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent; // Still need ActionEvent class
public class EventHandlingLambdaExample extends JFrame {
public EventHandlingLambdaExample() {
setTitle("Lambda Listener");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JButton button = new JButton("Click Me");
JLabel label = new JLabel("Not clicked yet");
// Add an ActionListener using a lambda expression
button.addActionListener(e -> label.setText("Button was clicked!"));
add(button);
add(label);
setSize(300, 150);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new EventHandlingLambdaExample());
}
}
● Adapter Classes:
○ For listener interfaces with multiple methods (like MouseListener or
WindowListener), Java provides "adapter" classes (e.g., MouseAdapter,
WindowAdapter).
○ Adapter classes provide empty default implementations for all methods in the
corresponding listener interface.
○ You can extend an adapter class and override only the methods you are
interested in, avoiding the need to implement all methods of the interface.
● Example Code (Using WindowAdapter to handle closing):
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WindowAdapterExample extends JFrame {
public WindowAdapterExample() {
setTitle("Window Adapter");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // Don't exit
automatically
setSize(300, 150);
// Add a WindowListener using a WindowAdapter
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// This method is called when the user clicks the close button
JOptionPane.showMessageDialog(null, "Window is closing!");
System.exit(0); // Manually exit the application
}
});
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new WindowAdapterExample());
}
}