GUI & Event例子

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例子,布布扣,bubuko.com

时间: 2024-12-29 16:26:14

GUI & Event例子的相关文章

Python线程Event例子 (Boss要求等待员工生产杯子例子)

需求:Boss需要一个员工生产100个杯子,Boss一直等待员工生产完成之后方可 import threading import logging import time logging.basicConfig(level=logging.INFO) def worker(event:threading.Event, count=10): logging.info("I'm working for U.") cups = [] while True: logging.info('make

Android中GUI系统的Event路由机制

前两天在论坛上看到有人发了一个帖子,询问一个Android GUI Event处理的问题:有一个LinearLayout,里面有很多的child view,他问如何监听这个LinearLayout的Click事件?他的做法是: setClickable(true); setOnClickListener(listener); 最后他发现listener中的回调函数根本不会被调用. 事实上,在Android的GUI系统的中,硬件触发的Event(KeyEvent. TouchEvent. Trac

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

http://web.jobbole.com/82503/ Javascript中的事件经常被认为如谜一般不可解.Javascript是一个事件驱动的语言,在这样的前提下前面的看法是很奇怪,但是说到它们的复杂本质和调试难度时,这样的看法又是很正常的.为此,我创建了可视化事件(Visual Event)来查看DOM节点上绑定的事件. 简介 Visual Event是一个开源 Javascript 书签,能提供绑定在DOM元素上的事件调试信息.Visual Event能显示如下信息: 1.哪一个元素

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

找这工具找了好久,统一找着了,开发人员不可多得的好东东,收藏做一下分享. 用Chrome插件Visual Event查看Dom绑定的事件 Visual Event简介 Visual Event是一个开源 Javascript 书签,能提供绑定在DOM元素上的事件调试信息.Visual Event能显示如下信息: 1.哪一个元素有事件绑定 2.某元素上绑定的事件类型 3.事件触发后运行的代码段 4.定义绑定函数的源文件和行号(仅限于WebKit和Opera浏览器) 除了对调试你自己的代码大有用途,

C#学习日记24----事件(event)

事件为类和类的实例提供了向外界发送通知的能力,实现了对象与对象之间的通信,如果定义了一个事件成员,表示该类型具有 1.能够在事件中注册方法 (+=操作符实现). 2.能够在事件中注销方法(-=操作符实现). 3.当事件被触发时注册的方法会被通知(事件内部维护了一个注册方法列表).委托(Delegate)是事件(event)的载体,要定义事件就的要有委托.  有关委托的内容请点击 委托(De... www.mafengwo.cn/event/event.php?iid=4971258www.maf

doom3的UI系统

doom3的UI系统是纯数据驱动的,例如 windowDef TextTitle2 { rect 20,341,600,55 visible 1 text "#str_00073" forecolor 0.6,1,1,0 textscale 0.8 font "fonts/micro" textalign 1 notime 1 onTime 0 { transition "forecolor" "1 1 1 0" "

Unity自定义mesh以及编译器

Star 自定义编辑器简易教程 an introduction to custom editors 原文地址 http://catlikecoding.com/unity/tutorials/star/ http://blog.csdn.net/lilanfei/article/details/7680802 简介 Introduction 这个教程将让你学会如何创建一个星型控件以及如何制作这个控件的自定义编辑器.你将学会: 动态的建立Mesh. 使用一个嵌套类. 建立一个自定义编辑器. 使用S

Object-Oriented Analysis and Design Using UML 翻译与学习 (目录)

Object-Oriented Analysis and Design Using UML 面向对象分析与使用UML设计 最近找了本书<Object-Oriented Analysis and Design Using UML>,这个书是Oracle培训里面的,同时也是获取scjd和scja可选的课程. 所以,我准备一边翻译,一边学习.嗯,只翻译重点. 转载请注明出处!!! Copyright 2010 Sun Microsystems, Inc., 4150 Network Circle,

15_游戏编程模式EventQueue

#### 两个例子 1.GUI event loop ``` while (running) { // 从事件队列里获取一个事件 Event event = getNextEvent(); // Handle event... } ``` 2.Central event bus 不同系统公用的通信中心 #### 有问题的code ``` class Audio { public: static void playSound(SoundId id, int volume); }; class Au