Student No.: _______________ Name: ________________________________________
1
TK2934 Object-Oriented Programming
Project : GUI & Event
In this lab you will be using the following Java Swing & awt classes:
• container – JFrame, JPanel
• components – JButton, JLabel, JTextField, JRadioButton, JComboBox
• layout managers – FlowLayout, GridLayout, BorderLayout
• component property – Color, Font
• event handling – ActionListener, ItemListener
Stage 1
Purpose: To change the attributes of the components
Stage output: A GUI with nice font and color.
Task Remarks Evaluation/Answer
1. Get a copy of Project.java from your instructor. Make a copy of Project.java and name it as
ProjStage1.java
2. Compile and run the program.
3. Change the font and color of the
labels.
Use methods:
• setFont(new Font(....));
• setForeground(Color);
and class :
• Font(String name, int style, int
size)
Check:
The output should look similar to the figure below.
Figure 1
Time:
Student No.: _______________ Name: ________________________________________
2
Stage 2
Purpose: To add JRadioButtons to the right panel and handle the events.
Stage output: An application with random addition questions.
Task Remarks Evaluation
1. Make a copy of ProjStage1.java
and name it ProjStage2.java
2. Create a panel named rightP
and add 2 radiobuttons ("Add" &
"Subtract")
3. Add the panel to the EAST (or
any other area) of pane.
4. Create a panel named mathP
and add to CENTER of pane
(comment the statement to add
mainP panel).
5. Generate 2 random numbers
between 0 to 9
6. Display the title and the addition
question as in Figure 2. Use nice
and interesting fonts and colors.
Use the following expression:
(int) (Math.random * max) + 1;
where max = 9
Check:
The output should look similar to the figure below.
Figure 2
Time:
Student No.: _______________ Name: ________________________________________
3
7. Handle the event such that when
the user input the correct answer
(and pressed enter), your
program should display
responds as in Figure 3.
Check:
The output should look similar to the figure below.
Figure 3
8. Test with incorrect answers.
Your program should respond
accordingly and request the user
to try again. (Figure 4)
Check:
The output should look similar to the figure below.
Figure 4
Student No.: _______________ Name: ________________________________________
4
Stage 3
Purpose: To handle event for subtraction questions.
Stage output: An application with random addition and subtraction questions.
Task Remarks Evaluation
1. Make a copy of ProjStage2.java
and name it ProjStage3.java
2. Handle the event such that when
the user select "Subtract"
radio button, a randomly
generated subtraction question
will be displayed
3. Handle the event for the
subtraction question. (Figure 5)
4. Test your program by clicking on
the "Add" radio button. Your
program should display a new
random addition question with
the event handling described in
Stage 2.
5. Test your program by clicking on
the "Subtract" radio button.
Your program should display a
new random subtraction
question with the event handling
subscribe in task (2) above.
Important:
Your subtraction question should
always have a positive answer.
Stage 4
Purpose: To handle panel switching.
Stage output: An application that can switch between two panels.
Task Remarks Evaluation
1. Make a copy of ProjStage3.java
and name it ProjStage4.java
2. Add a new radio button labeled
"Word Game" to the right panel.
3. Create a panel named wordP.
Time:
Time:
Student No.: _______________ Name: ________________________________________
5
4. Add a label "GUESS THE WORD"
to the panel
5. Handle the event such that :
a) when the user clicked "Word
Game", wordP panel will be
displayed at the CENTER of
pane
b) when the user clicked "Add",
mathP panel with random
addition question will be
displayed.
c) when the user clicked
"Subtract", mathP panel
with random subtraction
question will be displayed.
6. Test your program
Use the following statements to
switch panels:
pane.remove(currentP);
pane.add(wordP);
pane.revalidate();
currentP = wordP;
pane.repaint();
where currentP is initialized to
panel mathP.
7. Update your program such that
when the program started, the
mainP panel is displayed.
Hint:
initialize currentP to mainP
and add mainP to CENTER.
Stage 5
Purpose: To create an interface for guess a word game
Stage output: A GUI for the word game.
Task Remarks Evaluation
1. Make a copy of ProjStage4.java
and name it ProjStage5.java
2. Initialize a secretWord
3. In the wordP, add textfields
based on the number of
characters in the secretWord.
Hint:
use array of textfields for easy
manipulation later
Time:
Student No.: _______________ Name: ________________________________________
6
4. Set the textfield as non editable
5. Below the textfield, add buttons
with labels of character "A" to
"Z".
Hint:
use array of buttons for easy
manipulation later
Check:
The output should look similar to the figure below.
Figure 5
Stage 6
Purpose: To handle event for guess a word game
Stage output: An application with the word game.
Task Remarks Evaluation
1. Make a copy of ProjStage5.java
and name it ProjStage6.java
2. Handle the event as follows. When
the user clicked a button
a. Check if the character is in the
secretWord
Hint:
use array of textfields for easy
manipulation later
Time:
Student No.: _______________ Name: ________________________________________
7
b. if yes, display the character in
the textfield
c. disable the button
d. repeat the above steps until all
character are displayed in the
textfield
e. display massage such as
"Congratulations"
Refer to Figure 6
Check:
The output should look similar to the figure below.
Figure 6
Student No.: _______________ Name: ________________________________________
8
Stage 7
Purpose: To have a list of words to guess
Stage output: An application with a more flexible word game
Task Remarks Evaluation
1. Make a copy of ProjStage6.java
and name it ProjStage7.java
2. Create a JComboBox with 3 items
("word1", "word2", "word3")
3. Replace the radiobutton ("Word
game") with the comboBox
4. Initialize (3) secret word lists. Use
array for easy manipulation later
5. Modify your program. Test for
program correctness for all the
secret words
6. Test your program, it should work
correctly when user select a new
word.
Hint:
Test the variable initialization,
resets all textfields and activate
all buttons
Check:
The output should look similar to the figure below.
Figure 7
Time:
Answer following Question:
Project : Project_Worksheet.pdf
Write programs in stages as described in the worksheet.
Bonus :
Create an additional page of math or word game.
Initial file : Project.java
import javax.swing.*;
import java.awt.*;
class Project extends JFrame {
Container pane;
JPanel mainP;
public Project() {
pane = getContentPane();
pane.setBackground(Color.white);
pane.setLayout(new BorderLayout());
mainP = new JPanel();
mainP.setBackground(Color.white);
mainP.setLayout(new GridLayout(2, 1));
JLabel welcome = new JLabel("W E L C O M E", JLabel.CENTER);
mainP.add(welcome);
JLabel title = new JLabel("Java Math & Word Games", JLabel.CENTER);
mainP.add(title);
pane.add(mainP, BorderLayout.CENTER);
}
public static void main(String [] args) {
Project frame = new Project();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Java Math & Word Games");
frame.setSize(700, 700);
frame.setVisible(true);
}
}
Upload Required File to completed the Task
Individual Task