2024年5月21日发(作者:)
计算器源代码
一、计算器源代码文件名:
import .*;
import .*;
public class computer1 extends Frame implements ActionListener{
//声明窗口类并实现动作事件接口。
Button n0,n1,n2,n3,n4,n5,n6,n7,n8,n9;//声明数字按钮
Button op,os,om,od,oe,oc;//声明操作按钮
TextField tfd;//声明文本框
String flg,rslt;//声明标志串、结果串
Panel p1,p2,p3;//声明面板
int i1,i2;
float flt;
computer1(){
super("加减乘除计算器");
n0 = new Button("0");//实现各按钮
n1 = new Button("1");
n2 = new Button("2");
n3 = new Button("3");
n4 = new Button("4");
n5 = new Button("5");
n6 = new Button("6");
n7 = new Button("7");
n8 = new Button("8");
n9 = new Button("9");
op = new Button("加");
os = new Button("减");
om = new Button("乘");
od = new Button("除");
oe = new Button("=");
oc = new Button("c");
tfd = new TextField(20);//实现文本框
p1=new Panel();//实现各面板
p2=new Panel();
p3=new Panel();
setLayout(new FlowLayout());//布局设计,用于安排按钮位置
(n0);//将各数字按钮放入p1中
(n1);
(n2);
(n3);
(n4);
(n5);
(n6);
(n7);
(n8);
(n9);
(op);//将各操作按钮放入p2、p3中
(os);
(om);
(od);
(oe);
(oc);
setLayout(new BorderLayout());//布局设计,用于安排面板位置
add("North",tfd);
add("West",p1);
add("Center",p2);
add("East",p3);
ionListener(this);//注册监听器到各按钮
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
ionListener(this);
addWindowListener(new closeWin());
setSize(600,100);//确定窗口的尺寸
setVisible(true);
}
public static void main (String args[]){
new computer1();
}
public void actionPerformed(ActionEvent e){//处理鼠标事件的方法
try{//异常处理
if(rce()==n0)//按数字键时
t(t()+"0");
if(rce()==n1)
t(t()+"1");
if(rce()==n2)
t(t()+"2");
if(rce()==n3)
t(t()+"3");
if(rce()==n4)
t(t()+"4");
if(rce()==n5)
t(t()+"5");
if(rce()==n6)
t(t()+"6");
if(rce()==n7)
t(t()+"7");
if(rce()==n8)
t(t()+"8");
if(rce()==n9)
t(t()+"9");
if(rce()==op){//按加号键时
i1 = nt(t());
t("");
flg = "op";
}
if(rce()==os){//按减号键时
i1 = nt(t());
t("");
flg = "os";
}
if(rce()==om){//按乘号键时
i1 = nt(t());
t("");
flg = "om";
}
if(rce()==od){//按除号键时
i1 = nt(t());
t("");
flg = "od";
}
if(rce()==oe){//按等号键时
i2 = nt(t());
if(flg=="op"){
rslt=ng(i1+i2);
}
if(flg=="os"){
rslt=ng(i1-i2);
}
if(flg=="om"){
rslt=ng(i1*i2);
}
if(flg=="od"){//除法需做小数处理
flt=((float)i1)/((float)i2);
rslt=ng(flt);
}
t(rslt);
}
if(rce()==oc){//按清除键时
t("");
flg = "";
}
}catch(Exception ex){}//扑捉到异常,但不进行处理
}
}
class closeWin extends WindowAdapter{ //关闭窗口
public void windowClosing(WindowEvent e){
Frame frm=(Frame)(rce());
e();
(0);
}
}
二、计算器界面
三、修改后计算器界面
发布者:admin,转转请注明出处:http://www.yc00.com/web/1716293858a2727149.html
评论列表(0条)