// Java 语言函数计算 hsjs.java xss 2003.8.18
import java.awt.*;
//导入java语言awt抽象窗口工具包的所有类
import java.awt.event.*;
public class hsjs extends Frame{ //创建函数计算窗口框架
Label lbl1 = new Label(); //创建标签
Label lbl2 = new Label();
TextField tf1 = new TextField(); //创建文本域
TextField tf2 = new TextField();
Button btn1 = new Button(); //创建按钮
Button btn2 = new Button();
Button btn3 = new Button();
public hsjs(){ //构造方法
try{jbInit();} //初始化界面
catch(Exception e)
{e.printStackTrace();} //异常处理
}
public boolean handleEvent(Event e){ //处理事件:关闭窗口
switch(e.id){
case Event.WINDOW_DESTROY:System.exit(0);return true;
default:return false;
}
}
private void jbInit() throws Exception{ //设置AWT组件参数
this.setLayout(null); //当前窗框取消布局
lbl1.setText("输入数值:"); //标签文本
lbl1.setBounds(new Rectangle(20,30,70,30));
//边界:左上角,宽高
lbl2.setText("结果:");
lbl2.setBounds(new Rectangle(220,30,70,30));
tf1.setText("0"); //文本域文本
tf1.setBounds(new Rectangle(100,30,70,30));
tf2.setText("0");
tf2.setBounds(new Rectangle(300,30,70,30));
btn1.setLabel("正弦"); //按钮标签
btn1.setBounds(new Rectangle(30,70,70,30));
btn2.setLabel("余弦");
btn2.setBounds(new Rectangle(160,70,70,30));
btn3.setLabel("退出");
btn3.setBounds(new Rectangle(290,70,70,30));
this.add(lbl1,null); //当前框架添加标签
this.add(lbl2,null);
this.add(tf1,null); //当前框架添加文本域
this.add(tf2,null);
this.add(btn1,null); //当前框架添加按钮
this.add(btn2,null);
this.add(btn3,null);
btn1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
//监听按钮事件
btn1_actionPerformed(e); //调用处理程序
}
});
btn2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
//监听按钮事件
btn2_actionPerformed(e); //调用处理程序
}
});
btn3.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
//监听按钮事件
btn3_actionPerformed(e); //调用处理程序
}
});
}
void btn1_actionPerformed(ActionEvent e){//处理按钮事件:sin
tf2.setText(String.valueOf(
Math.sin(Integer.parseInt(tf1.getText()))));
}
void btn2_actionPerformed(ActionEvent e){//处理按钮事件:cos
tf2.setText(String.valueOf(
Math.cos(Integer.parseInt(tf1.getText()))));
}
void btn3_actionPerformed(ActionEvent e){//处理按钮事件:退出
System.exit(0);
}
public static void main(String[] args){ //主程序
hsjs hsjsFrame = new hsjs();
hsjsFrame.setSize(400,300);
hsjsFrame.setTitle
("Java 语言函数计算 fsxss 2003.8.18");
hsjsFrame.show();
}
}