import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.lang.Math;
import java.lang.*;
//import java.text.DecimalFormat;
public class Calculator extends WindowAdapter implements ActionListener
{
Frame f ;
MenuBar mb ;
Menu medit,mview,mhelp;
MenuItem mcopy,mpaste,mstand,mscien,mvector,mtophelp,maboutcal;
Dialog d1,d2,d3 ;
Panel p2 ,p3 ,p4;
JPanel p0,p1 ,p5;
TextField result ,blank ;
Button num_button[];
Button add,decrease,mul,div;
Button bsqrt,baifenshu,daoshu,equal,addanddec,dian;
Button backspace,clear,ce;
Button mc,mr,ms,madd;
boolean clickable = true ;
short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
double memoryi = 0.0 ; // 把数保存在此内存
double vard ,answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd)
String copy ;
public void display()
{
f = new Frame("课程设计--计算器");
f.setSize(260,230);
f.setLocation(300,400);
f.setBackground(Color.lightGray );
//public void addmyMenu(Frame f)
mb = new MenuBar();
f.setMenuBar(mb);
medit = new Menu("编辑(E)");
mview = new Menu("查看(V)");
mhelp = new Menu("帮助(H)");
mb.add(medit);
mb.add(mview);
mb.add(mhelp);
p0 = new JPanel();
p1 = new JPanel();
p5 = new JPanel();
mcopy = new MenuItem("复制(C)",new MenuShortcut(KeyEvent.VK_C));
mpaste = new MenuItem("粘贴(P)",new MenuShortcut(KeyEvent.VK_V));
mstand = new MenuItem("●标准型(T)");
mscien = new MenuItem(" 科学型(S)");
mvector = new MenuItem("数字分组(I)");
mtophelp = new MenuItem("帮助主题(H)");
maboutcal= new MenuItem("关于计算器(A)");
medit.add(mcopy);
medit.add(mpaste);
mview.add(mstand);
mview.add(mscien);
mview.add(mvector);
mhelp.add(mtophelp);
mhelp.add(maboutcal);
//public void addmyButton(Frame f)
result = new TextField(30);
result.setEditable(false);
p2=new Panel();
p2.add(result);
f.add(p2,BorderLayout.NORTH);
p3=new Panel();
blank= new TextField(2);
blank.setEditable(false);
p3.setLayout(new FlowLayout(FlowLayout.RIGHT,12,2));
backspace= new Button("BackSpace");
backspace.setForeground(Color.red);
ce = new Button("CE");
ce.setForeground(Color.red);
clear = new Button("Clear");
clear.setForeground(Color.red);
p3.add(blank);
p3.add(backspace);
p3.add(ce);
p3.add(clear);
f.add(p3,BorderLayout.CENTER);
p4 = new Panel();
p4.setLayout(new GridLayout(4,6,6,6));
num_button = new Button[10];
for(int i=0 ; i<=9 ; i++)
{
num_button[i]= new Button(Integer.toString(i));
num_button[i].setForeground(Color.blue);
}
mc = new Button("MC");
mc.setForeground(Color.red);
mr = new Button("MR");
mr.setForeground(Color.red);
ms = new Button("MS");
ms.setForeground(Color.red);
madd= new Button("M+");
madd.setForeground(Color.red);
div = new Button("/");
div.setForeground(Color.red);
mul = new Button("*");
mul.setForeground(Color.red);
decrease=new Button("-");
decrease.setForeground(Color.red);
add = new Button("+");
add.setForeground(Color.red);
bsqrt=new Button("sqrt");
bsqrt.setForeground(Color.blue);
baifenshu=new Button("%");
baifenshu.setForeground(Color.blue);
daoshu = new Button("1/x");
daoshu.setForeground(Color.blue);
equal = new Button("=");
equal.setForeground(Color.red);
addanddec=new Button("+/-");
addanddec.setForeground(Color.blue);
dian = new Button(".");
dian.setForeground(Color.blue);
p4.add(mc); p4.add(num_button[7]); p4.add(num_button[8]); p4.add(num_button[9]);
p4.add(div); p4.add(bsqrt); p4.add(mr); p4.add(num_button[4]);
p4.add(num_button[5]); p4.add(num_button[6]); p4.add(mul); p4.add(baifenshu);
p4.add(ms); p4.add(num_button[1]); p4.add(num_button[2]); p4.add(num_button[3]);
p4.add(decrease); p4.add(daoshu); p4.add(madd); p4.add(num_button[0]);
p4.add(addanddec); p4.add(dian); p4.add(add); p4.add(equal);
f.add(p4,BorderLayout.SOUTH);
f.addWindowListener(this);
f.setVisible(true);
////////////////////////以上为图形用户界面设计////////////////////////
/////////////////////////以下是为各个组件注册监听器///////////////////////////////////
medit.addActionListener(this);
mview.addActionListener(this);
mhelp.addActionListener(this);
mcopy.addActionListener(this);
mpaste.addActionListener(this);
mscien.addActionListener(this);
mvector.addActionListener(this);
mtophelp.addActionListener(this);
maboutcal.addActionListener(this);
backspace.addActionListener(this);
ce.addActionListener(this);
clear.addActionListener(this);
mc.addActionListener(this);
mr.addActionListener(this);
ms.addActionListener(this);
madd.addActionListener(this);
addanddec.addActionListener(this);
dian.addActionListener(this);
div.addActionListener(this);
mul.addActionListener(this);
decrease.addActionListener(this);
add.addActionListener(this);
bsqrt.addActionListener(this);
baifenshu.addActionListener(this);
daoshu.addActionListener(this);
equal.addActionListener(this);
for(int i=0 ;i <=9 ; i++)
num_button[i].addActionListener(this);
}
/////////////////////////以上是为组件注册监听器////////////////////////////
/////////////////////////以下是事件处理/////////////////////////////////////
String fenzu(String s1)
{
String s = s1.replaceAll(",","");
String temp1="" ;
String temp2="" ;
String temp3="";
String temp4="";
String temp5="";
int t ;
int strlen = s.length();
if(s.contains("."))
{
t = s.indexOf('.');
}
else t = s.length() ;
if(t <= 3)
{
temp2 = s.substring(0,t) ;
}
else if(t >3 && t<= 6)
{
temp1 = s.substring(t-3,t);
temp2 = s.substring(0,t-3);
temp2 = temp2+","+temp1 ;
}
else if(t>6 && t<=9)
{
temp1 = s.substring(t-3,t);
temp2 = s.substring(t-6,t-3);
temp3 = s.substring(0,t-6);
temp2 = temp3+","+temp2+","+temp1;
}
else if(t>9 && t<=12)
{
temp1 = s.substring(t-3,t);
temp2 = s.substring(t-6,t-3);
temp3 = s.substring(t-9,t-6);
temp4 = s.substring(0,t-9);
temp2 = temp4+","+temp3+","+temp2+","+temp1;
}
else if(t>12 )
{
temp1 = s.substring(t-3,t);
temp2 = s.substring(t-6,t-3);
temp3 = s.substring(t-9,t-6);
temp4 = s.substring(t-12,t-9);
temp5 = s.substring(0,t-12);
temp2 = temp5+","+temp4+","+temp3+","+temp2+","+temp1;
}
if(s.contains("."))
{
temp2 = temp2 + s.substring(t , strlen) ;
return temp2 ;
}
else return temp2 ;
}
public void actionPerformed(ActionEvent e)
{
//boolean sign = false; //判定是否是double参与运算,是为true,不是为false
Object temp = e.getSource(); //temp 是具体的点击到的按钮对象
try
{
/************************如按下 数据 按钮,将数插入当前文本框字符串之后************************/
for (int i = 0; i <= 9; i++) //&& clickable == true
{
if (temp == num_button[i] && mvector.getLabel().compareTo("数字分组(I)")==0)
{
result.setText(result.getText() + Integer.toString(i));
}
else if(temp == num_button[i] && mvector.getLabel().compareTo("√数字分组(I)")==0)
{
String s = result.getText();
String stemp = s.replaceAll(",","");
//int strlen = s.length();
int len1 =
评论3
最新资源