zoukankan      html  css  js  c++  java
  • java代码----------计算器代码

    总结:

    很多不完善——

    package com.rue;
    
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    //实现计算器的功能,我他妈的真是不懂变通。
    
    class Ji extends JFrame implements ActionListener {
    	private static final String CE = null;
    	// 定义按钮
    	StringBuffer str = new StringBuffer();
    	JButton jb_0, jb_1, jb_2, jb_3, jb_4, jb_5, jb_6, jb_7, jb_8, jb_9, jb_jia,
    			jb_jian, jb_cheng, jb_chu, jb_dian, jb_deng, jb_delete, jb_daoshu;
    	JLabel jf;
    	double num1 = 0;
    	double num2 = 0;
    	char c = '';
    	double m;
    	double f;
    	boolean ready = true;
    
    	public Ji() {
    		JPanel jp = new JPanel(new GridLayout(5, 4));// 把所有的按钮组件和文本框放在这个panel里面
    		jf = new JLabel("0.0");
    		jf.setFont(new Font("", 1, 18));
    		jb_daoshu = new JButton("1/x");// /这是求倒数
    		jf.setHorizontalAlignment(JLabel.RIGHT);
    		jb_delete = new JButton("CE");// 设一个删除的按钮
    		jb_0 = new JButton("0");
    		JButton jb_genghao = new JButton("√");
    		jb_1 = new JButton("1");
    		jb_genghao.addActionListener(this);
    		jb_2 = new JButton("2");
    		jb_3 = new JButton("3");
    		jb_4 = new JButton("4");
    		jb_5 = new JButton("5");
    		jb_6 = new JButton("6");
    		jb_7 = new JButton("7");
    		jb_8 = new JButton("8");
    		jb_9 = new JButton("9");
    		jb_jia = new JButton("+");
    		jb_jian = new JButton("-");
    		jb_cheng = new JButton("*");
    		jb_chu = new JButton("/");
    		jb_dian = new JButton(".");
    		jb_deng = new JButton("=");
    		//
    		jb_daoshu.addActionListener(this);
    		jb_delete.addActionListener(this);
    		jb_0.addActionListener(this);
    		jb_1.addActionListener(this);
    		jb_2.addActionListener(this);
    		jb_3.addActionListener(this);
    		jb_4.addActionListener(this);
    		jb_5.addActionListener(this);
    		jb_6.addActionListener(this);
    		jb_7.addActionListener(this);
    		jb_8.addActionListener(this);
    		jb_9.addActionListener(this);
    		jb_jia.addActionListener(this);
    		jb_jian.addActionListener(this);
    		jb_cheng.addActionListener(this);
    		jb_chu.addActionListener(this);
    		jb_dian.addActionListener(this);
    		jb_deng.addActionListener(this);
    
    		JButton jb_Back = new JButton("Back");
    		jb_Back.addActionListener(this);
    
    		jp.add(jb_1);
    		jp.add(jb_2);
    		jp.add(jb_3);
    		jp.add(jb_4);
    		jp.add(jb_5);
    		jp.add(jb_6);
    		jp.add(jb_7);
    		jp.add(jb_8);
    		// this.add(jp);
    
    		jp.add(jb_9);
    		jp.add(jb_daoshu);// 为将按钮添加进
    		jp.add(jb_jia);
    		jp.add(jb_jian);
    		jp.add(jb_cheng);
    		jp.add(jb_chu);
    		jp.add(jb_dian);
    		jp.add(jb_deng);
    		jp.add(jb_0);
    		jp.add(jb_Back);
    		jp.add(jb_delete);
    		jp.add(jb_genghao);
    		this.add(jf, BorderLayout.NORTH);
    		this.add(jp);
    
    		// this.setLayout();
    		this.setVisible(true);
    
    		this.setBounds(100, 20, 230, 300);
    		this.setDefaultCloseOperation(3);
    
    	}
    
    	public double count() {
    		switch (c) {
    		case '+':
    			m = num1 + num2;
    			break;
    		case '-':
    			m = num1 - num2;
    			break;
    		case '*':
    			m = num1 * num2;
    			break;
    		case '/':
    			
    			m = num1 / num2;
    			break;
    		case '√':
    			
    			m = Math.sqrt(num1);
    			break;
    		case 'E':
    			m = '';
    		case 'd':
    			m = 1 / num1;
    		}
    
    		return m;
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		String str = e.getActionCommand();
    		if (str.equals("0") || str.equals(".") || str.equals("1")
    				|| str.equals("2") || str.equals("3") || str.equals("4")
    				|| str.equals("5") || str.equals("6") || str.equals("7")
    				|| str.equals("8") || str.equals("9") || str.equals("1")) {
    			if (ready) {
    				jf.setText(str);
    				ready = false;
    			} else {
    
    				jf.setText(jf.getText() + str);
    				ready = false;
    			}
    
    		} else if (str.equals("+")) {
    			num1 = Double.parseDouble(jf.getText());
    			// count();
    			// ////////////其实可以感觉到操作符有点不正常的。就是操作符设置的问题有点大
    			// if(num1=){}
    			c = '+';
    
    			ready = true;
    		} else if (str.equals("√")) {///////////负号没有平方根,那么这个负数还不是没显示出来的。不是符号了吗?
    			num1 = Double.parseDouble(jf.getText());
    			
    			c = '√';
    			ready = true;
    		} else if (str.equals("CE")) {
    			num1 = Double.parseDouble(jf.getText());
    			// c='c';//这里可以判断按的是哪一个按钮吗?
    			jf.setText("0.0");// 记住这里是要清空,但并不是把lable也全部删除看不见。Label的作用。
    			ready = true;// 当这里为false时,它会与0.0连接一起显示出来,此时设置我ture。它正常了。是不是继续执行下一步的意思呢?
    
    		} else if (str.equals("-")) {
    			num1 = Double.parseDouble(jf.getText());
    			c = '-';
    			ready = true;
    		} else if (str.equals("*")) {
    			num1 = Double.parseDouble(jf.getText());
    			c = '*';
    			ready = true;
    		} else if (str.equals("1/x")) {
    			num1 = Double.parseDouble(jf.getText());
    			// c='';
    			String s = jf.getText().trim();// 这是将文本框内的数据为它的的倒数
    			f = Double.parseDouble(s);// 将字符串转换为double型
    			jf.setText("" + 1 / f);// 这里的m有新的意义、它是double型数据,,这里的m可以设成其他,只要是double型就可以
    
    			ready = true;
    
    		}
    
    		else if (str.equals("/")) {
    			num1 = Double.parseDouble(jf.getText());
    			c = '/';
    
    			ready = true;
    		} else if (str.equals("=")) {
    			num2 = Double.parseDouble(jf.getText());
    
    			jf.setText(count() + "");
    
    			ready = false;
    		}
    
    	}
    
    }
    
    public class Test {
    	public static void main(String[] args) {
    		new Ji();
    	}
    }
    

      

  • 相关阅读:
    MySQL 允许远程连接
    EeePad刷机
    Ubuntu安装Oracle JDK
    Windows Azure Tips
    查看MySQL数据库大小
    Tomcat 7 DBCP 配置(MySQL)
    几个国内的OpenSource镜像站
    好吧,这是我的第一篇文章。
    安卓软件推荐56冰箱IceBox
    ArrayList 冷门方法
  • 原文地址:https://www.cnblogs.com/langlove/p/3505568.html
Copyright © 2011-2022 走看看