📄 applet1.java
字号:
/**
* 课程设计示例程序,applet 数据处理类
* 本程序有两个类,雇员类 Employee,主类 Applet1
* 雇员类 Employee 定义了雇员一些属性,定义了一个构造函数,
* 定义了方法 toString,用来组织雇员有关信息成一个字符串。
* 主类 Applet1,用JBuilder 5的可视化设计手段产生了以下组件:
* Label label1 提示标签,"输入新增雇员信息:"
* Label label2 提示标签,"姓名:"
* TextField textField1 文本输入框,输入姓名用,8列宽
* Label label3 提示标签,"性别:"
* TextField textField2 文本输入框,输入性别用,2列宽
* Label label4 提示标签,"工资:"
* TextField textField3 文本输入框,输入工资用,6列宽
* Button button1 按钮,"确定新增雇员"
* Label label5 提示标签,"查找一位已有雇员,编号为:"
* TextField textField4 文本输入框,输入已有雇员的编号,4列宽
* Button button2 按钮,"查找雇员信息"
* 主类 Applet1,定义了雇员数组,每一个数组元素都是雇员类 Employee 对象
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class Applet1 extends Applet {
boolean isStandalone = false;
int num; // 雇员数组大小,从Html传递此数据
Employee[] emp; // 雇员数组,数组大小为 num
int empIndex = 0; // 当前雇员数组下标
boolean bSearch = false;// 是否查询雇员信息
int iSearch; // 要查询雇员信息的编号,即雇员数组下标
Label label1 = new Label();
Label label2 = new Label();
TextField textField1 = new TextField();
Label label3 = new Label();
Label label4 = new Label();
TextField textField2 = new TextField();
Label label5 = new Label();
TextField textField3 = new TextField();
TextField textField4 = new TextField();
Button button1 = new Button();
Button button2 = new Button();
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public Applet1() {
}
/**Initialize the applet*/
public void init() {
try {
num = Integer.parseInt(this.getParameter("number", "100"));
}
catch(Exception e) {
e.printStackTrace();
}
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
emp = new Employee[num]; // 创建雇员数组空间,数组大小为 num
}
/**Component initialization*/
private void jbInit() throws Exception {
label1.setText("输入新增雇员信息:");
label2.setText("姓名:");
textField1.setColumns(8);
label3.setText("性别:");
label4.setText("工资:");
textField2.setColumns(2);
label5.setText("查找一位已有雇员,编号为:");
textField3.setColumns(6);
textField4.setColumns(4);
button1.setLabel("确定新增雇员");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
button2.setLabel("查找雇员信息");
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button2_actionPerformed(e);
}
});
this.add(label1, null);
this.add(label2, null);
this.add(textField1, null);
this.add(label3, null);
this.add(textField2, null);
this.add(label4, null);
this.add(textField3, null);
this.add(button1, null);
this.add(label5, null);
this.add(textField4, null);
this.add(button2, null);
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
String[][] pinfo =
{
{"number", "int", "雇员人数"},
};
return pinfo;
}
// 显示当前新增雇员信息和按编号查询雇员信息
public void paint(Graphics g){
if(empIndex >= 0) {
g.drawString("新增的雇员,编号:" + (empIndex-1) + emp[empIndex-1].toString(), 20, 100);
}
if(bSearch) {
g.drawString("要查询的雇员," + emp[iSearch].toString(), 20, 130);
bSearch = false;
}
}
// 按钮button1事件,"确定新增雇员"
void button1_actionPerformed(ActionEvent e) {
String name = "";
String gender = "";
double s;
name = textField1.getText(); // 姓名
gender = textField2.getText(); // 性别
s = Double.parseDouble(textField3.getText()); // 工资
emp[empIndex] = new Employee(name, gender, s); // 构造一个雇员
empIndex++; // 当前雇员数组下标增加一,准备下一次构造一个雇员用
repaint();
}
// 按钮button2事件,"查找雇员信息",按编号即数组下标显示雇员信息
void button2_actionPerformed(ActionEvent e) {
bSearch = true; // 要查询
iSearch = Integer.parseInt(textField4.getText()); // 读取编号,第一个编号为0
repaint();
}
} // class Applet1 结束
class Employee { // 雇员类
String empName; // 雇员姓名
String empGender; // 雇员性别
double empSalary; // 雇员工资
Employee(String name, String gender, double sal) { // 构造函数
empName = name;
empGender = gender;
empSalary = sal;
}
public String toString() {
String s;
s = " 姓名:" + empName +
";性别:" + empGender +
";工资:" + empSalary;
return s;
}
} // class Employee 结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -