Loading...

Go Back

Next page
Go Back Course Outline

Java Full Course


Graphical User Interface (GUI)

Java GUI Programming

Introduction to AWT and Swing

AWT (Abstract Window Toolkit) and Swing are Java's libraries for creating graphical user interfaces.

Note: Swing is built on top of AWT and provides more advanced components.

Basic Swing Application

import javax.swing.*;

public class SimpleGUI {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Simple Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
            
            JLabel label = new JLabel("Hello, Swing!");
            frame.add(label);
            
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}
    
    

GUI Components

Common components: JFrame, JPanel, JButton, JLabel, JTextField

Component Example

import javax.swing.*;
import java.awt.*;

public class ComponentDemo extends JFrame {
    public ComponentDemo() {
        setTitle("Component Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        
        JPanel panel = new JPanel();
        panel.add(new JLabel("Name:"));
        panel.add(new JTextField(15));
        panel.add(new JButton("Submit"));
        
        add(panel);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(ComponentDemo::new);
    }
}
    
    

Event Handling

Responding to user interactions with event listeners.

ActionListener Example

import javax.swing.*;
import java.awt.event.*;

public class ButtonDemo extends JFrame {
    private JButton button;
    private JLabel label;
    private int count = 0;
    
    public ButtonDemo() {
        setTitle("Button Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 200);
        
        button = new JButton("Click Me!");
        label = new JLabel("Count: 0");
        
        button.addActionListener(e -> {
            count++;
            label.setText("Count: " + count);
        });
        
        setLayout(new FlowLayout());
        add(button);
        add(label);
        
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(ButtonDemo::new);
    }
}
    
    

Layout Managers

Arranging components in containers.

Layout Example

import javax.swing.*;
import java.awt.*;

public class LayoutDemo extends JFrame {
    public LayoutDemo() {
        setTitle("Layout Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        
        // Create panels with different layouts
        JPanel mainPanel = new JPanel(new BorderLayout());
        
        JPanel northPanel = new JPanel();
        northPanel.add(new JButton("North"));
        mainPanel.add(northPanel, BorderLayout.NORTH);
        
        JPanel centerPanel = new JPanel(new GridLayout(2, 2));
        centerPanel.add(new JButton("Button 1"));
        centerPanel.add(new JButton("Button 2"));
        centerPanel.add(new JButton("Button 3"));
        centerPanel.add(new JButton("Button 4"));
        mainPanel.add(centerPanel, BorderLayout.CENTER);
        
        add(mainPanel);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(LayoutDemo::new);
    }
}
    
    

Creating Forms

Building a registration form with validation.

Registration Form Example

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RegistrationForm extends JFrame {
    private JTextField nameField, emailField;
    private JPasswordField passwordField;
    private JButton submitButton;
    
    public RegistrationForm() {
        setTitle("Registration Form");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLayout(new GridLayout(4, 2, 10, 10));
        
        add(new JLabel("Name:"));
        nameField = new JTextField();
        add(nameField);
        
        add(new JLabel("Email:"));
        emailField = new JTextField();
        add(emailField);
        
        add(new JLabel("Password:"));
        passwordField = new JPasswordField();
        add(passwordField);
        
        add(new JLabel());
        submitButton = new JButton("Register");
        submitButton.addActionListener(this::handleSubmit);
        add(submitButton);
        
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
private void handleSubmit(ActionEvent e) { String name = nameField.getText().trim(); String email = emailField.getText().trim(); String password = new String(passwordField.getPassword()); if (name.isEmpty() || email.isEmpty() || password.isEmpty()) { JOptionPane.showMessageDialog(this, "All fields are required!", "Error", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog(this, "Registration successful!\nName: " + name, "Success", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] args) { SwingUtilities.invokeLater(RegistrationForm::new); } }
Go Back

Next page