java编的简单计算器

时间:2014-07-27 09:43:32
【文件属性】:

文件名称:java编的简单计算器

文件大小:6KB

文件格式:JAVA

更新时间:2014-07-27 09:43:32

计算器

一个java编译的最简单的计算器 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jisuan extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 5823423246304135749L; JPanel cp=new JPanel(); JPanel cp1=new JPanel(); Double nu1,nu2; ImageIcon ii=new ImageIcon("images/2_marcblue.jpg"); JPanel cp2=new JPanel(); JLabel jl=new JLabel(); String[] Jlabel = {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//声明标签数组label1存放按钮上的标签 JButton[] b; JButton h=new JButton("c"); //private JButton buttonWithTitle=new JButton("Button with Title",new ImageIcon("src/anwendung/tree.jpg")); private JButton c=new JButton("后退",ii); JButton pf=new JButton("x^y"); int j=0,n=0,a=0,g=0; //Button[] b=new Button[10]; //JTextArea jt=new JTextArea(1,34); JTextField jt=new JTextField("0",30); //JLabel jt=new JLabel(); String st,st1,st2; Boolean t=true; public jisuan(){ setTitle("计算器"); setSize(220,220); //this.setbackgroundImage = new ImageIcon("images/6.21.bmp"); cp=(JPanel)this.getContentPane(); //this.setExtendedState(HIDE_ON_CLOSE); this.setUndecorated(false); this.setResizable(false);//窗口固定不变。 //cp.setEnabled(false); this.addWindowListener(new WindowAdapter(){//给窗口f添加窗口事件监听器 public void windowClosing(WindowEvent eve){ System.exit(0); } }); cp.setLayout(new BorderLayout()); cp.add(jt,BorderLayout.NORTH); cp.add(cp1,BorderLayout.CENTER); //cp.add(cp2,BorderLayout.SOUTH); //cp1.setLayout(new GridLayout(3,4)); cp1.setLayout(null); //cp.add(jt); jt.setHorizontalAlignment(JTextField.RIGHT);//右边显示。 jt.setFont(new Font("Serif",Font.BOLD|Font.ITALIC,15));//设置字体的大小和显示方式; jt.setBackground(Color.white);//背景白色 jt.setEditable(false); //jt.setEnabled(true); b = new JButton[Jlabel.length]; for(int i=0;i1){ nu1=nu1*num;nu--;} jt.setText(String.valueOf(nu1)); } j=0; st1=e.getActionCommand(); } } else{ st1=e.getActionCommand(); jt.setText("0"); j=0;a=0;n=0;g=0; } t=false; } else if("=".equals(e.getActionCommand())){ if("=".equals(String.valueOf(st.charAt(st.length()-1)))) a=1; else nu2=Double.parseDouble(jt.getText()); System.out.println(nu2); if(a==1){ //nu2=Double.parseDouble(st); if(st1.equals("+")){ nu1=nu1+nu2; jt.setText(String.valueOf(nu1)); } else if(st1.equals("-")){ nu1=nu1-nu2; jt.setText(String.valueOf(nu1)); } else if(st1.equals("*")){ nu1=nu1*nu2; jt.setText(String.valueOf(nu1)); } else if(st1.equals("/")){ nu1=nu1/nu2; jt.setText(String.valueOf(nu1)); } else if(st1.equals("x^y")){ Double num=nu1; for(;nu2>1;nu2--) nu1=nu1*num; jt.setText(String.valueOf(nu1)); } } a=0;g=1;t=true;st=e.getActionCommand(); } else if("x^y".equals(e.getActionCommand())){ if(t=true){ nu1=Double.parseDouble(jt.getText()); j=0; st1=e.getActionCommand(); a=1; } } } }


网友评论

  • 还不错,容易理解
  • 看着还行,没跑起来
  • 简单易理解~帮助学习