Java编写的简易计算器

基于swing组件进行开发:

package calculator_1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.math.*;


public class Count_1 extends JFrame{
	int i;
	/*判断运算数据类型
	 * i=1	加法
	 * i=2	减法
	 * i=3	乘法
	 * i=4	除法
	 * i=5	取余
	 * i=6	求根
	 */
	double a,b;//储存两个计算数的值
	private String[] labels= {
			"C","/","*","X",
			"7","8","9","-",
			"4","5","6","+",
			"1","2","3",".",
			"%","0","q","="
	};//按钮信息
	private JTextField jt=new JTextField(20);
	//输出一个文本框
	
	JButton c[]=new JButton[20];//创建按钮
	Count_1(){
		super("计算器");//填写标题名字
		setBounds(650,300,230,230);//设置窗体大小
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		//设置关闭规则,窗体关闭时结束程序运行
		Container con=getContentPane();//获取窗体容器
		
		final JPanel p1=new JPanel();//设置显示面板
		getContentPane().add(p1,BorderLayout.NORTH);
		//顶层显示
		//result.setHorizontalAlignment(SwingConstants.RIGHT);
		//设置文本的水平对齐方式
		jt.setEditable(false); //只能显示,不能编辑 
		jt.setColumns(18);
		jt.setBorder(null);//去掉文本框边线
		jt.setHorizontalAlignment(JTextField.RIGHT);
		//文本框信息从右边显示
		p1.add(jt);//将显示器添加到面板p1中
		
		JPanel p2=new JPanel(new GridLayout(5,4));//设置按钮面板,并设置布局为5行四列
		getContentPane().add(p2,BorderLayout.CENTER);//设置按键面板显示在窗体底部
		for(int j=0;j<20;j++)
		{
			c[j]=new JButton(labels[j]);//添加按钮信息
			p2.add(c[j]);//添加按钮
		}
		
		setVisible(true);//设置窗体可见
	}
	
	public void Button_action() {
		c[19].setBackground(Color.pink);//改变按钮颜色
		
		c[0].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jt.setText("");//清空文本框信息
			}
		});
		
		c[1].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				i=4;
				if("".equals(jt.getText()))	//判断文本框是否为空
					JOptionPane.showMessageDialog(null, "请先输入除数");
					//若为空,则弹出提示框
				else
				{
					a=Double.parseDouble(jt.getText());//数据转换,将除数的值赋给a
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
					//文本框显示除法符号以及原有符号
				}
			}
		});
		c[2].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				i=3;
				if("".equals(jt.getText()))	//判断文本框是否为空
					JOptionPane.showMessageDialog(null, "请先输入乘数");
					//若为空,则弹出提示框
				else
				{
					a=Double.parseDouble(jt.getText());//数据转换,将乘数的值赋给a
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
					//文本框显示除法符号以及原有符号
				}
			}
		});
		c[3].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))//若文本框不为空,则可执行撤销操作
				{
					String wr=jt.getText();//得到文本框内数据
					jt.setText(wr.substring(0,wr.length()-1));
				}
			}
		});
		c[4].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[5].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[6].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[7].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				i=2;
				if("".equals(jt.getText()))	//判断文本框是否为空
					JOptionPane.showMessageDialog(null, "请先输入减数");
					//若为空,则弹出提示框
				else
				{
					a=Double.parseDouble(jt.getText());
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());//文本框显示减法符号
				}
			}
		});
		c[8].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[9].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[10].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[11].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				i=1;
				if("".equals(jt.getText()))	//判断文本框是否为空
					JOptionPane.showMessageDialog(null, "请先输入加数");
					//若为空,则弹出提示框
				else
				{
					a=Double.parseDouble(jt.getText());
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());//文本框显示加法符号
				}
			}
		});
		c[12].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[13].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[14].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[15].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[16].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				i=5;
				if("".equals(jt.getText()))	//判断文本框是否为空
					JOptionPane.showMessageDialog(null, "请先输入取余数");
					//若为空,则弹出提示框
				else
				{
					a=Double.parseDouble(jt.getText());
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());//文本框显示取余数符号
				}
			}
		});
		c[17].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					jt.setText(jt.getText()+((JButton)e.getSource()).getText());
				else
					jt.setText(((JButton)e.getSource()).getText());
			}
		});
		c[18].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(jt.getText()))
					JOptionPane.showMessageDialog(null, "请先输入开根号");
				else
				{
					i=6;
					jt.setText(((JButton)e.getSource()).getText());//在文本框显示按钮的信息
				}
			}
		});
		c[19].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(i==1)
				{
					String tp=jt.getText();
					int m=tp.indexOf('+');
					String tp1=tp.substring(m+1);
					b=Double.parseDouble(tp1);
					double k=a+b;
					//保留小数点后6位数
					String tp3=String.format("%.6f",k);
					jt.setText(tp3);
				}
				else if(i==2)
				{
					String tp=jt.getText();
					int m=tp.indexOf('-');
					String tp1=tp.substring(m+1);
					b=Double.parseDouble(tp1);
					double k=a-b;
					String tp3=String.format("%.6f",k);
					jt.setText(tp3);
				}
				else if(i==3)
				{
					String tp=jt.getText();
					int m=tp.indexOf('*');
					String tp1=tp.substring(m+1);
					b=Double.parseDouble(tp1);
					double k=a*b;
					String tp3=String.format("%.6f",k);
					jt.setText(tp3);
				}
				else if(i==4)
				{
					String tp=jt.getText();
					int m=tp.indexOf('/');
					String tp1=tp.substring(m+1);
					b=Double.parseDouble(tp1);
					double k=a/b;
					String tp3=String.format("%.6f",k);
					jt.setText(tp3);
				}
				else if(i==5)
				{
					String tp=jt.getText();
					int m=tp.indexOf('%');
					String tp1=tp.substring(m+1);
					b=Double.parseDouble(tp1);
					double k=a%b;
					String tp3=String.format("%.6f",k);
					jt.setText(tp3);
				}
				else if(i==6)
				{
					String tp=jt.getText();
					int m=tp.indexOf('q');
					String tp1=tp.substring(m+1);
					b=Double.parseDouble(tp1);
					if(b>=0)
					{
						double k=Math.sqrt(b);
						String tp3=String.format("%.6f",k);
						jt.setText(tp3);
					}
					else
						JOptionPane.showMessageDialog(null, "开根数必须为正数");
				}
			}
		});
	}
	
	/*main函数*/
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			Count_1  c=new Count_1();//定义一个
			c.Button_action();
	}
}

运行结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值