简易型科学计算器费下载.doc

上传人:scccc 文档编号:12965720 上传时间:2021-12-08 格式:DOC 页数:10 大小:50.50KB
返回 下载 相关 举报
简易型科学计算器费下载.doc_第1页
第1页 / 共10页
简易型科学计算器费下载.doc_第2页
第2页 / 共10页
简易型科学计算器费下载.doc_第3页
第3页 / 共10页
简易型科学计算器费下载.doc_第4页
第4页 / 共10页
简易型科学计算器费下载.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《简易型科学计算器费下载.doc》由会员分享,可在线阅读,更多相关《简易型科学计算器费下载.doc(10页珍藏版)》请在三一文库上搜索。

1、简易型科学计算器import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.DecimalFormat;import javax.swing.AbstractButton;import javax.swing.BorderFactor

2、y;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JTextField;import java.lang.Math;public class myCalucator extends JFrameprivate JPanel panel1,panel2,panel3,panel4;pri

3、vate JTextField tf;private JMenu menu1,menu2;private JMenuItem seeItem1,seeItem2,help1,help2;private JMenuBar myBar;private JButton Backspace,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;String s;int x;double num1,

4、num2,num3;public myCalucator()super("简易型科学计算器");this.setLayout(new BorderLayout(10,5);panel1=new JPanel(new GridLayout(1,3,10,10);panel2=new JPanel(new GridLayout(4,2,5,5);panel3=new JPanel(new GridLayout(4,4,5,5);panel4=new JPanel(new BorderLayout(5,5);/* * 菜单栏 */myBar=new JMenuBar();menu

5、1=new JMenu("查看");menu2=new JMenu("帮助");menu1.setFont(new Font("宋体",Font.PLAIN,12);menu2.setFont(new Font("宋体",Font.PLAIN,12);/* * 查看栏 */seeItem1=new JMenuItem("标准型");seeItem2=new JMenuItem("科学型");seeItem1.setFont(new Font("宋体",Fo

6、nt.PLAIN,12);seeItem2.setFont(new Font("宋体",Font.PLAIN,12);/* * 帮助栏 */help1=new JMenuItem("查看帮助");help2=new JMenuItem("关于计算器");help1.setFont(new Font("宋体",Font.PLAIN,12);help2.setFont(new Font("宋体",Font.PLAIN,12);menu1.add(seeItem1);menu1.add(seeItem

7、2);menu2.add(help1);menu2.add(help1);myBar.add(menu1);myBar.add(menu2);this.setJMenuBar(myBar);/* * 文本域 */tf=new JTextField();tf.setEditable(false);tf.setBackground(Color.white);tf.setText("");t(JTextField.RIGHT);tf.setBorder(BorderFactory.createLoweredBevelBorder();init();/对计算器进行初始化public

8、 void init()Backspace=new JButton("");Backspace.setForeground(Color.blue);CE=new JButton("CE");CE.setForeground(Color.blue);C=new JButton("C");C.setForeground(Color.blue);panel1.add(Backspace);panel1.add(CE);panel1.add(C);sin=new JButton("sin");sin.setForegrou

9、nd(Color.red);ercifang=new JButton("x2");ercifang.setForeground(Color.red);cos=new JButton("cos");cos.setForeground(Color.red);sancifang=new JButton("x3");sancifang.setForeground(Color.red);tan=new JButton("tan");tan.setForeground(Color.red);jiecheng=new JButt

10、on("n!");jiecheng.setForeground(Color.red);lg=new JButton("lg");lg.setForeground(Color.red);pai=new JButton("");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);pa

11、nel2.add(pai);seven=new JButton("7");seven.setForeground(Color.blue);eight=new JButton("8");eight.setForeground(Color.blue);nine=new JButton("9");nine.setForeground(Color.blue);jia=new JButton("+");jia.setForeground(Color.blue);four=new JButton("4");

12、four.setForeground(Color.blue);five=new JButton("5");five.setForeground(Color.blue);six=new JButton("6");six.setForeground(Color.blue);jian=new JButton("-");jian.setForeground(Color.blue);one=new JButton("1");one.setForeground(Color.blue);two=new JButton("

13、;2");two.setForeground(Color.blue);three=new JButton("3");three.setForeground(Color.blue);cheng=new JButton("*");cheng.setForeground(Color.blue);zero=new JButton("0");zero.setForeground(Color.blue);dot=new JButton(".");dot.setForeground(Color.blue);equal=

14、new JButton("=");equal.setForeground(Color.blue);chu=new JButton("/");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);(jian);panel3.add(one);panel3.add(two);panel3.add(three);pane

15、l3.add(cheng);panel3.add(zero);panel3.add(dot);panel3.add(equal);panel3.add(chu);/添加按钮事件监听jianting number=new jianting();jianting_sf suanfa=new jianting_sf();dActionListener(number);eight.addActionListener(number);nine.addActionListener(number);jia.addActionListener(suanfa);four.addActionListener(nu

16、mber);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.addActionLi

17、stener(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

18、.addActionListener(suanfa);pai.addActionListener(suanfa);Backspace.addActionListener(number);JButton btns=new JButton("计算器");btns.setEnabled(false);/按钮不可被按btns.setPreferredSize(new Dimension(10,10);panel1.add(btns);panel4.add(panel1,BorderLayout.NORTH);panel4.add(panel2,BorderLayout.WEST);

19、panel4.add(panel3,BorderLayout.CENTER);this.add(panel4);d(tf,BorderLayout.NORTH);pack();this.setResizable(true);this.setSize(500,250);this.setDefaultCloseOperation(EXIT_ON_CLOSE);/窗口可关闭class jianting implements ActionListenerpublic void actionPerformed(ActionEvent e)nCommand();if(str.equals("C&

20、quot;)tf.setText("");if(str.equals("CE")tf.setText("");if(str.equals("")s=tf.getText();int i=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.getTe

21、xt()+"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.ge

22、tText()+"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()+".");class jianting_sf implements Actio

23、nListenerpublic void actionPerformed(ActionEvent e)String str=e.getActionCommand();if(str.equals("+")s=tf.getText();/获取文本框的值num1=Double.parseDouble(s);/String类型转换为double型tf.setText("");/赋值x=0;else if(str.equals("-")s=tf.getText();num1=Double.parseDouble(s);tf.setText(&q

24、uot;");x=1;else if(str.equals("*")s=tf.getText();num1=Double.parseDouble(s);tf.setText("");x=2;else if(str.equals("/")s=tf.getText();num1=Double.parseDouble(s);tf.setText("");x=3;else if(str.equals("sin")s=tf.getText();num1=Double.parseDouble(s)

25、;x=4;else if(str.equals("cos")s=tf.getText();num1=Double.parseDouble(s);x=5;else if(str.equals("tan")s=tf.getText();num1=Double.parseDouble(s);x=6;else if(str.equals("n!")s=tf.getText();num1=Double.parseDouble(s);num3=1;for(double start=1;start<=num1;start+)num3=star

26、t*num3;x=7;else if(str.equals("x2")s=tf.getText();num1=Double.parseDouble(s);x=8;else if(str.equals("x3")s=tf.getText();num1=Double.parseDouble(s);x=9;else if(str.equals("") tf.setText(String.valueOf(Math.PI);x=10;else if(str.equals("lg")s=tf.getText();num1=Do

27、uble.parseDouble(s);x=11;if(str.equals("=")s=tf.getText();num2=Double.parseDouble(s);switch(x)case 0:tf.setText(""+(num1+num2);break;case 1:tf.setText(""+(num1-num2);break;case 2:tf.setText(""+(num1*num2);break;case 3:tf.setText(""+(num1/num2);break;

28、case 4:tf.setText(""+(Math.sin(num1*Math.PI/180);/转换为度数break;case 5:tf.setText(""+(Math.cos(num1*Math.PI/180);break;case 6:tf.setText(""+(Math.tan(num1*Math.PI/180);break;case 7:tf.setText(""+num3);break;case 8:tf.setText(""+(num1*num1);break;case 9:tf.setText(""+(num1*num1*num1);break;case 10:num1=Math.PI;tf.setText(""+num1);break;case 11:tf.setText(""+(Math.log(num1);break;public static void main(Stringargs)new myCalucator().setVisible(true);

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

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1