Java 实现简单计算器

 

package 计算器;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class Calculator extends JFrame implements ActionListener{
	JFrame frame;
	JButton num0;
	JButton num1;
	JButton num2;
	JButton num3;
	JButton num4;
	JButton num5;
	JButton num6;
	JButton num7;
	JButton num8;
	JButton num9;
	JButton Back;
	JButton C;
	JButton minusButton;
	JButton multiplyButton;
	JButton divideButton;
	JButton plusButton;
	JButton dotButton;
	JButton equalButton;
	JTextField text;
	JMenuItem exitItem;
	JMenuItem aboutItem;
	JMenuItem fileItem;
	boolean clicked=true;
	boolean clear=true;
	int all=0;
	double pervious;
	String fuhao;
    int first;
		
	public Calculator() {
		setTitle("计算器");
		setSize(420,230);
		setLocationRelativeTo(null);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		text =new JTextField(30);
		text.setHorizontalAlignment(JTextField.LEFT);//设置文本的水平对齐方式
		JPanel p1=new JPanel();
		JPanel p2=new JPanel();
		JPanel p3=new JPanel();
		//界面布局
		JMenuBar mainMenu=new JMenuBar();//菜单条
		setJMenuBar(mainMenu);
		JMenu editMenu=new JMenu("文件");
		JMenu helpMenu=new JMenu("帮助");
	    aboutItem=new JMenuItem("关于");
		mainMenu.add(editMenu);
		mainMenu.add(helpMenu);
		helpMenu.add(aboutItem);
		aboutItem.addActionListener(this);
		fileItem=new JMenuItem("文件");
	    exitItem=new JMenuItem("退出");
	    fileItem.addActionListener(this);
		exitItem.addActionListener(this);
		editMenu.add(exitItem);
		editMenu.add(fileItem);
			
		getContentPane().add(p1,"North");
		getContentPane().add(p2,"Center");
		getContentPane().add(p3,"South");
		p1.setLayout(new GridLayout(1,1));//网格布局
		p2.setLayout(new GridLayout(1,2));
		p3.setLayout(new GridLayout(4,4));
		plusButton=new JButton("+");
		minusButton=new JButton("-");
		multiplyButton=new JButton("*");
		divideButton=new JButton("/");
		equalButton=new JButton("=");
	    dotButton=new JButton(".");
		num0=new JButton("0");
		num1=new JButton("1");
		num2=new JButton("2");
		num3=new JButton("3");
		num4=new JButton("4");
		num5=new JButton("5");
		num6=new JButton("6");
		num7=new JButton("7");
		num8=new JButton("8");
		num9=new JButton("9");
		p1.add(text);
		text.setEditable(false);
		text.setBackground(Color.yellow);
		Back=new JButton("Back");
		C=new JButton("C");	
		p2.add(Back);
		Back.addActionListener(this);
		p2.add(C);
		C.addActionListener(this);
		p3.add(num7);
		num7.addActionListener(this);
		p3.add(num8);
		num8.addActionListener(this);
		p3.add(num9);
		num9.addActionListener(this);
		p3.add(plusButton);
		plusButton.addActionListener(this);
		p3.add(num4);
		num4.addActionListener(this);
		p3.add(num5);
		num5.addActionListener(this);
		p3.add(num6);
		num6.addActionListener(this);
		p3.add(minusButton);
		minusButton.addActionListener(this);
		p3.add(num1);
		num1.addActionListener(this);
		p3.add(num2);
		num2.addActionListener(this);
		p3.add(num3);
		num3.addActionListener(this);
		p3.add(multiplyButton);
		multiplyButton.addActionListener(this);	
	    p3.add(num0); 
	    num0.addActionListener(this); 
	    p3.add(equalButton);
	    equalButton.addActionListener(this); 
	    p3.add(dotButton);
	    dotButton.addActionListener(this); 	   
	    p3.add(divideButton);
	    divideButton.addActionListener(this);
		setVisible(true);
		addWindowListener(new WindowDestoryer());		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(first == 1) {
			text.setText("");
			first=0;
		}
		Object temp=e.getSource();
		if(temp == fileItem) {
			JOptionPane.showMessageDialog(null, "欢迎使用","",JOptionPane.INFORMATION_MESSAGE);
		}
		if(temp == exitItem) {
			System.exit(0);
		}
		if(temp == aboutItem) {
			JOptionPane.showMessageDialog(null, "请查看帮助文档","帮助",JOptionPane.INFORMATION_MESSAGE);
		}
			if(temp == Back) {
				String s=text.getText();
				text.setText("");
				for(int i=0;i<s.length()-1;i++) {
					char a=s.charAt(i);
					text.setText(text.getText()+a);
					
				}
			}
			if(temp==C) {
				text.setText("0.");
				clear=true;
				first=1;
			}
			if(temp==num0) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"0");
			}
			if(temp==num1) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"1");
				clear=true;
			}
			if(temp==num2) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"2");
				clear=true;
			}
			if(temp==num3) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"3");
				clear=true;
			}
			if(temp==num4) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"4");
				clear=true;
			}
			if(temp==num5) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"5");
				clear=true;
			}
			if(temp==num6) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"6");
				clear=true;
			}
			if(temp==num7) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"7");
				clear=true;
			}
			if(temp==num8) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"8");
				clear=true;
			}
			if(temp==num9) {
				if(clear==false)
					text.setText("");
				text.setText(text.getText()+"9");
				clear=true;
			}
			//判断是否有小数点
			if(temp==dotButton) {
			    clicked=true;
			    for(int i=0;i<text.getText().length();i++) {
			    	if('.'==text.getText().charAt(i)) {
			    		clicked=false;
			    		break;
			    	}
			    	if(clicked==true) {
						text.setText(text.getText()+".");
					}		    	
			}
		}
			try {
	    		if(temp==plusButton) {
	    			pervious=Double.parseDouble(text.getText());
	    			fuhao="+";
	    			clear=false;
	    		}
	    		if(temp==minusButton) {
	    			pervious=Double.parseDouble(text.getText());
	    			fuhao="-";
	    			clear=false;
	    		}
	    		if(temp==multiplyButton) {
	    			pervious=Double.parseDouble(text.getText());
	    			fuhao="*";
	    			clear=false;
	    		}
	    		if(temp==divideButton) {
	    			pervious=Double.parseDouble(text.getText());
	    			fuhao="/";
	    			clear=false;
	    		}
	    		if(temp == equalButton) {
	    			double next= Double.parseDouble(text.getText());
	    			text.setText("");
	    			if(fuhao=="+") {
	    				text.setText(pervious + next + "");
	    			}
	    			if(fuhao=="-") {
	    				text.setText(pervious - next + "");
	    			}
	    			if(fuhao=="*") {
	    				text.setText(pervious * next + "");
	    			}
	    			if(fuhao=="/") {
	    				if(next==0) {
	    					text.setText("除数不能为0");
	    				}
	    				else {
	    				text.setText(pervious / next + "");
	    				clear=false;
	    				}
	    			}
	    			
	    		}
	    	}
	    	catch(Exception e1) {
	    		text.setText("wrong");
	    		clear=false;
	    	}
	    }
			
	
	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		Calculator cal=new Calculator();
		
	}
}

 关闭窗口:

package 计算器;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class WindowDestoryer extends WindowAdapter {
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		System.exit(0);
	}

}

结果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值