整理版简易型科学计算器.docx

上传人:b****5 文档编号:5938131 上传时间:2023-01-02 格式:DOCX 页数:13 大小:32.26KB
下载 相关 举报
整理版简易型科学计算器.docx_第1页
第1页 / 共13页
整理版简易型科学计算器.docx_第2页
第2页 / 共13页
整理版简易型科学计算器.docx_第3页
第3页 / 共13页
整理版简易型科学计算器.docx_第4页
第4页 / 共13页
整理版简易型科学计算器.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

整理版简易型科学计算器.docx

《整理版简易型科学计算器.docx》由会员分享,可在线阅读,更多相关《整理版简易型科学计算器.docx(13页珍藏版)》请在冰豆网上搜索。

整理版简易型科学计算器.docx

整理版简易型科学计算器

 

简易型科学计算器

import;

import;

import;

import;

import;

import;

import;

import;

import;

import;

import;

import;

import;

import;

import;

import;

import;

import;

publicclassmyCalucatorextendsJFrame{

privateJPanelpanel1,panel2,panel3,panel4;

privateJTextFieldtf;

privateJMenumenu1,menu2;

privateJMenuItemseeItem1,seeItem2,help1,help2;

privateJMenuBarmyBar;

privateJButtonBackspace,CE,C,sin,ercifang,cos,sancifang,tan,jiecheng,lg,pai,

zero,one,two,three,four,five,six,seven,eight,nine,jia,jian,cheng,chu,dot,equal;

Strings;

intx;

doublenum1,num2,num3;

publicmyCalucator(){

super("简易型科学计算器");

this.setLayout(newBorderLayout(10,5));

panel1=newJPanel(newGridLayout(1,3,10,10));

panel2=newJPanel(newGridLayout(4,2,5,5));

panel3=newJPanel(newGridLayout(4,4,5,5));

panel4=newJPanel(newBorderLayout(5,5));

/*

*菜单栏

*/

myBar=newJMenuBar();

menu1=newJMenu("查看");

menu2=newJMenu("帮助");

menu1.setFont(newFont("宋体",Font.PLAIN,12));

menu2.setFont(newFont("宋体",Font.PLAIN,12));

/*

*查看栏

*/

seeItem1=newJMenuItem("标准型");

seeItem2=newJMenuItem("科学型");

seeItem1.setFont(newFont("宋体",Font.PLAIN,12));

seeItem2.setFont(newFont("宋体",Font.PLAIN,12));

/*

*帮助栏

*/

help1=newJMenuItem("查看帮助");

help2=newJMenuItem("关于计算器");

help1.setFont(newFont("宋体",Font.PLAIN,12));

help2.setFont(newFont("宋体",Font.PLAIN,12));

menu1.add(seeItem1);

menu1.add(seeItem2);

menu2.add(help1);

menu2.add(help1);

myBar.add(menu1);

myBar.add(menu2);

this.setJMenuBar(myBar);

/*

*文本域

*/

tf=newJTextField();

tf.setEditable(false);

tf.setBackground(Color.white);

tf.setText("");

tf.setHorizontalAlignment(JTextField.RIGHT);

tf.setBorder(BorderFactory.createLoweredBevelBorder());

init();//对计算器进行初始化

}

publicvoidinit(){

Backspace=newJButton("←");

Backspace.setForeground(Color.blue);

CE=newJButton("CE");

CE.setForeground(Color.blue);

C=newJButton("C");

C.setForeground(Color.blue);

panel1.add(Backspace);

panel1.add(CE);

panel1.add(C);

sin=newJButton("sin");

sin.setForeground(Color.red);

ercifang=newJButton("x^2");

ercifang.setForeground(Color.red);

cos=newJButton("cos");

cos.setForeground(Color.red);

sancifang=newJButton("x^3");

sancifang.setForeground(Color.red);

tan=newJButton("tan");

tan.setForeground(Color.red);

jiecheng=newJButton("n!

");

jiecheng.setForeground(Color.red);

lg=newJButton("lg");

lg.setForeground(Color.red);

pai=newJButton("∏");

pai.setForeground(Color.red);

panel2.add(sin);

panel2.add(ercifang);

panel2.add(cos);

panel2.add(sancifang);

panel2.add(tan);

panel2.add(jiecheng);

panel2.add(lg);

panel2.add(pai);

seven=newJButton("7");

seven.setForeground(Color.blue);

eight=newJButton("8");

eight.setForeground(Color.blue);

nine=newJButton("9");

nine.setForeground(Color.blue);

jia=newJButton("+");

jia.setForeground(Color.blue);

four=newJButton("4");

four.setForeground(Color.blue);

five=newJButton("5");

five.setForeground(Color.blue);

six=newJButton("6");

six.setForeground(Color.blue);

jian=newJButton("-");

jian.setForeground(Color.blue);

one=newJButton("1");

one.setForeground(Color.blue);

two=newJButton("2");

two.setForeground(Color.blue);

three=newJButton("3");

three.setForeground(Color.blue);

cheng=newJButton("*");

cheng.setForeground(Color.blue);

zero=newJButton("0");

zero.setForeground(Color.blue);

dot=newJButton(".");

dot.setForeground(Color.blue);

equal=newJButton("=");

equal.setForeground(Color.blue);

chu=newJButton("/");

chu.setForeground(Color.blue);

panel3.add(seven);

panel3.add(eight);

panel3.add(nine);

panel3.add(jia);

panel3.add(four);

panel3.add(five);

panel3.add(six);

panel3.add(jian);

panel3.add(one);

panel3.add(two);

panel3.add(three);

panel3.add(cheng);

panel3.add(zero);

panel3.add(dot);

panel3.add(equal);

panel3.add(chu);

//添加按钮事件监听

jiantingnumber=newjianting();

jianting_sfsuanfa=newjianting_sf();

seven.addActionListener(number);

eight.addActionListener(number);

nine.addActionListener(number);

jia.addActionListener(suanfa);

four.addActionListener(number);

five.addActionListener(number);

six.addActionListener(number);

jian.addActionListener(suanfa);

one.addActionListener(number);

two.addActionListener(number);

three.addActionListener(number);

cheng.addActionListener(suanfa);

zero.addActionListener(number);

dot.addActionListener(number);

equal.addActionListener(suanfa);

chu.addActionListener(suanfa);

C.addActionListener(number);

CE.addActionListener(number);

sin.addActionListener(suanfa);

cos.addActionListener(suanfa);

tan.addActionListener(suanfa);

ercifang.addActionListener(suanfa);

sancifang.addActionListener(suanfa);

jiecheng.addActionListener(suanfa);

lg.addActionListener(suanfa);

pai.addActionListener(suanfa);

Backspace.addActionListener(number);

JButtonbtns=newJButton("计算器");

btns.setEnabled(false);//按钮不可被按

btns.setPreferredSize(newDimension(10,10));

panel1.add(btns);

panel4.add(panel1,BorderLayout.NORTH);

panel4.add(panel2,BorderLayout.WEST);

panel4.add(panel3,BorderLayout.CENTER);

this.add(panel4);

this.add(tf,BorderLayout.NORTH);

pack();

this.setResizable(true);

this.setSize(500,250);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);//窗口可关闭

}

classjiantingimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringstr=e.getActionCommand();

if(str.equals("C")){

tf.setText("");

}if(str.equals("CE")){

tf.setText("");

}if(str.equals("←")){

s=tf.getText();

inti=s.length();

if(i>0){

tf.setText(tf.getText().substring(0,i-1));

}

}if(e.getSource()==seven){

tf.setText(tf.getText()+"7");

}if(e.getSource()==eight){

tf.setText(tf.getText()+"8");

}if(e.getSource()==nine){

tf.setText(tf.getText()+"9");

}if(e.getSource()==four){

tf.setText(tf.getText()+"4");

}if(e.getSource()==five){

tf.setText(tf.getText()+"5");

}if(e.getSource()==six){

tf.setText(tf.getText()+"6");

}if(e.getSource()==one){

tf.setText(tf.getText()+"1");

}if(e.getSource()==two){

tf.setText(tf.getText()+"2");

}if(e.getSource()==three){

tf.setText(tf.getText()+"3");

}if(e.getSource()==zero){

tf.setText(tf.getText()+"0");

}if(e.getSource()==dot){

tf.setText(tf.getText()+".");

}

}

}

