小应用程序Applet设计
一、 课题内容和要求
内容:设计和编写一个可以用鼠标操作的Applet小应用程序和相应的HTML页面,观察Applet的执行过程,测试程序鼠标用户交互操作的效果。
要求:要求学生能在学习和理解课堂学习内容中JAVA小应用程序的基础上,通过实验,培养学生将JAVA小应用程序相关知识点(包括JAVA Applet和低级事件处理模型)有机结合,设计基于WEB浏览器的小应用程序的能力。
二、设计思路分析
class App:一个Java Applet计算器的主类 public void init():完成初始化applet public boolean action():实现事件处理的方法 public void doOperator():运算及运算结果输出操作
public void doForeScreen():数字和小数点输出到文本框操作 public void doClear():清空操作
三、概要设计
public class App extends Applet {
TextField tfAnswer; }
Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
Button bPoint, bEqual, bPlus, bMinus, bClear, bMulti, bDivision; String OperatorCurrent, OperatorPre; String ForeScreen, BackScreen; boolean isFloat = false; public void init();
public boolean action(Event e, Object o); public void doOperator()
public void doForeScreen(String s); public void doClear();
事件处理:
运算及运算结果输出:
四、详细设计
Java代码:
import java.awt.*; import java.applet.*;
public class App extends Applet { TextField tfAnswer;// 定义变量 Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9; Button bPoint, bEqual, bPlus, bMinus, bClear, bMulti, bDivision; String OperatorCurrent, OperatorPre; String ForeScreen, BackScreen; boolean isFloat = false; public void init() {// 初始化 OperatorCurrent = new String(\ OperatorPre = new String(\
ForeScreen = new String(\保证输入字符串不为空防止计算错误 BackScreen = new String(\setBackground(Color.gray);
setLayout(null);// 设为空布局便于自定义组件位置 tfAnswer = new TextField();// 设置文本框样式 tfAnswer.setBounds(20, 20, 175, 40);
tfAnswer.setFont(new Font(BackScreen, Font.BOLD, 28)); add(tfAnswer);
tfAnswer.setText(ForeScreen);
bClear = new Button(\设置按钮样式 bClear.setBounds(20, 70, 40, 40); add(bClear);
bDivision = new Button(\
bDivision.setBackground(Color.green); bDivision.setBounds(65, 70, 40, 40); add(bDivision);
bMulti = new Button(\
bMulti.setBackground(Color.green); bMulti.setBounds(110, 70, 40, 40); add(bMulti);
bMinus = new Button(\
bMinus.setBackground(Color.green); bMinus.setBounds(155, 70, 40, 40); add(bMinus);
b7 = new Button(\
b7.setBounds(20, 115, 40, 40); b7.setBackground(Color.orange); add(b7);
b8 = new Button(\
b8.setBackground(Color.orange); b8.setBounds(65, 115, 40, 40); add(b8);
b9 = new Button(\
b9.setBackground(Color.orange); b9.setBounds(110, 115, 40, 40); add(b9);
bPlus = new Button(\
bPlus.setBackground(Color.green); bPlus.setBounds(155, 115, 40, 85);
}
add(bPlus);
b4 = new Button(\
b4.setBackground(Color.orange); b4.setBounds(20, 160, 40, 40); add(b4);
b5 = new Button(\
b5.setBackground(Color.orange); b5.setBounds(65, 160, 40, 40); add(b5);
b6 = new Button(\
b6.setBackground(Color.orange); b6.setBounds(110, 160, 40, 40); add(b6);
b1 = new Button(\
b1.setBackground(Color.orange); b1.setBounds(20, 205, 40, 40); add(b1);
b2 = new Button(\
b2.setBackground(Color.orange); b2.setBounds(65, 205, 40, 40); add(b2);
b3 = new Button(\
b3.setBackground(Color.orange); b3.setBounds(110, 205, 40, 40); add(b3);
bEqual = new Button(\
bEqual.setBackground(Color.green); bEqual.setBounds(155, 205, 40, 85); add(bEqual);
b0 = new Button(\
b0.setBackground(Color.orange); b0.setBounds(20, 250, 85, 40); add(b0);
bPoint = new Button(\
bPoint.setBackground(Color.orange); bPoint.setBounds(110, 250, 40, 40); add(bPoint);
public boolean action(Event e, Object o) {// 实现事件处理的方法
String s = new String(\ if ((e.target == b0) || (e.target == b1) || (e.target == b2) || (e.target == b3) || (e.target == b4) || (e.target == b5) || (e.target == b6) || (e.target == b7) || (e.target == b8) || (e.target == b9)) {// 获取数字按钮数据并在控制台和文本框输出 if (e.target != bPoint) {// 获取数字 s = (String) o; doForeScreen(s); } } if ((e.target == bPoint) && !ForeScreen.contains(\保证原数不含小数点的情况下获取小数点并在控制台和文本框输出
s = (String) o; if (ForeScreen.equals(\原来没有输入数据则用0. { doForeScreen(\ } else// 原来有输入数据则用. { doForeScreen(s); } } if (e.target == bClear) {// 清除按钮进行清除操作 doClear(); } if ((e.target == bMulti) || (e.target == bDivision) || (e.target == bPlus) || (e.target == bMinus) || (e.target == bEqual)) {// 加减乘除等于按钮获取运算操作符并进行运算操作
if (ForeScreen != \重新输入数字进行运算操作 OperatorCurrent = ((String) o); doOperator(); } else {// 没有重新输入数字则作为上一运算符--->保证不输入连续的同一运算符
OperatorPre = ((String) o); } } return true; }