java 计算器.docx

上传人:b****6 文档编号:4295143 上传时间:2022-11-29 格式:DOCX 页数:19 大小:18.24KB
下载 相关 举报
java 计算器.docx_第1页
第1页 / 共19页
java 计算器.docx_第2页
第2页 / 共19页
java 计算器.docx_第3页
第3页 / 共19页
java 计算器.docx_第4页
第4页 / 共19页
java 计算器.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

java 计算器.docx

《java 计算器.docx》由会员分享,可在线阅读,更多相关《java 计算器.docx(19页珍藏版)》请在冰豆网上搜索。

java 计算器.docx

java计算器

packagech17;

importjava.awt.*;

importjava.awt.event.*;

importjava.lang.Math.*;

importjavax.swing.*;

importjavax.swing.JTextField;

publicclassCalculator

{

publicstaticvoidmain(Stringargs[])

{

Frameframe=newFrame("java计算器");

JTextFieldresult=newJTextField();

result.setHorizontalAlignment(JTextField.RIGHT);//从右往左输入

//框架设置

//frame.setSize(700,1000);

//frame.setLayout(newBorderLayout(5,5));

frame.setBackground(Color.cyan);

frame.setLocation(10,10);

Panelp=newPanel();

Panelh=newPanel();

//菜单条

MenuBarmenubar=newMenuBar();

frame.setMenuBar(menubar);

Menumenu=newMenu("菜单");

Menuchakan=newMenu("查看");

Menuedit=newMenu("编辑");

Menuhelp=newMenu("帮助");

//Menum50=newMenu("回应");

menubar.add(menu);

menubar.add(chakan);

menubar.add(edit);

menubar.setHelpMenu(help);

MenuItemfile=newMenuItem("文件");

MenuItemsave=newMenuItem("保存");

MenuItemquit=newMenuItem("退出");

MenuItemopen=newMenuItem("打开");

MenuItemcopy=newMenuItem("复制");

MenuItempaste=newMenuItem("粘贴");

MenuItemabout=newMenuItem("关于");

MenuItemintroduce=newMenuItem("介绍");

menu.add(file);

menu.add(save);

menu.addSeparator();

menu.add(quit);

chakan.add(open);

edit.add(copy);

edit.add(paste);

help.add(about);

help.add(introduce);

h.setLayout(newGridLayout(1,3,1,1));

ButtonCE=newButton("CE");

ButtonC=newButton("C");

ButtonBackspace=newButton("Backspace");

h.add(CE);h.add(C);h.add(Backspace);

CE.setBackground(newColor(251,74,103));

C.setBackground(newColor(251,74,103));

Backspace.setBackground(newColor(251,74,103));//按钮颜色

//数字以及运算面板

p.setLayout(newGridLayout(4,5,1,1));

Buttonb1=newButton("7");

Buttonb2=newButton("8");

Buttonb3=newButton("9");

Buttonb4=newButton("/");

Buttonsqrt=newButton("sqrt");

Buttonb6=newButton("4");

Buttonb7=newButton("5");

Buttonb8=newButton("6");

Buttonb9=newButton("*");

Buttonmod=newButton("%");

Buttonb11=newButton("1");

Buttonb12=newButton("2");

Buttonb13=newButton("3");

Buttonb14=newButton("-");

Buttondaoshu=newButton("1/x");

Buttonb16=newButton("0");

Buttonb17=newButton("+/-");

Buttonadd=newButton("+");

ButtonBdot=newButton(".");

Buttonequal=newButton("=");

p.add(b1);p.add(b2);p.add(b3);

p.add(b4);p.add(sqrt);p.add(b6);

p.add(b7);p.add(b8);p.add(b9);

p.add(mod);p.add(b11);p.add(b12);

p.add(b13);p.add(b14);p.add(daoshu);

p.add(b16);p.add(b17);p.add(add);

p.add(Bdot);p.add(equal);

b1.setBackground(newColor(249,248,158));

b2.setBackground(newColor(249,248,158));

b3.setBackground(newColor(249,248,158));

b4.setBackground(newColor(205,221,236));

sqrt.setBackground(newColor(205,221,236));

b6.setBackground(newColor(249,248,158));

b7.setBackground(newColor(249,248,158));

b8.setBackground(newColor(249,248,158));

b9.setBackground(newColor(205,221,236));

mod.setBackground(newColor(205,221,236));

b11.setBackground(newColor(249,248,158));

b12.setBackground(newColor(249,248,158));

b13.setBackground(newColor(249,248,158));

b14.setBackground(newColor(205,221,236));

daoshu.setBackground(newColor(205,221,236));

b16.setBackground(newColor(249,248,158));

b17.setBackground(newColor(249,248,158));

add.setBackground(newColor(249,248,158));

Bdot.setBackground(newColor(205,221,237));

equal.setBackground(newColor(205,221,236));

Monitormonitoresult=newMonitor(result);

//监听计算的抽象类,需要重新定义

CE.addActionListener(monitoresult);

C.addActionListener(monitoresult);

Backspace.addActionListener(monitoresult);

b1.addActionListener(monitoresult);

b2.addActionListener(monitoresult);

b3.addActionListener(monitoresult);

b4.addActionListener(monitoresult);

sqrt.addActionListener(monitoresult);

b6.addActionListener(monitoresult);

b7.addActionListener(monitoresult);

b8.addActionListener(monitoresult);

b9.addActionListener(monitoresult);

mod.addActionListener(monitoresult);

b11.addActionListener(monitoresult);

b12.addActionListener(monitoresult);

b13.addActionListener(monitoresult);

b14.addActionListener(monitoresult);

daoshu.addActionListener(monitoresult);

b16.addActionListener(monitoresult);

b17.addActionListener(monitoresult);

add.addActionListener(monitoresult);

Bdot.addActionListener(monitoresult);

equal.addActionListener(monitoresult);

Monitor2m=newMonitor2(frame);//抽象类,需要重新定义

frame.addWindowListener(m);

result.setText("0");

frame.add(result,BorderLayout.NORTH);

frame.add(p,BorderLayout.SOUTH);

frame.add(h,BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

}

 

classMonitorimplementsActionListener

{

JTextFieldt;

intflag;

doubledoublend;

inti=0;

Stringbegin="";

Monitor(JTextFieldt)

{

this.t=t;

}

publicvoidactionPerformed(ActionEvente)

{

if(e.getActionCommand().equals("1"))

{

if(t.getText().trim().equals("0"))

{

t.setText("");

begin=t.getText();

begin=begin+"1";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"1";

t.setText(begin);

}

}

if(e.getActionCommand().equals("2"))

{

if(t.getText().trim().equals("0"))

{

t.setText("");

begin=t.getText();

begin=begin+"2";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"2";

t.setText(begin);

}

}

if(e.getActionCommand().equals("3"))

{

if(t.getText().trim().equals("0"))

{

t.setText("");

begin=t.getText();

begin=begin+"3";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"3";

t.setText(begin);

}

}

if(e.getActionCommand().equals("4"))

{

if(t.getText().trim().equals("0"))

{

t.setText("");

begin=t.getText();

begin=begin+"4";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"4";

t.setText(begin);

}

}

if(e.getActionCommand().equals("5"))

{

if(t.getText().trim().equals("0"))

{

t.setText("");

begin=t.getText();

begin=begin+"5";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"5";

t.setText(begin);

}

}

if(e.getActionCommand().equals("6"))

{

if(t.getText().trim().equals("0"))

{

t.setText("");

begin=t.getText();

begin=begin+"6";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"6";

t.setText(begin);

}

}

if(e.getActionCommand().equals("7"))

{

if(t.getText().trim().equals("0"))

{

t.setText("");

begin=t.getText();

begin=begin+"7";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"7";

t.setText(begin);

}

}

if(e.getActionCommand().equals("8"))

{

if(t.getText().trim().equals("0"))

{

t.setText("");

begin=t.getText();

begin=begin+"8";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"8";

t.setText(begin);

}

}

if(e.getActionCommand().equals("9"))

{

if(t.getText().trim().equals("0"))

{

t.setText("");

Stringbegin=t.getText();

begin=begin+"9";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"9";

t.setText(begin);

}

}

if(e.getActionCommand().equals("0"))

{

if(t.getText().trim().equals("0"))//0的问题,0在第一位则不显示

{

t.setText("");

begin=t.getText();

begin=begin+"0";

t.setText(begin);

}

else

{

begin=t.getText();

begin=begin+"0";

t.setText(begin);

}

}

if(e.getActionCommand().equals("+/-"))

{

begin=t.getText();

doublend=Double.parseDouble(begin);

doublere=-doublend;

t.setText(re+"");

//inta=Integer.valueOf(begin);

//a=a*(-1);

//begin=String.valueOf(a);没问题

//t.setText(begin);

}

if(e.getActionCommand().equals("."))

{

intindex=begin.indexOf('.');//该方法的作用是查找特定字符或字符串在当前字符串中的起始位置,如果不存在则返回-1

/*/intindexOf(intch,intfromIndex)函数:

就是字符ch在字串fromindex位后出现的第一个位置.没有找到返加-1

eg:

Stringstr="a2dfcfar1bzvb";

System.out.println(str.indexOf(97,2));

输出:

6

a的ASCII为97,就从d开始找a找到了输出a所在字符串的确切位置,找不到就输出-1!

(java中位置第一个从0开始)

*/

if(index==-1)//小数点只能按一次

{

begin=begin+".";

}

else

System.out.println("你再点的话就不是小数了!

");

t.setText(begin);

}

if(e.getActionCommand().equals("+"))

{

flag=1;

begin=t.getText();

doublend=Double.parseDouble(begin);

t.setText("");

}

if(e.getActionCommand().equals("-"))

{

flag=2;

begin=t.getText();

t.setText("");

doublend=Double.parseDouble(begin);

}

if(e.getActionCommand().equals("*"))

{

flag=3;

begin=t.getText();

t.setText("");

doublend=Double.parseDouble(begin);

}

if(e.getActionCommand().equals("/"))

{

flag=4;

begin=t.getText();

t.setText("");

doublend=Double.parseDouble(begin);

}

if(e.getActionCommand().equals("sqrt"))

{

flag=5;

begin=t.getText();

doublend=Double.parseDouble(begin);

}

if(e.getActionCommand().equals("%"))

{

flag=6;

begin=t.getText();

doublend=Double.parseDouble(begin);

}

if(e.getActionCommand().equals("1/x"))

{

flag=7;

begin=t.getText();

doublend=Double.parseDouble(begin);

}

if(e.getActionCommand().equals("C"))

{

doublend=0;

t.setText("0");

}

if(e.getActionCommand().equals("CE"))

{

doublend=0;

t.setText("0");

}

if(e.getActionCommand().equals("Backspace"))

{

intcount;

count=begin.length()-1;//

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 其它课程

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1