计算器的程序代码java_求用JAVA编写的计算器程序代码
展開全部
import java.awt.*;//引入包java.awt中所有的類
import java.awt.event.*;//引入包java.awt.event中所有的類.
public class Calculator extends WindowAdapter implements ActionListener//創建Calculator類,
實現62616964757a686964616fe78988e69d8331333239303838ActionListener接口.
{
private double result=0,data1=0,radixPointDepth=1;//定義變量
private boolean radixPointIndicate=false,resultIndicate=false;
private char prec='+';//創建優先默認字符"+"
private Frame f;//創建窗口
private TextField tf;//創建文本框
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;//創建按鈕
private Panel p;//創建/面板容器
static public void main(String args[])//main方法,創建calGUI(圖形用戶界面),完成初試化
{
//構造器
Calculator de=new Calculator();//創建構造方法
de.go();
}
public void go()
{
f=new Frame("計算器");
p=new Panel();//運算界面p
p.setLayout(new GridLayout(4,4)); // 設置p的布局為GridLayout,四行四列
tf=new TextField(30);
//實例化按鈕
b1=new Button("7");
b2=new Button("8");
b3=new Button("9");
b4=new Button("+");
b5=new Button("4");
b6=new Button("5");
b7=new Button("6");
b8=new Button("-");
b9=new Button("1");
b10=new Button("2");
b11=new Button("3");
b12=new Button("*");
b13=new Button("0");
b14=new Button(".");
b15=new Button("=");
b16=new Button("/");
b17=new Button("清零");
f.add(tf,"North"); //把文本區域添加到框架的上方
f.add(p,"Center"); //把面版添加到框架的中間
f.add(b17,"South"); //把按鈕(清零)添加到框架的下方
//把按鈕添加到面版上
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
//為按鈕添加監聽
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
f.addWindowListener(this); //為框架添加監聽
f.setSize(300,190);//設置框架的大小
f.setVisible(true);//設置框架為可見
}
//監聽程序
public void actionPerformed(ActionEvent e)
{
String s;
s=e.getActionCommand();
//SWITCH開關
switch(s.charAt(0))
{
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case
'7': case '8': case '9'://按了“0-9”,就執行下面
if(resultIndicate)
{
result=0;
data1=0;
prec='+';
}
Integer Int1=new Integer(s);
if(radixPointIndicate)
{
radixPointDepth=radixPointDepth/10;
data1=data1+(Int1.intValue())*radixPointDepth;
}
else
{
data1=data1*10+(Int1.intValue());
}
Double displayNumber=new Double(data1);
tf.setText(displayNumber.toString());
resultIndicate=false;
break;
case '+': case '-':case '*':case '/':case '='://按了“+、-、*、/”,就
執行下面
if(s.charAt(0)!='='&&resultIndicate)
{
prec=s.charAt(0);
resultIndicate=false;
}
else
{
//用SWITCH開關運算出執行了“+、-、*、/”的結果
switch(prec)
{
case '+':
result=result+data1;
break;
case '-':
result=result-data1;
break;
case '*':
result=result*data1;
break;
case '/':
result=result/data1;
break;
}
}
radixPointIndicate=false;
radixPointDepth=1;
displayNumber=new Double(result);
tf.setText(displayNumber.toString());
//監聽是否按了“=”
if(s.charAt(0)!='=')
{
data1=0;
prec=s.charAt(0);
}
else
{
resultIndicate=true;
}
break;
case '.':
radixPointIndicate=true;
break;
}
//監聽是否按了為“清零”,是則對各數據清零
if(s.equals("清零"))
{
result=0;
data1=0;
radixPointDepth=1;
tf.setText("");
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
已贊過
已踩過<
你對這個回答的評價是?
評論
收起
總結
以上是生活随笔為你收集整理的计算器的程序代码java_求用JAVA编写的计算器程序代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 万事兴集成灶的售后怎么样?是跟家电品牌一
- 下一篇: 德力西低压电控设备怎么样