Java Swing Basics
Exercise 1:
1. Define GUI and explain its purpose in software applications.
2. Differentiate between AWT and Swing in Java.
3. What is the role of a JFrame in a Java Swing application?
4. Explain the function of each of the following components and provide at least 2 functions
for each:
a) JButton
b) JLabel
c) JTextField
5. Describe the difference between a container and a component in Swing.
6. What is an event in Swing? Give two examples.
7. Briefly explain what an ActionListener does in a Swing application.
8. What is the purpose of a layout manager?
9. Compare FlowLayout, BorderLayout, and GridLayout where would each be most
suitable?
10.Why is it important to call setVisible(true) in your Swing application?
Exercise 2:
Use Java Swing to create a simple calculator that performs basic arithmetic operations, addition,
subtraction, multiplication, and division using GridLayout. Your program should include:
● A main window (JFrame) titled “Simple Calculator”
● Two input fields for numbers (JTextField)
● Four buttons: +, −, ×, ÷
● A label to display the result
Instructions:
Solutions should be written on a piece of paper and submitted to the class rep who will submit to
the lecture by or on the 17th of October 2025.