package mypackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mycalculator extends WindowAdapter implements ActionListener{
/**
* @param args
*/
JFrame frame;
JTextField dataplay;
JButton jia,jian,cheng,chu,tuige,fuwei,qingling,guanyu,kaifang,baifen,daoshu,dengyu,fuhao,dian;
JButton num[]=new JButton[10];
JDialog dialog;
double result,getResult;//用于存放计算结果和计算过程
int i=0,k=0,m,n,jisuan;//i用于对输入数据进行计数,jisuan用于保留上次的运算符号,其他控制循环
final int length=32;
boolean flag,shouci=true;//flag用于防止没有输入数据而连续点击运算按钮,用于容错,shouci用于第一次运算,给getResult赋值
public void calcultor(){//设置面板和响应
frame=new JFrame("计算器");
frame.setSize(360, 230);
frame.setBackground(Color.LIGHT_GRAY);
frame.setLocation(380,260);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));//??????
frame.setResizable(false);
dataplay=new JTextField(32);
dataplay.setText("0");
dataplay.setHorizontalAlignment(SwingConstants.RIGHT);
dataplay.setEditable(false);
frame.add(dataplay);
Panel toppanel=new Panel();
frame.add(toppanel);
toppanel.setLayout(new GridLayout(1,4,3,3));//??????
tuige=new JButton("Back←");
tuige.setForeground(Color.RED);
toppanel.add(tuige);
tuige.addActionListener(this);
fuwei=new JButton("CE");
fuwei.setForeground(Color.RED);
toppanel.add(fuwei);
fuwei.addActionListener(this);
qingling=new JButton("C");
qingling.setForeground(Color.RED);
toppanel.add(qingling);
qingling.addActionListener(this);
guanyu=new JButton("Aboutme");
guanyu.setForeground(Color.RED);
toppanel.add(guanyu);
guanyu.addActionListener(this);
dialog=new JDialog(frame,"Mycalculator",true);
dialog.setSize(230, 110);
dialog.setLocation(500, 300);
Label lable=new Label("欢迎交流! hello_chenjin@sina.com",1);
lable.setForeground(Color.RED);
dialog.add(lable,"Center");
dialog.addWindowListener(this);
Panel mainpanel=new Panel();
mainpanel.setLayout(new GridLayout(1,2,8,5));
frame.add(mainpanel);
Panel numpanel=new Panel();
numpanel.setLayout(new GridLayout(4,3,4,3));
mainpanel.add(numpanel);
Panel calpanel=new Panel();
calpanel.setLayout(new GridLayout(4,2,4,3));
mainpanel.add(calpanel);
for(m=9;m>=0;m--){
num[m]=new JButton(String.valueOf(m));
numpanel.add(num[m]);
num[m].addActionListener(this);
}
chu=new JButton(" / ");
chu.setForeground(Color.RED);
calpanel.add(chu);
chu.addActionListener(this);
kaifang=new JButton("sqrt");
calpanel.add(kaifang);
kaifang.addActionListener(this);
cheng=new JButton("*");
cheng.setForeground(Color.RED);
calpanel.add(cheng);
cheng.addActionListener(this);
baifen=new JButton("%");
calpanel.add(baifen);
baifen.addActionListener(this);
jian=new JButton("-");
jian.setForeground(Color.RED);
calpanel.add(jian);
jian.addActionListener(this);
daoshu=new JButton("1/X");
calpanel.add(daoshu);
daoshu.addActionListener(this);
jia=new JButton("+");
jia.setForeground(Color.RED);
calpanel.add(jia);
jia.addActionListener(this);
dengyu=new JButton("=");
dengyu.setForeground(Color.RED);
calpanel.add(dengyu);
dengyu.addActionListener(this);
fuhao=new JButton(" +/- ");
numpanel.add(fuhao);
fuhao.addActionListener(this);
dian=new JButton(".");
numpanel.add(dian);
dian.addActionListener(this);
Toolkit tk=Toolkit.getDefaultToolkit();
Image image=tk.createImage("jsq.gif");
frame.setIconImage(image);
frame.addWindowListener(this);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
getResult=Double.valueOf(dataplay.getText()).doubleValue();//string转换为double
for(k=0;k<10;k++){//数字响应,控制有效的数字输入
if(e.getSource()==num[k]){
flag=true;
if(i==0)
dataplay.setText("");
String s=dataplay.getText();
if(s.length()<length&&(getResult!=0||s.contains(".")))
dataplay.setText(dataplay.getText()+e.getActionCommand());
if(e.getSource()==num[0]&&getResult==0&&!s.contains("."))
dataplay.setText("0");
if(e.getSource()!=num[0]&&getResult==0&&!s.contains("."))
dataplay.setText(e.getActionCommand());//??????
i++;
}
}
if(e.getSource()==jia){
calculate();//调用计算函数
jisuan=1;}//记录此次运算符号,在下次输入运算符或等号时计算此次运算结果
if(e.getSource()==jian){
calculate();
jisuan=2;
}
if(e.getSource()==cheng){
calculate();
jisuan=3;
}
if(e.getSource()==chu){
calculate();
jisuan=4;
}
if(e.getSource()==dengyu){
calculate();
jisuan=0;
}
if(e.getSource()==fuhao){
String s=dataplay.getText();
if(s.charAt(0)=='-'){
dataplay.setText("");//重新进行赋值,去掉负号
for(i=1;i<s.length();i++){
dataplay.setText(dataplay.getText()+s.charAt(i));
}
}
else if(s.charAt(0)!='0'){
dataplay.setText('-'+s);
}
}
if(e.getSource()==dian){
String s=dataplay.getText();
if(!s.contains("."))//防止出现两个点
dataplay.setText(dataplay.getText()+e.getActionCommand());
}
if(e.getSource()==kaifang){
result=Math.sqrt(getResult);
setResult();
}
if(e.getSource()==baifen){
result=getResult/100;
setResult();
}
if(e.getSource()==daoshu){
result=1/getResult;
setResult();
}
if(e.getSource()==tuige){
String s=dataplay.getText();
if(s.length()>1){
dataplay.setText("");//重新赋值,去掉s.length()-1即最后一位
for(n=0;n<s.length()-1;n++)
dataplay.setText(dataplay.getText()+s.charAt(n));
}
else
dataplay.setText("0");
}
if(e.getSource()==fuwei||e.getSource()==qingling){
i=0;jisuan=0;flag=false;shouci=true;//初始化
dataplay.setText("0");
}
if(e.getSource()==guanyu){
dialog.setVisible(true);
}
}
public void setResult(){
dataplay.setText(String.valueOf(result));
String s=dataplay.getText();
char a=s.charAt(s.length()-1);//保留有效位
char b=s.charAt(s.length()-2);
if(a=='0'&&b=='.')
dataplay.setText(String.valueOf(Math.round(result)));
}
public void windowClosing(WindowEvent e){
if(e.getSource()==dialog)//关闭对话框
dialog.setVisible(false);
else
if(e.getSource()==frame)//关闭计算器
System.exit(0);
}
public void calculate(){//按钮按下后进行计算
if(shouci)
result=getResult;//首次运算后进行赋值
else
{
if(jisuan==1&&flag){//计算出上次运算结果,且不是连续点击按钮,输入容错
result+=getResult;
flag=false;
}
if(jisuan==2&&flag){
result-=getResult;
flag=false;
}
if(jisuan==3&&flag){
result*=getResult;
flag=false;
}
if(jisuan==4&&flag){
result/=getResult;
flag=false;
}
}
setResult();
shouci=false;i=0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Mycalculator mycalcultor=new Mycalculator();
mycalcultor.calcultor();
}
}
- 1
- 2
前往页