自己写的一个计算器,极其简单,并且很多功能不完备

来源:互联网 发布:手机淘宝换手机登录 编辑:程序博客网 时间:2024/05/17 06:55

其实贴上来的代码都会有点搞笑,学了Java不过个把月,写的完全不知所云,很多基本知识还是不是很透彻,就当这段路上的勉励站吧,再接再厉!


要是不幸污了您的眼睛,别见怪啊大笑还望求教,指点一下,我也能进步进步,很多东西都是书上学不到的。奋斗


/** * 做一个计算器,使其具有简单的功能 * @author:JeremyJone * @2014.9.24 *  * 做出界面,具有显示面板,同时具有0-9,和加减乘除,还有删除和清空按钮 *  * 按下每个按钮时显示面板都具有相应功能 *  */package Demo1;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class calculator extends JFrame implements ActionListener{String s="";//定义两个中间缓存变量double p1;double p2;//在定义一个加减乘除的符号变量,0 代表无表示,1代表加,2代表减,3代表乘,4代表除int x=0;JTextField text=null;JButton jbc=null;JButton jb1=null;JButton jb2=null;JButton jb3=null;JButton jb4=null;JButton jb5=null;JButton jb6=null;JButton jb7=null;JButton jb8=null;JButton jb9=null;JButton jb0=null;JButton jbdot=null;JButton jbe=null;JButton jbadd=null;JButton jbminus=null;JButton jbtime=null;JButton jbdivide=null;public static void main(String[] args) {// TODO Auto-generated method stubcalculator demo1_1=new calculator();}//构造public calculator(){JPanel mainpanel=new JPanel();JPanel northpanel=new JPanel();text=new JTextField();text.setPreferredSize(new Dimension(135,50)); text.setEnabled(false);text.setFont(new Font("黑体",30,30));text.setHorizontalAlignment(JTextField.RIGHT);   //右对齐jbc=new JButton("C");jbc.setPreferredSize(new Dimension(50,50));jbc.addActionListener(this);jbc.setActionCommand("C");northpanel.add(text);northpanel.add(jbc);JPanel buttonpanel=new JPanel(new GridLayout(4,3,10,10));jb1=new JButton("1");jb1.addActionListener(this);jb1.setActionCommand("1");jb2=new JButton("2");jb2.addActionListener(this);jb2.setActionCommand("2");jb3=new JButton("3");jb3.addActionListener(this);jb3.setActionCommand("3");jb4=new JButton("4");jb4.addActionListener(this);jb4.setActionCommand("4");jb5=new JButton("5");jb5.addActionListener(this);jb5.setActionCommand("5");jb6=new JButton("6");jb6.addActionListener(this);jb6.setActionCommand("6");jb7=new JButton("7");jb7.addActionListener(this);jb7.setActionCommand("7");jb8=new JButton("8");jb8.addActionListener(this);jb8.setActionCommand("8");jb9=new JButton("9");jb9.addActionListener(this);jb9.setActionCommand("9");jb0=new JButton("0");jb0.addActionListener(this);jb0.setActionCommand("0");jbdot=new JButton(".");jbdot.addActionListener(this);jbdot.setActionCommand("dot");jbe=new JButton("=");jbe.addActionListener(this);jbe.setActionCommand("e");JPanel mathpanel=new JPanel(new GridLayout(4,1,10,10));jbadd=new JButton("+");jbadd.addActionListener(this);jbadd.setActionCommand("add");jbminus=new JButton("-");jbminus.addActionListener(this);jbminus.setActionCommand("minus");jbtime=new JButton("*");jbtime.addActionListener(this);jbtime.setActionCommand("time");jbdivide=new JButton("/");jbdivide.addActionListener(this);jbdivide.setActionCommand("divide");buttonpanel.add(jb7);buttonpanel.add(jb8);buttonpanel.add(jb9);buttonpanel.add(jb4);buttonpanel.add(jb5);buttonpanel.add(jb6);buttonpanel.add(jb1);buttonpanel.add(jb2);buttonpanel.add(jb3);buttonpanel.add(jbdot);buttonpanel.add(jb0);buttonpanel.add(jbe);mathpanel.add(jbadd);mathpanel.add(jbminus);mathpanel.add(jbtime);mathpanel.add(jbdivide);mainpanel.add(northpanel);mainpanel.add(buttonpanel,BorderLayout.WEST);mainpanel.add(mathpanel,BorderLayout.EAST);text.setText(s);this.add(mainpanel);this.setLocation(700, 300);this.setSize(220, 250);this.setResizable(false);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}@Overridepublic void actionPerformed(ActionEvent e)  {// TODO Auto-generated method stubtry {if(e.getActionCommand().equals("C")){s="";text.setText(s);}else if(e.getActionCommand().equals("1")){s=s+"1";text.setText(s);}else if(e.getActionCommand().equals("2")){s=s+"2";text.setText(s);}else if(e.getActionCommand().equals("3")){s=s+"3";text.setText(s);}else if(e.getActionCommand().equals("4")){s=s+"4";text.setText(s);}else if(e.getActionCommand().equals("5")){s=s+"5";text.setText(s);}else if(e.getActionCommand().equals("6")){s=s+"6";text.setText(s);}else if(e.getActionCommand().equals("7")){s=s+"7";text.setText(s);}else if(e.getActionCommand().equals("8")){s=s+"8";text.setText(s);}else if(e.getActionCommand().equals("9")){s=s+"9";text.setText(s);}else if(e.getActionCommand().equals("0")){s=s+"0";text.setText(s);}else if(e.getActionCommand().equals("dot")){if (text.getText().contains(".")){s=s;text.setText(s);}else{s=s+".";text.setText(s);}}else if(e.getActionCommand().equals("e")){switch(x){case 0:text.setText(s);break;case 1:p2=Double.valueOf(text.getText());s=String.valueOf(p1+p2);text.setText(s);break;case 2:p2=Double.valueOf(text.getText());s=String.valueOf(p1-p2);text.setText(s);break;case 3:p2=Double.valueOf(text.getText());s=String.valueOf(p1*p2);text.setText(s);break;case 4:p2=Double.valueOf(text.getText());if(p2!=0){s=String.valueOf(p1/p2);text.setText(s);}else{s="ERROR";text.setText(s);}break;}}else if(e.getActionCommand().equals("add")){p1=Double.valueOf(text.getText());s="";x=1;}else if(e.getActionCommand().equals("minus")){p1=Double.valueOf(text.getText());s="";x=2;}else if(e.getActionCommand().equals("time")){p1=Double.valueOf(text.getText());s="";x=3;}else if(e.getActionCommand().equals("divide")){p1=Double.valueOf(text.getText());s="";x=4;}} catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}}}

0 0