classjianting_sfimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringstr=e.getActionCommand();

if(str.equals("+")){

s=tf.getText();//获取文本框的值

num1=Double.parseDouble(s);//String类型转换为double型

tf.setText("");//赋值

x=0;

}

elseif(str.equals("-")){

s=tf.getText();

num1=Double.parseDouble(s);

tf.setText("");

x=1;

}

elseif(str.equals("*")){

s=tf.getText();

num1=Double.parseDouble(s);

tf.setText("");

x=2;

}

elseif(str.equals("/")){

s=tf.getText();

num1=Double.parseDouble(s);

tf.setText("");

x=3;

}

elseif(str.equals("sin")){

s=tf.getText();

num1=Double.parseDouble(s);

x=4;

}

elseif(str.equals("cos")){

s=tf.getText();

num1=Double.parseDouble(s);

x=5;

}

elseif(str.equals("tan")){

s=tf.getText();

num1=Double.parseDouble(s);

x=6;

}

elseif(str.equals("n!

")){

s=tf.getText();

num1=Double.parseDouble(s);

num3=1;

for(doublestart=1;start<=num1;start++){

num3=start*num3;

}

x=7;

}

elseif(str.equals("x^2")){

s=tf.getText();

num1=Double.parseDouble(s);

x=8;

}

elseif(str.equals("x^3")){

s=tf.getText();

num1=Double.parseDouble(s);

x=9;

}

elseif(str.equals("∏")){

tf.setText(String.valueOf(Math.PI));

x=10;

}

elseif(str.equals("lg")){

s=tf.getText();

num1=Double.parseDouble(s);

x=11;

}

if(str.equals("=")){

s=tf.getText();

num2=Double.parseDouble(s);

switch(x){

case0:

tf.setText(""+(num1+num2));

break;

case1:

tf.setText(""+(num1-num2));

break;

case2:

tf.setText(""+(num1*num2));

break;

case3:

tf.setText(""+(num1/num2));

break;

case4:

tf.setText(""+(Math.sin(num1*Math.PI/180)));//转换为度数

break;

case5:

tf.setText(""+(Math.cos(num1*Math.PI/180)));

break;

case6:

tf.setText(""+(Math.tan(num1*Math.PI/180)));

break;

case7:

tf.setText(""+num3);

break;

case8:

tf.setText(""+(num1*num1));

break;

case9:

tf.setText(""+(num1*num1*num1));

break;

case10:

num1=Math.PI;

tf.setText(""+num1);

break;

case11:

tf.setText(""+(Math.log(num1)));

break;

}

}

}

}

publicstaticvoidmain(String[]args){

newmyCalucator().setVisible(true);

}

}

 

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

当前位置:首页 > 医药卫生 > 基础医学

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

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