Exercise Overview

How to Use These Exercises

These exercises will help you master Java Swing GUI programming. Start with simple windows and progress to complete applications with multiple components and event handling.

Tips for Success:

  • Run each program to see the GUI in action
  • Experiment with different layouts and component arrangements
  • Test event handling by clicking buttons and entering text
  • Read error messages carefully - they guide you to solutions
  • Refer back to the module content for syntax help
3 Easy Exercises
3 Medium Exercises
2 Hard Exercises

Easy Exercises - Start Here!

4.1

Create Your First Window

Easy

Create a basic JFrame window with a title and size.

Requirements:

  • Create a class that extends JFrame
  • Set the window title to "My First GUI"
  • Set the window size to 400x300 pixels
  • Center the window on screen
  • Set close operation to EXIT_ON_CLOSE
  • Make the window visible

Hints:

  • Use setTitle() to set window title
  • Use setSize(400, 300) to set dimensions
  • Use setLocationRelativeTo(null) to center
  • Don't forget to call setVisible(true)

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_4.1.zip. Max 5MB.
4.2

Hello World Label

Easy

Add a label displaying "Hello, World!" to your window.

Requirements:

  • Create a JLabel with text "Hello, World!"
  • Add the label to the frame
  • Use FlowLayout as the layout manager
  • Set appropriate window size (300x150)

Hints:

  • Create label: JLabel label = new JLabel("Hello, World!");
  • Add to frame: add(label);
  • Set layout: setLayout(new FlowLayout());

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_4.2.zip. Max 5MB.
4.3

Simple Button Click

Easy

Create a button that shows a message when clicked.

Requirements:

  • Create a JButton with text "Click Me!"
  • Add an ActionListener to the button
  • When clicked, show a JOptionPane message dialog
  • Message should say "Button was clicked!"

Hints:

  • Use lambda: button.addActionListener(e -> { ... });
  • Show message: JOptionPane.showMessageDialog(null, "Message");
  • Don't forget to add the button to the frame

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_4.3.zip. Max 5MB.

Medium Exercises - Build Your Skills!

4.4

Name Greeting App

Medium

Create an app where the user enters their name and clicks a button to see a greeting.

Requirements:

  • Create a JLabel with text "Enter your name:"
  • Create a JTextField for name input
  • Create a JButton with text "Greet Me"
  • Use GridLayout(3, 1) for layout
  • When button is clicked, show greeting dialog
  • Greeting should say "Hello, [Name]!"

Hints:

  • Get text field value: textField.getText()
  • Use string concatenation for greeting
  • Handle empty name input

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_4.4.zip. Max 5MB.
4.5

Simple Counter

Medium

Build a counter app with increment and decrement buttons.

Requirements:

  • Create a JLabel showing "Count: 0"
  • Create "Increment" and "Decrement" buttons
  • Use BorderLayout
  • Label in CENTER, buttons in SOUTH panel
  • Update label text when buttons are clicked
  • Counter starts at 0

Hints:

  • Use an instance variable to track count
  • Update label: label.setText("Count: " + count);
  • Create JPanel for buttons at SOUTH

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_4.5.zip. Max 5MB.
4.6

Temperature Converter

Medium

Create a Celsius to Fahrenheit temperature converter.

Requirements:

  • JLabel "Celsius:" and JTextField for input
  • JButton "Convert to Fahrenheit"
  • JLabel to display result
  • Formula: F = (C × 9/5) + 32
  • Handle invalid input with error dialog
  • Use GridLayout(4, 1)

Hints:

  • Parse input: Double.parseDouble(textField.getText())
  • Use try-catch for NumberFormatException
  • Format result: String.format("%.2f", result)

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_4.6.zip. Max 5MB.

Hard Exercises - Master the Concepts!

4.7

Registration Form

Hard

Create a complete user registration form.

Requirements:

  • Fields: Name, Email, Password, Confirm Password
  • Use JTextField for name and email
  • Use JPasswordField for passwords
  • Submit and Clear buttons
  • Validate: all fields filled, emails match format, passwords match
  • Show success or error messages
  • Use GridLayout for form layout

Hints:

  • Email regex: .*@.*\\..*
  • Get password: new String(passwordField.getPassword())
  • Clear all fields on Clear button click
  • Check for empty fields: text.isEmpty()

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_4.7.zip. Max 5MB.
4.8

To-Do List Application

Hard

Build a functional to-do list with add and remove features.

Requirements:

  • JTextField for task input
  • JButton "Add Task"
  • JTextArea to display tasks (use JScrollPane)
  • JButton "Clear All"
  • Add tasks to text area when Add is clicked
  • Clear all tasks when Clear All is clicked
  • Use BorderLayout with panels

Hints:

  • Append to text area: textArea.append(task + "\n")
  • Clear text area: textArea.setText("")
  • Wrap text area in scroll pane
  • Set text area rows/columns for size

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_4.8.zip. Max 5MB.

🎉 Module 4 Exercises Complete!

You've practiced Java Swing GUI programming. You can now create desktop applications with buttons, text fields, and event handling!