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

GUI & Event例子的更多相关文章

  1. Babylon.GUI官方文档翻译

    Babylon.GUI是一个基于Babylon.js的WebGL库,可以用来在WebGL3D场景中生成交互性UI与动态纹理.相比于html ui,Babylon.GUI的功能较为简化,但使用起来也更加 ...

  2. 处理事件的方式:两种类的覆盖处理(自己管理,覆盖专用事件函数;自己统一管理,覆盖QWidget::Event通用函数),一种对象的处理(父控件统一管理,即安装过滤器),两种全局处理(QCoreApplication安装过滤器;覆盖notify方法)

    虽然只有一句话,但却是我自己的心得. 特别注意,bool QCoreApplication::notify(QObject *receiver, QEvent *event) 明确指明了要发送的对象, ...

  3. Chrome插件Visual Event查看Dom元素绑定事件的利器

    找这工具找了好久,统一找着了,开发人员不可多得的好东东,收藏做一下分享. 用Chrome插件Visual Event查看Dom绑定的事件 Visual Event简介 Visual Event是一个开 ...

  4. Python的GUI用法1

    代码: #python GUI的例子1 import tkinter as tk class Window: def __init__(self,master): frame = tk.Frame(m ...

  5. Visual Event :快速查看 DOM 上绑定的 JS 事件

    http://web.jobbole.com/82503/ Javascript中的事件经常被认为如谜一般不可解.Javascript是一个事件驱动的语言,在这样的前提下前面的看法是很奇怪,但是说到它 ...

  6. Chapter 1. Hello, Perl/Tk

    Chapter 1. Hello, Perl/Tk 内容: Perl/Tk Concepts Some Perl/Tk History Getting Started with Perl/Tk Hel ...

  7. unity 面试题(答案)

    一.什么是渲染管道?是指在显示器上为了显示出图像而经过的一系列必要操作.渲染管道中的很多步骤,都要将几何物体从一个坐标系中变换到另一个坐标系中去.主要步骤有:本地坐标->视图坐标->背面裁 ...

  8. 史上最全的Unity面试题(持续更新总结。。。。。。) 包含答案的Unity面试题

    这个是我刚刚整理出的Unity面试题,为了帮助大家面试,同时帮助大家更好地复习Unity知识点,如果大家发现有什么错误,(包括错别字和知识点),或者发现哪里描述的不清晰,请在下面留言,我会重新更新,希 ...

  9. 15_游戏编程模式EventQueue

    #### 两个例子 .GUI event loop ``` while (running) { // 从事件队列里获取一个事件 Event event = getNextEvent(); // Han ...

随机推荐

  1. 如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。

    如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断.

  2. jQuery实现可编辑表格

    在很多的网页中,这个可编辑表格在许多地方都是非常有用,配合上AJAX技术能够实现很好的用户体验,下面我 们就jQuery来说明一下可编辑表格的实现步骤 首先是HTML代码,非常简单 <!DOCT ...

  3. ThinkPhp学习03

    原文:ThinkPhp学习03 一.ThinkPHP 3 的输出      (重点) a.通过 echo 等PHP原生的输出方式在页面中输出 b.通过display方法输出   想分配变量可以使用as ...

  4. NEU 1173: 这是物理学的奇迹!! 分解质数

    1173: 这是物理学的奇迹!! 题目描述 goagain在做物理电学实验时需要一个2Ω的电阻,但是他发现他的实验台上只剩下了3Ω,4Ω,5Ω,6Ω的电阻若干,于是goagain把两个4Ω的电阻并联起 ...

  5. 利用手工编码的方式对srtus2进行输入验证

    对action方法进行校验有两种方法一种是: 1手工编码书写 2一种是用xml 输入校验的流程: 1类型转化器对请求参数执行类型转化,并把转换后的值赋给action属性. 2.如果执行类型转化的过程中 ...

  6. H264 Decoder

    http://www.cnblogs.com/mcodec/category/213433.html

  7. linux通过使用mail发送电子邮件

    通过外部方法发送的电子邮件 bin/mail默认为本地sendmail发送电子邮件,求本地的机器必须安装和启动Sendmail服务.配置很麻烦,并且会带来不必要的 资源占用.而通过改动配置文件能够使用 ...

  8. vijos P1352 最大获利(最小割)

    请不要随便指点别人该怎么做.每一个人的人生都应该自己掌握.你给不了别人一切.你也不懂别人的忧伤. 微笑不代表快乐.哭泣不一定悲伤 不努力怎么让关心你的人幸福.不努力怎么让看不起你的人绝望. 我用生命在 ...

  9. cocostudio——js 3 final控件事件

    近期试用了下cocos ide,然后引擎用的cocos2dx js 3 final,须要build runtime一下,以下是cocos studio相关的一些事件: 加入事件侦听: // butto ...

  10. Everything You Wanted to Know About Machine Learning

    Everything You Wanted to Know About Machine Learning 翻译了理解机器学习的10个重要的观点,增加了自己的理解.这些原则在大部分情况下或许是这样,可是 ...