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

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

相关推荐

    java GUI代码例子

    本资源“java GUI代码例子”是一个针对初学者的测试案例,旨在帮助他们理解和实践如何在Java中构建GUI应用。 GUI(Graphical User Interface)提供了一种直观的方式与计算机进行交互,通过按钮、文本框、菜单等组件...

    一个gui的简单例子,输入框输出框按钮

    GUI,全称图形用户界面(Graphical User ...总的来说,这个简单的GUI例子展示了基础的用户交互流程,是理解和学习GUI编程的一个良好起点。通过扩展和优化,我们可以构建出功能更丰富、用户界面更友好的应用程序。

    matlab gui q&a 1

    在这个例子中,首先创建了一个名为`myEdit`的`EDIT TEXT`控件,然后通过`set`函数将其内部的文本设置为“Hello World!”。 #### 四、实例分析 根据提供的代码片段,我们可以看到作者尝试在MATLAB GUI中实现以下...

    matalb gui设计实例

    本文将通过一个具体的例子来详细介绍如何在MATLAB中设计GUI,并实现图像处理相关的功能。 #### 二、项目背景与目标 本项目旨在利用MATLAB的GUIDE工具包设计并实现一个基本的图像处理GUI系统。该系统主要具备以下...

    35个GUI的例子,由简入繁.7z

    这个7z压缩包文件包含了35个GUI的例子,旨在帮助初学者逐步理解并掌握GUI设计的基本原理和实践技巧,从最简单的界面到复杂的布局,一步步深入。下面,我们将详细探讨这些GUI例子所涵盖的知识点。 1. **基础组件**:...

    matlabGUI读取Excel数据,matlabgui怎么导入excel数据,matlab

    在这个例子中,`handles.tableHandle`代表了GUI中的表格控件句柄,我们需要确保在GUI初始化时正确设置了这个句柄。`uigetfile`函数用于打开文件选择对话框,让用户选择要导入的Excel文件。 在运行GUI时,用户点击...

    Java GUI,我初学的时候感觉这些例子很好,所以就上传了,希望对初学者有用!

    通过实践这些小例子,初学者可以逐步掌握Java GUI编程的基础,并能够创建出功能丰富的交互式应用。在实际开发中,还可以结合JavaFX这样的现代图形库,提供更高级的视觉效果和动画支持。总之,Java GUI为开发者提供了...

    GUI.rar_java的readme例子

    7. **事件调度线程(Event Dispatch Thread, EDT)**:所有对Swing组件的修改和绘制都必须在EDT中进行,以确保界面的线程安全。 为了运行这个例子,首先需要将"实例85"解压,并使用Java编译器(javac)编译源代码,...

    windous下perl-gui编程模块 Win32-GUI-1.06-PPM-5.8

    在这个例子中,我们创建了一个主窗口,然后在窗口中添加了一个按钮。当用户点击按钮时,会触发`button_click`事件处理程序,打印出一条消息。 `Win32-GUI`模块还支持复杂的控件交互、多线程处理、自定义控件以及与...

    gui.rar_GUI绘图_gui 绘图_matlab GUI_绘图 matlab

    在这个例子中,`pushbutton1_Callback`是按钮的回调函数,当用户点击按钮时,MATLAB会绘制一个正弦函数的图形。`handles`结构体包含了GUI中所有控件的句柄,通过它可以访问并操作特定的控件,如`handles.axes1`代表...

    matlab gui 选项卡应用

    在这个例子中,`tabpanelcopydemo`很可能是初始化GUI并设置选项卡的函数。这个脚本会定义GUI的布局,创建选项卡,并可能包含用于处理用户交互的回调函数。 `tabpanelcopyfcn.m`是另一个函数文件,它可能包含了与...

    c#关于Event与委托的例子

    在C#编程中,事件(Event)和委托(Delegate)是两种非常重要的概念,它们是...它们是事件驱动编程的核心,广泛应用于GUI应用、多线程编程以及异步操作中。熟悉并熟练掌握这些概念对于任何C#开发者来说都是至关重要的。

    GUI.rar_C GUI_GUI

    一个简单的C语言使用GTK+创建窗口的例子: ```c #include int main(int argc, char *argv[]) { GtkWidget *window; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window...

    GUI源代码100例

    同时,通过对不同例子的比较,可以掌握如何根据需求选择合适的GUI设计模式和控件组合。 总之,"GUI源代码100例"是一份宝贵的教育资源,无论你是初学者还是经验丰富的开发者,都能从中获得宝贵的知识和灵感。通过...

    Python制作GUI图形界面源码

    通过这个例子,我们可以看到PySimpleGUI简化了创建GUI的过程,使得开发者可以专注于应用逻辑,而不是界面设计的细节。同时,结合pandas的强大功能,我们可以轻松地处理各种数据操作,如读取Excel、筛选数据、保存...

    matlabGUI简单教程-MatlabGUI简单教程.doc

    这个教程以一个简单的加法例子展示了MATLABGUI的基本使用方法,包括创建组件、设置属性、编写回调函数以及处理用户输入。通过掌握这些基础知识,你可以进一步开发更复杂的GUI应用,如数据分析、图像处理或者控制系统...

    Untitled_matlabGUI_Untitled_

    在这个例子中,`uigetfile`和`uiputfile`函数分别用于打开文件选择对话框和保存文件对话框,让用户选择要加载或保存的文件。 文件列表中的"Untitled.m"很可能是实现这些功能的MATLAB脚本或函数,它包含了GUI的定义...

    Java GUI编程示例

    4. 事件处理(Event Handling):通过监听器接口(Listener Interfaces)如ActionListener、MouseListener等,实现对用户操作的响应。 5. 视觉样式(Look and Feel):允许开发者改变应用程序的视觉风格,使其与操作...

    matlab gui 加法计算器

    在这个例子中,`gui_OpeningFcn`函数用于初始化GUI,而`add_button_Callback`函数是加法按钮的回调,当用户点击按钮时调用`jiafa`函数执行加法操作。注意,`handles`结构体存储了所有控件的句柄,使得我们可以在回调...

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

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

Global site tag (gtag.js) - Google Analytics