0% found this document useful (0 votes)
1 views

Lab4

This document outlines a lab exercise for 2nd Year Computer Science Engineers at the University of Mostaganem, focusing on Object-Oriented Programming using Java Swing. It includes step-by-step instructions for creating interactive GUIs, handling events, and improving user interfaces through various exercises, culminating in a simple calculator application. The lab aims to teach students the fundamentals of GUI design and event handling in Java.

Uploaded by

abdelkarim fatis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab4

This document outlines a lab exercise for 2nd Year Computer Science Engineers at the University of Mostaganem, focusing on Object-Oriented Programming using Java Swing. It includes step-by-step instructions for creating interactive GUIs, handling events, and improving user interfaces through various exercises, culminating in a simple calculator application. The lab aims to teach students the fundamentals of GUI design and event handling in Java.

Uploaded by

abdelkarim fatis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

University of Mostaganem

2nd Year Computer Science Engineers


Subject : Object-Oriented Programming (OOP)

Lab4

Objective: This lab is designed to introduce to Java Swing by guiding you through the creation
of interactive graphical user interfaces (GUIs). You will learn to build windows, add
components, handle user interactions, and manage layouts, while creating functional
applications like a calculator. By the end, you will understand the basics of Swing and be able
to design dynamic, user-friendly interfaces.

Exercise 1: Creating a Simple Window


1. Open NetBeans and create a new Java project (type "Java Application").
2. Create a class MaFenetre that extends JFrame.
3. In the class constructor:
o Set the window title using setTitle("My First Window").
o Set the window size using setSize(400, 300).
o Set the close operation
using setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).
4. In the main method, create an instance of MaFenetre and make it visible
using setVisible(true).
Exercise 2: Adding Components
1. Reuse the MaFenetre class from Exercise 1.
2. Add a JLabel with the text "Hello, World!" and a JButton with the text "Click Me".
3. Use a JPanel to organize the components and add it to the window.
4. Display the window and verify that the components are visible.
Exercise 3: Event Handling with Color Change
1. Create the Main Window:
o Create a class MaFenetre that extends JFrame.

o Set the window title to "My Interactive Window".

o Set the window size to 400x200 pixels.

o Center the window on the screen.

o Ensure the window closes properly when the user clicks the close button.

Year : 2024/2025 OOP : TP4 ENSEIGNANT : M. ZEBOUDJ


2. Add Components:
o Add a JLabel with the initial text "Hello, Swing!". Center the text in the label.
o Add a JButton with the text "Click Me".
o Organize the components in a JPanel using a BorderLayout:
▪ Place the label in the center.
▪ Place the button at the bottom.
3. Handle Events:
o Add an event listener (ActionListener) to the button.
o When the user clicks the button:
▪ Change the label text to "You clicked!".
▪ Change the label's text color to red (Color(255, 0, 0)).
▪ Change the label's background color to yellow (Color(255, 255, 0)).
▪ Change the button's background color to blue (Color(0, 0, 255)).
▪ Change the button's text color to white (Color(255, 255, 255)).
Possible Improvements (Bonus):
• Color Chooser: Add a JColorChooser to allow the user to select a custom color.
Expected Output:
1. When the application starts:
o The label displays "Hello, Swing!".
o The button displays "Click Me".
2. After clicking the button:
o The label displays "You clicked!".
o The label text turns red.
o The label background turns yellow.
o The button turns blue with white text.
Exercise 4: Improved Exercise: Dynamic Layout Exploration
1. Create a New Class FenetreLayout:
o This class extends JFrame.
o The window size will be 500x400 pixels.
o The window title will be "Layout Exploration".
2. Add Buttons to Change the Layout:
o Add three buttons at the top of the window:

Year : 2024/2025 OOP : TP4 ENSEIGNANT : M. ZEBOUDJ


▪ BorderLayout: Changes the layout to BorderLayout.
▪ FlowLayout: Changes the layout to FlowLayout.
▪ GridLayout: Changes the layout to GridLayout.
3. Add Components to Illustrate Layouts:
o Add five buttons to the window to illustrate component organization:
▪ Button 1: "North" (for BorderLayout).
▪ Button 2: "South" (for BorderLayout).
▪ Button 3: "Center" (for BorderLayout).
▪ Button 4: "East" (for BorderLayout).
▪ Button 5: "West" (for BorderLayout).
4. Implement Logic to Change the Layout:
o Add event listeners to the layout change buttons.
o When the user clicks a button, the window's layout is updated, and the
components are reorganized accordingly.
Exercise 5: Creating a Complete User Interface

1. Create a new class CalculatriceSimple.

2. Add buttons for digits (0-9) and basic operations (+, -, *, /).

3. Add a text field (JTextField) to display the results.

4. Implement the logic to perform simple calculations when the user clicks the buttons.

Exercise 6: Improving the Interface

1. Reuse the calculator interface from Exercise 5.

2. Add colors, fonts, and borders to improve the appearance.

3. Use JPanel to organize components in a more aesthetic way.

Exercise 7: Error Handling

1. Reuse the calculator from Exercise 5.

2. Add error handling to prevent division by zero or invalid inputs.

3. Display an error message in the text field or via a dialog box (JOptionPane) in case of
an error.

Year : 2024/2025 OOP : TP4 ENSEIGNANT : M. ZEBOUDJ

You might also like