⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.java

📁 简易的JAVA计算器
💻 JAVA
字号:

/**
 * Created by IntelliJ IDEA.
 * User: student
 * Date: 2004-11-11
 * Time: 10:17:23
 * To change this template use Options | File Templates.
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
        private Container content;//声明面版
        private GridBagLayout bagLayout;//声明GridBagLayout的对象
        private GridBagConstraints constraints;//声明GridBagConstaints()对象
        JMenuBar bar=new JMenuBar();//添加MenuBar类的对象,注意Menu类只能在Frame框架中使用
        JMenu mu=new JMenu("Option");//添加Option菜单
        JMenuItem m=new JMenuItem("Exit");//添加Exit菜单选项
        JButton bt0,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9;//按钮0-10和+-*/=
		JButton btEqual,btAdd,btMin,btMul,btDiv;
		JButton btBackspace,btreset;//退格和清零
		JLabel lb=new JLabel("简易计算器",0);
        JTextField text1 = new JTextField("0");
        String sop[]={"",""};//sop[0]存放按下操作数(+-*/)之前输入的数,sop[1]存放按下操作数之后的数.
	int i=0;//用作sop数组的下标
	char op;//记录操作符 "+ - * /"
	boolean flag=false;//判断是否第一次按下+,-,*,/
	boolean doubleClick=true;//限制按钮按下的次数,有的可以连续按,有的限制为只按一次,比如"="
	boolean twoOpNum=false;//有两个操作数才能进行"="操作
	String result="";//保存运算结果
  public Calculator()
  {
        super("Calculator");
        text1.setHorizontalAlignment(JTextField.RIGHT);//文本域的内容为右对齐
        content=getContentPane() ;//获得内容面版
        bagLayout=new GridBagLayout();//创建对象
        content.setLayout(bagLayout);
        constraints=new GridBagConstraints();
        mu.add(m);
        bar.add(mu);
        this.setJMenuBar(bar);
        constraints.fill=GridBagConstraints.BOTH;//表示组件水平方向和竖直方向都可占满整个显示区域
        addComponent(text1,1,0,4,1);//添加组件text1其布局为:第1行,第0列,占据1行4列的位置
        constraints.fill=GridBagConstraints.BOTH;
        addComponent(lb,0,0,4,1);
       // constraints.weightx=500;//可水平分配的多余空间为500
       // constraints.weighty=1;//可垂直分配的多余空间为1
        m.addActionListener(this);
        m.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
        Font font=new Font("宋体",Font.BOLD,15);
        lb.setFont(font);
        lb.setForeground(Color.BLUE);
        btBackspace=new JButton("退格");
        btBackspace.setFont(font);
        btBackspace.setForeground(Color.red);
        btBackspace.addActionListener(this);
 		btreset=new JButton("清零");
        btreset.setFont(font);
        btreset.setForeground(Color.red);
 		btreset.addActionListener(this);
      	bt0=new JButton("0");
		bt0.addActionListener(this);
		bt1=new JButton("1");
		bt1.addActionListener(this);
		bt2=new JButton("2");
		bt2.addActionListener(this);
		bt3=new JButton("3");
		bt3.addActionListener(this);
		bt4=new JButton("4");
		bt4.addActionListener(this);
		bt5=new JButton("5");
		bt5.addActionListener(this);
		bt6=new JButton("6");
		bt6.addActionListener(this);
		bt7=new JButton("7");
		bt7.addActionListener(this);
		bt8=new JButton("8");
		bt8.addActionListener(this);
		bt9=new JButton("9");
		bt9.addActionListener(this);
		btAdd=new JButton("+");
        btAdd.setForeground(Color.red);
		btAdd.addActionListener(this);
		btMin=new JButton("-");
        btMin.setForeground(Color.red);
		btMin.addActionListener(this);
		btMul=new JButton("*");
        btMul.setForeground(Color.red);
		btMul.addActionListener(this);
		btDiv=new JButton("/");
        btDiv.setForeground(Color.red);
		btDiv.addActionListener(this);
		btEqual=new JButton("=");
        btEqual.setForeground(Color.red);
		btEqual.addActionListener(this);
        constraints.fill=GridBagConstraints.BOTH;
        constraints.insets=new Insets(2,1,1,1);//表示组件边界间距大小,四个参数分别表示"上,左,下,右"
        addComponent(btBackspace,2,0,2,1);
        addComponent(btreset,2,2,2,1);
        addComponent(bt7,3,0,1,1);
        addComponent(bt8,3,1,1,1);
        addComponent(bt9,3,2,1,1);
        addComponent(btDiv,3,3,1,1);
        addComponent(bt4,4,0,1,1);
        addComponent(bt5,4,1,1,1);
        addComponent(bt6,4,2,1,1);
        addComponent(btMul,4,3,1,1);
        addComponent(bt1,5,0,1,1);
        addComponent(bt2,5,1,1,1);
        addComponent(bt3,5,2,1,1);
        addComponent(btMin,5,3,1,1);
        addComponent(bt0,6,0,1,1);
        addComponent(btEqual,6,1,2,1);
        addComponent(btAdd,6,3,1,1);
       // constraints.weightx=10;
       // constraints.weighty=10;
        setSize(200,270);//Frame大小为长200,高270
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
			if(i==1&&doubleClick)//获取第二个操作数之前清空文本框
			{
				twoOpNum=true;
				text1.setText("");
			}
			if(e.getSource()==m)//按下Exit菜单退出
			{
				System.exit(0);
			}
			if(e.getSource()==btBackspace)//退格
			{
                String s1="",s2="";
                int l=0;
                l=text1.getText().length();
                s1=text1.getText();
                if(l==0||l==1)
                    text1.setText("0");
                else
                {
                    s2=s1.substring(0,l-1);
                    text1.setText(s2);
                    sop[i]=s2;
                }                
                doubleClick=false;
			}
			if(e.getSource()==btreset)//清零
			{								
				sop[0]="0";sop[1]="";
				i=0;flag=false;
				doubleClick=true;
				twoOpNum=false;
				result="";
                text1.setText("0");
			}
			if(e.getSource()==bt0)
			{
				sop[i]=sop[i]+bt0.getText();
				doubleClick=true;
			}
			if(e.getSource()==bt1)
			{
				sop[i]=sop[i]+bt1.getText();
				doubleClick=true;
			}
			if(e.getSource()==bt2)
			{
				sop[i]=sop[i]+bt2.getText();
				doubleClick=true;
			}
			if(e.getSource()==bt3)
			{
				sop[i]=sop[i]+bt3.getText();
				doubleClick=true;
			}
			if(e.getSource()==bt4)
			{
				sop[i]=sop[i]+bt4.getText();
				doubleClick=true;
			}
			if(e.getSource()==bt5)
			{
				sop[i]=sop[i]+bt5.getText();
				doubleClick=true;
			}
			if(e.getSource()==bt6)
			{
				sop[i]=sop[i]+bt6.getText();
				doubleClick=true;
			}
			if(e.getSource()==bt7)
			{
				sop[i]=sop[i]+bt7.getText();
				doubleClick=true;
			}
			if(e.getSource()==bt8)
			{
				sop[i]=sop[i]+bt8.getText();
				doubleClick=true;
			}
			if(e.getSource()==bt9)
			{
				sop[i]=sop[i]+bt9.getText();
				doubleClick=true;
			}
			if(doubleClick) text1.setText(sop[i]);//连续按下的数字累加显示到text1上
			if(e.getSource()==btAdd)
			{
				if(doubleClick)//如果不是连续按下的操作就进行运算
				{
					calcul();
				}
				op='+';
				doubleClick=false;//使连续按下操作无效
			}
			if(e.getSource()==btMin)
			{
				if(doubleClick)
				{
					calcul();
				}
				op='-';
				doubleClick=false;
			}
			if(e.getSource()==btMul)
			{
				if(doubleClick)
				{
					calcul();
				}
				op='*';
				doubleClick=false;
			}
			if(e.getSource()==btDiv)
			{
				if(doubleClick)
				{
					calcul();
				}
				op='/';
				doubleClick=false;
			}
			if(e.getSource()==btEqual)
			{
				if(doubleClick)
			    {
			    	if(twoOpNum)
				    {
				     	calcul();
					    twoOpNum=false;
				    }
			    }
			doubleClick=false;
			}

	}
	 void calcul()
	{
		if(flag)
		{
			result=operation();
			sop[0]=result;
			i=1;
			sop[1]="";
			text1.setText(result);
		}
		else
		{
			i=1;
		}
		flag=true;
	}
	String operation()//加减乘除运算
	{
		float re=0;
		switch(op)
		{
			case '+':
			re=Float.parseFloat(sop[0])+Float.parseFloat(sop[1]);
			break;
			case '-':
			re=Float.parseFloat(sop[0])-Float.parseFloat(sop[1]);
			break;
			case '*':
			re=Float.parseFloat(sop[0])*Float.parseFloat(sop[1]);
			break;
			case '/':
			re=Float.parseFloat(sop[0])/Float.parseFloat(sop[1]);
			break;
		}
		return(String.valueOf(re));
	}
    private void addComponent(Component component,int row, int column, int width ,int height){//创建addComponent方法
        constraints.gridx=column;
        constraints.gridy=row;
        constraints.gridwidth=width;
        constraints.gridheight=height;
        bagLayout.setConstraints(component,constraints);
        content.add(component);
    }
    public static void main(String[] arg){
        Calculator testCal=new Calculator();//初始化计算器类的对象
        testCal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//响应关闭按钮
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -