奋战一下午写的一个简单计算器

package test;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener{
	String[] key = {"7","8","9","/","4","5","6","*","1","2","3","-",".","0","=","+"};
	String[] cmd = {"DEL", "C" };
	JButton[] keyb= new JButton[key.length];
	JPanel keybpanel=new JPanel(new GridLayout(4,4,3,3));
	JPanel cmdpanel=new JPanel();
	JButton cmdb[] = new JButton[cmd.length];
	JTextField display = new JTextField(15);
	
	JMenuBar menu=new JMenuBar();
	JMenu xuanxiang,bangzhu=null;
	JMenuItem a1,a2,b1,b2=null;
	
	float first,second,result;
	boolean check=true;
	String operate;
	boolean record=false;
	
	public static void main(String[] args) {
		new Calculator();
	}
	
	
	public Calculator()
	{
		
		xuanxiang=new JMenu("选项");
		bangzhu=new JMenu("帮助");
		a1=new JMenuItem("切换");
		a2=new JMenuItem("退出");
		b1=new JMenuItem("查看帮助");
		b2=new JMenuItem("关于");
		xuanxiang.add(a1);
		xuanxiang.add(a2);
		bangzhu.add(b1);
		bangzhu.add(b2);
		menu.add(xuanxiang);
		menu.add(bangzhu);
		for(int i=0;i<key.length;i++)
		{
			keyb[i]=new JButton(key[i]);
			keyb[i].setForeground(Color.blue);
			keybpanel.add(keyb[i]);
		}
		display.setEditable(false);
		for(int i=0;i<cmd.length;i++)
		{
			cmdb[i]=new JButton(cmd[i]);
			cmdb[i].setForeground(Color.red);
			cmdpanel.add(cmdb[i]);
		}
		
		this.setJMenuBar(menu);
		this.add("North",display);
		this.add("Center",cmdpanel);
		this.add("South",keybpanel);
		this.setLocation(500,200);
		this.setSize(274, 240);
		this.setTitle("计算器");
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		
		a1.addActionListener(this);
		a2.addActionListener(this);
		b1.addActionListener(this);
		b2.addActionListener(this);
		for(int i=0;i<key.length;i++)
			keyb[i].addActionListener(this);
		for(int i=0;i<cmd.length;i++)
			cmdb[i].addActionListener(this);
	}
	

	public void actionPerformed(ActionEvent e) {
		for(int i=0;i<key.length;i++)
		{
			if(e.getSource()==keyb[i]&&i!=14&&i!=3&&i!=7&&i!=11&&i!=15)
			{
				
				if(record==true&&e.getActionCommand()==".")
					JOptionPane.showMessageDialog(null, "非法输入!");
				else
				display.setText(display.getText()+key[i]);
				if(e.getActionCommand()==".")
				{
					record=true;
				}
			}	
		}
		
		
		if(e.getSource()==keyb[7]||e.getSource()==keyb[3]||e.getSource()==keyb[11]||e.getSource()==keyb[15])
		{
			if(check==true)
			{
				first=Float.valueOf(display.getText()).floatValue();
				display.setText("");
			}
			operate=e.getActionCommand();
			check=false;
		}
			
		
		if(e.getSource()==keyb[14])
			{
				if(check==false)
				{
					second=Float.valueOf(display.getText()).floatValue();
					if(operate.equals("+"))
						result=first+second;
					else if(operate.equals("-"))
						result=first-second;
					else if(operate.equals("/"))
						result=first/second;
					else if(operate.equals("*"))
						result=first*second;
					display.setText(String.valueOf(result));
				}
				else
				{
					JOptionPane.showMessageDialog(null, "您的输入存在错误!");
				}
			}
		
		
		if(e.getSource()==cmdb[1])	
		{
			check=true;
			record=false;
			display.setText("");
		}
		
		
		if(e.getSource()==cmdb[0])
			display.setText("");
		
		
		if(e.getSource()==a1)
			JOptionPane.showMessageDialog(null, "暂时无法切换");
		
		
		if(e.getSource()==a2)
			System.exit(0);
		
		
		if(e.getSource()==b1)
			JOptionPane.showMessageDialog(null, "");
		
		
		if(e.getSource()==b2)
			JOptionPane.showMessageDialog(null, "版本号:v1.0\n作者:康彪\n学号:2013220202022\njava记事本实验作业");
		
	}
	
}

 

下面是运行效果:





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值