用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

计算器

2018-04-16 作者: Jalyce_ge举报

[java]代码库

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.NoRouteToHostException;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.xml.soap.Text;

public class JiSuanQi   extends JFrame implements ActionListener,KeyListener,MouseListener{
	String  str1="";
	JPanel panel=new JPanel();
	JTextField Text=new JTextField(21);
	JPanel buttonPanel=new JPanel();
	LinkedList<Double> list=new LinkedList<Double>();
	LinkedList<Character> oplist=new LinkedList<Character>();
	Double ms;
	public static void main(String[] args) {

		new JiSuanQi();
	}
	public JiSuanQi() {


		
		Text.setEditable(false);
		Text.setBackground(Color.GRAY);
		Text.setFont(Text.getFont().deriveFont(Font.BOLD,(float) 32.0));
		Text.setForeground(Color.WHITE);   // 前景色,是设置文本显示颜色
		Text.setHorizontalAlignment(JTextField.RIGHT);
		//		Text.setEditable(false);



		JButton[] buttons=new JButton[42];
		String[] name=new String[] {"sin","sinh","sqrt","%","log","log10","PI","cos","cosh","sqrt3","MS","MR","MC","C",
				"tan","tanh","1/x","7","8","9","+","asin","asinh","x^2","4","5","6","-","acos","acosh","x^3","1","2","3","*",
				"atan","atanh","x^y",".","0","=","/"};
		for(int i=0;i<=buttons.length-1;i++) {
			buttons[i]=new JButton(name[i]);
			buttons[i].setSize(67, 67);
			buttonPanel.setLayout(new GridLayout(6,7,5,5));
			buttonPanel.add(buttons[i]);
			buttons[i].setBackground(Color.GRAY);
			buttons[i].setForeground(Color.WHITE);
			buttons[i].addActionListener(this);
			buttons[i].addKeyListener(this);
			buttons[i].addMouseListener(this);

			buttons[i].addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent e) {
					Object sourse=e.getSource();
					if(sourse.getClass()==JButton.class) {
						JButton button=(JButton)sourse;
						String b=button.getText();
						buttonEvent(b,button);   //用此函数实现按钮的功能

					}

				}

			});
		}
		panel.add(Text);
		this.setLayout( new BorderLayout());
		this.add(panel,BorderLayout.NORTH);
		this.add(buttonPanel, BorderLayout.CENTER);
		this.setTitle("计算器");
		this.setSize(602, 500);
		this.setLocation(700, 300);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void buttonEvent(String b,JButton button) {
		if(b!="="&&b!="MC"&&b!="MR"&&b!="MS"&&b!="C") {   //用于实现这些字符不在文本框中显示
							str1=str1+button.getText();
							TextShow(str1);
						}
						if(b=="MS") {
							ms=new Double(str1);
							str1="";
							TextShow(str1);
						}
						if(b=="MR") {
							str1+=ms;
							TextShow(str1);
						}
						if(b=="MC") {
							ms=null;
							str1="";
							TextShow(str1);
						}
						if(b=="C") {
							str1="";
							TextShow(str1);
						}
						if(b=="=") {
							try {
								TextGet(str1);	
							//							System.out.println(str1);
							Operation();
							String a=list.get(0).toString();
							TextShow(a);
							str1=a;
							list.clear();
							//															for(int i=0;i<list.size();i++) {
							//																System.out.println(list.get(i));
							//															}
							//															System.out.println(a);

							}catch(Exception ee) {
								TextShow("输入错误");
							}
							
						}
	}
	public void TextShow(String ts) {
		Text.setText(ts);
	}
	
	/*
	 * TextGet为了实现将  tg字符串 分开,数值为数值, 操作符为操作符
	 */
	public void TextGet(String tg) {
		Double doubletemp;
		String temp="";
		for(int index=0;index<=tg.length()-1;index++) {

			if(tg.charAt(index)!='+'&&tg.charAt(index)!='-'&&tg.charAt(index)!='*'&&tg.charAt(index)!='/') {
				temp+=tg.charAt(index);
			}else {
				if(temp!="") {    // temp 为空时 不能加入list中
					doubletemp=new Double(temp);
				list.add(doubletemp);
				temp="";
				oplist.add(tg.charAt(index));	 
				}else {
					break;
				}
			}
		}
		doubletemp=new Double(temp);
		list.add(doubletemp);
		temp="";
	}
	public  void Operation() {
			while(list.size()!=1) {
				if(oplist.contains('*')||oplist.contains('/')) {
					for (int index = 0; index <= oplist.size() - 1; index++) {
						if (oplist.get(index) == '*') {
							Double t = list.get(index) * list.get(index + 1);
							list.remove(index + 1);   // 移除index+1处的值
							list.set(index, t);    // 将t的值替换index的值
							oplist.remove(index);  
						}
						else if (oplist.get(index) == '/') {
							Double t = list.get(index) / list.get(index + 1);
							list.remove(index + 1);
							list.set(index, t);
							oplist.remove(index);
						}
					}
				}else {
					for (int index = 0; index <= oplist.size() - 1; index++) {
						if (oplist.get(index) == '+') {
							Double t = list.get(index) + list.get(index + 1);
							list.remove(index + 1);
							list.set(index, t);
							oplist.remove(index);
						}
						else if (oplist.get(index) == '-') {
							Double t = list.get(index) - list.get(index + 1);
							list.remove(index + 1);
							list.set(index, t);
							oplist.remove(index);
						}
					}
				}
			}
		}

	

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseEntered(MouseEvent event) {
		// TODO Auto-generated method stub
		Object sourse=event.getSource();
		if(sourse.getClass()==JButton.class) {
			JButton button=(JButton)sourse;
			button.setBackground(Color.LIGHT_GRAY);
		}

	}
	@Override
	public void mouseExited(MouseEvent event) {
		// TODO Auto-generated method stub
		Object sourse=event.getSource();
		if(sourse.getClass()==JButton.class) {
			JButton button=(JButton)sourse;
			button.setBackground(Color.GRAY);
		}

	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void actionPerformed(ActionEvent e) {
	}

}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...