資源簡介
簡易計(jì)算器,java代碼編寫,有詳細(xì)注釋

代碼片段和文件信息
import?java.applet.*;
import?java.awt.*;
import?java.awt.event.*;
public?class?計(jì)算器1?extends?applet?implements?ActionListener?
{
boolean?com?=?true;?
TextField?text1=new?TextField(10);
Button?button[]=new?Button[17];
Panel?B=new?Panel();
GridLayout?c1=new?GridLayout(44);
int?symbol=0;????????????????????????????//必須定義成全局變量
double?num=0.0num1=0.0;?????????????????//否則結(jié)果出錯(cuò)
String?s=““;
int?i=0;double?total=0;
/*init是初始化模塊(包括按鈕的添加初始化,面板的布局監(jiān)視器的添加)
*/
public?void?init()??
{
//setLayout(new?BorderLayout());
text1.setEnabled(false);
Panel?p=new?Panel();
button[16]=new?Button(“清除“);
p.setLayout(new?FlowLayout());
p.add(text1);
p.add(button[16]);
button[16].addActionListener(new?ActionListener()
{
public?void?actionPerformed(ActionEvent?e)
{
text1.setText(““);
}
});
add(p);
text1.addActionListener(this);
B.setLayout(c1);
for(int?i=0;i<=9;i++)
{
button[i]=new?Button(““+i);
}
button[10]=new?Button(“+“);
button[11]=new?Button(“-“);
button[12]=new?Button(“*“);
button[13]=new?Button(“/“);
button[14]=new?Button(“=“);
button[15]=new?Button(“Esc“);
for(int?i=0;i<=15;i++)??????????????????//利用一個(gè)循環(huán)添加按鈕對象
{
B.add(button[i]);
}??
for(int?j=0;j<=15;j++)??????????????????//利用循環(huán)添加按鈕監(jiān)視器
{
button[j].addActionListener(this);??
}
add(B);??
}
public?void?actionPerformed(ActionEvent?e)??????//觸發(fā)事件的定義
{
/*此函數(shù)主要實(shí)現(xiàn)用戶按下一個(gè)按鈕,將按鈕上的信息顯示在文本框中,
?*?同時(shí)用參數(shù)對其進(jìn)行保存,在用戶按下操作符按鈕是能進(jìn)行后續(xù)的
?*?計(jì)算操作。
?*?*/
/*?for(int?j=0;j<=9;j++)
{
if(e.getSource()==button[0])
{
if(com||?i==0)
{
text1.setText(““+j);
com=false;
i=1;
}
else?????????????????????????//實(shí)現(xiàn)重復(fù)輸入
{
s=text1.getText();
text1.setText(s+j);
}
????}
}??????*/
?
if(e.getSource()==button[0])??
{
/*解釋這一個(gè)函數(shù),你自己再去理解一下,
?*在這里設(shè)置一個(gè)參數(shù)i初始值為0,
?*一個(gè)狀態(tài)參數(shù)com(該參數(shù)主要是實(shí)現(xiàn)數(shù)據(jù)的重復(fù)輸入)
?*當(dāng)按下第一次的時(shí)候,就將com值修改為flase,
?*第二次點(diǎn)擊該按鈕的時(shí)候就進(jìn)入else語句中,實(shí)現(xiàn)多位數(shù)字的輸入,按5下按鈕就顯示“00000”
?*如果沒有這個(gè)狀態(tài)參數(shù)的話就只能顯示一個(gè)“0”,不管你按幾下只顯示一個(gè)“0”;
?*老師問你的時(shí)候你照著上面說就行了,
?*這個(gè)參數(shù)i的作用和com參數(shù)的作用是一樣的。
?*?下面的函數(shù)和這個(gè)是一樣的,按幾下按鈕,前面就顯示幾個(gè)按鈕上的數(shù)字,
?*?比如按鈕“1”,按一下是一個(gè)“1”,按兩下,就是“11”,就這樣了。
?*?
?*/
if(com||?i==0)
{
text1.setText(“0“);
com=false;
i=1;
//System.out.println(“com=“+com);
}
else?????????????????????????//實(shí)現(xiàn)重復(fù)輸入
{
s=text1.getText();
text1.setText(s+“0“);
//System.out.println(“com=“+com);
}
????}
else?if(e.getSource()==button[1])
{
if(com||?i==0)
{
text1.setText(“1“);
//num=Double.parseDouble(text1.getText());
com=false;
i=1;
}
else?????????????????????????//實(shí)現(xiàn)重復(fù)輸入
{
s=text1.getText();
text1.setText(s+“1“);
}
????}
else?if(e.getSource()==button[2])
{
if(com||?i==0)
{
text1.setText(“2“);
//num=Double.parseDouble(text1.getText());
com=false;
i=1;
}
else????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????8766??2010-05-19?00:05??簡易計(jì)算器.java
評(píng)論
共有 條評(píng)論