`
guoyiqi
  • 浏览: 966564 次
社区版块
存档分类
最新评论

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 
分享到:
评论

相关推荐

    在MatlabGUI里面启动或者暂停Simulink模型-start_and_stop_gui.fig

    第二步:创建自己的GUI, 这个论坛里也有例子,我们使用以下文件。 start_and_stop_gui.fig start_and_stop_gui.m Figure12.jpg ...

    在MatlabGUI里面启动或者暂停Simulink模型-start_and_stop_gui.m

    第二步:创建自己的GUI, 这个论坛里也有例子,我们使用以下文件。 start_and_stop_gui.fig start_and_stop_gui.m Figure12.jpg ...

    在MatlabGUI里面启动或者暂停Simulink模型-start_and_stop.mdl

    第二步:创建自己的GUI, 这个论坛里也有例子,我们使用以下文件。 start_and_stop_gui.fig start_and_stop_gui.m Figure12.jpg ...

    在MatlabGUI里面启动或者暂停Simulink模型-sysstop_new.m

    第二步:创建自己的GUI, 这个论坛里也有例子,我们使用以下文件。 start_and_stop_gui.fig start_and_stop_gui.m Figure12.jpg ...

    完整的hook程序的例子

    一个完整的hook程序的例子 一、客户端 程序命名为Client。监视系统的运行,如发现系统中有“记事本”进程(notepad.exe)或者“计算器”进程(calc.exe),立即杀死(kill)该进程,并将该事件写入数据库;定期进行...

    PyQt5 在label显示的图片中绘制矩形的方法

    核心点是重写Label,使其实现相应的功能,看下面的一个小例子 from PyQt5.QtWidgets import QWidget, QApplication, QLabel from PyQt5.QtCore import QRect, Qt from PyQt5.QtGui import QImage, QPixmap, ...

    PyQt5 Python 桌面应用程序源码.zip

    2 PyQt5布局管理 绝对定位框布局 Boxlayout表格布局 QGridLayout评论的例子 PyQt5布局有两种方式,绝对定位和布局类 绝对定位 程序指定每个控件的位置和大小(以像素为单位) 3 PyQt5菜单和工具栏 主窗口状态栏菜单栏...

    JINI 核心技术

    7.6.6 另一个例子:使用GUI Bean 151 7.7 参考读物 153 第8章 深入理解:使用查找服务 154 8.1 查找概述 154 8.1.1 查找服务是Jini服务 154 8.1.2 服务如何使用查找 155 8.1.3 客户如何使用查找 155 8.2 发布服务...

    JINI核心技术

    7.6.6 另一个例子:使用GUI Bean 151 7.7 参考读物 153 第8章 深入理解:使用查找服务 154 8.1 查找概述 154 8.1.1 查找服务是Jini服务 154 8.1.2 服务如何使用查找 155 8.1.3 客户如何使用查找 155 8.2 发布服务...

    java图书馆swing源码-Oea.svg:SVGGUI和图形库遵循JavaSwing构建。Oea框架提供了三个Javascript库,1-

    上面的例子是一个用 Oea.svg 构建的工作计算器。 更多示例请点击 Java.js 该包包含许多实现 Draw2D.svg 和 Swing.svg 所需的核心 Java 类。 已经移植了两个 Java 包,AWT 和 Util。 AWT 类包括 Event 和 Geom 命名...

    测试 Matlab Listeners 和 Event Notifiers:这是一个指示 Matlab 侦听器模型计算成本的示例-matlab开发

    Matlab 监听器框架是实现不同 GUI 元素之间消息传递的便捷方式。 这个例子显示了背后机器所涉及的计算成本。 尝试几个具有不同听众数量的示例,您将看到响应中的延迟。 如果您计划基于 Matlab 构建复杂的应用程序,...

    python3+PyQt5重新实现QT事件处理程序

    本文是对《Python Qt GUI快速编程》的第10章的例子events用Python3+PyQt5进行改写,涉及到重新实现QWidget的事件处理程序。本例子涉及到上下文菜单,鼠标事件,键盘事件,可作为重新实现事件处理程序的参考。 注:在...

    node-x11:X11 node.js网络协议客户端

    例子 核心请求用法: var x11 = require ( 'x11' ) ; var Exposure = x11 . eventMask . Exposure ; var PointerMotion = x11 . eventMask . PointerMotion ; x11 . createClient ( function ( err , display ) { ...

    Qt Creator 的安装和hello world 程序+其他程序的编写--不是一般的好

    4.这里我们选择Qt4 Gui Application。 5.下面输入工程名和要保存到的文件夹路径。我们这里的工程名为helloworld。 6.这时软件自动添加基本的头文件,因为这个程序我们不需要其他的功能,所以 直接点击Next。 7.我们...

    JAVA核心技术

    JSP(TagLib)——>ActionForm——>Action ——> Event——>EJBAction——>EJB ——>DAO——>Database JSP(TagLib) (forward) <——Action <——EventResponse<—— ??Turbine:??主要应用方面:WEB层。??主要应用...

    Java开发技术大全 电子版

    14.12.2监听ListSelectionEvent事件556 14.12.3按钮响应事件556 14.12.4对话框的显示557 14.12.5返回用户选择的字体557 14.12.6如何使用字体选择对话框557 14.13GUI程序设计实例4——记事本558 14.13.1增加弹...

    如何编写批处理文件批处理文件批处理文件

    执行的应用程序是 32-位 GUI 应用程序时,CMD.EXE 不等应用程序终止就返回命令提示。如果在命令脚本内执行,该新行为则不会发生。 8.choice 命令 choice 使用此命令可以让用户输入一个字符,从而运行不同的命令。...

    QT 绘图函数

    分类: C++ GUI Programming with Qt 4 2007-05-29 21:52 8228人阅读 评论(3) 收藏 举报 要在绘图设备(paint device,一般是一个控件)上开始绘制,我们只要创建一个QPainter,把绘图设备指针传给QPainter对象。...

    javaSE代码实例

    3.7.4 令人困扰的例子 37 3.8 赋值运算 37 3.8.1 普通赋值运算 37 3.8.2 运算赋值运算 38 3.9 括号及运算符间的优先级关系 38 3.10 常用数学工具包——java.lang.Math类 39 3.10.1 数学常量 39 3.10.2...

Global site tag (gtag.js) - Google Analytics