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

📄 mainss.java

📁 这是一个JAVA编写的计算器,有完整代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

class calculator extends JFrame implements ActionListener,KeyListener
{ String x1="0",x2="0";  //记录双操作数运算中的双操作数
double memory=0;         //为内存数,用以存储和读出内存数据
int oper=-2,state=0,combo=-1,eq=0;  //oper控制双操作数的算术运算是什么,state控制算术符号按了几次,combo控制同一符号的重复按,eq判断前一操作是否为等
                                     //于,memory为存储用.oper取值包括-2(空运算),1(加),2(减),3(乘),4(除).
									 //state取值为0,1,2;每当state=3的时候,自动减1变2,表示运算次数大于1次,且state只记录双操作数的按键次数				//combo的取值为-1(空运算),1(加),2(减),3(乘),4(除),此值的作用是用于与oper比较,确定是否重复了同一运算执行
									  //eq的取值为0或1,0表示前一操作不是"等于",1表示前一操作为"等于"
boolean yn=false,ifdot;  //yn控制当前的数是否已经输入结束;ifdot控制当前的输入是否已经有浮点作用是不准许对同一数多次加浮点号.
    JMenuBar menubar;   //菜单条
  JMenu menu,help=new JMenu("帮助(H)"),tent=new JMenu();  //菜单,menu为显示的菜单(包含复制和粘贴),tent为不可显示的菜单,用以添加各种快捷键(对应26个按钮)
  JMenuItem copy,plaster,helpt=new JMenuItem("帮助主题"),about=new JMenuItem("关于计算器")   //菜单项,作用是复制和粘贴,加在menu中
	  ,mb0=new JMenuItem("0")   
	  ,mb1=new JMenuItem("1")
	  ,mb2=new JMenuItem("2")
	  ,mb3=new JMenuItem("3")
	  ,mb4=new JMenuItem("4")
	  ,mb5=new JMenuItem("5")
	  ,mb6=new JMenuItem("6")
	  ,mb7=new JMenuItem("7")
	  ,mb8=new JMenuItem("8")
	  ,mb9=new JMenuItem("9")
	  ,mbadd=new JMenuItem("+")
	  ,mbsub=new JMenuItem("-")
	  ,mbmul=new JMenuItem("*")
	  ,mbdiv=new JMenuItem("/")
	  ,mbdot=new JMenuItem(".")
	  ,mbsign=new JMenuItem("+/-")
	  ,mbsqrt=new JMenuItem("sqrt")
	  ,mb1x=new JMenuItem("1/x")
	  ,mbeq=new JMenuItem("=")
	  ,mbper=new JMenuItem("%")
	  ,mbspace=new JMenuItem("Backspace")
	  ,mbce=new JMenuItem("CE")
	  ,mbc=new JMenuItem("C")
	  ,mbmc=new JMenuItem("MC")
	  ,mbmr=new JMenuItem("MR")
	  ,mbms=new JMenuItem("MS")
	  ,mbmadd=new JMenuItem("M+");   //以上26个菜单项对应26个计算器上的按钮,作用是添加相应的快捷键,他们是不可显示的状态
  Container con;   //最底层的容器
  JTextField show,mem;      //show为显示结果的部分,而mem用以内存的标志,当内存中存入了数,显示'M',否则不显示任何内容
  JTextArea showtemp=new JTextArea(10,10);   //此文本不可见.showtemp的作用是用来进行进行粘贴和复制的操作,当粘贴和复制的内容不为数字,则不将内容赋给show
  JButton b0=new JButton("0")
	  ,b1=new JButton("1")
	  ,b2=new JButton("2")
	  ,b3=new JButton("3")
	  ,b4=new JButton("4")
	  ,b5=new JButton("5")
	  ,b6=new JButton("6")
	  ,b7=new JButton("7")
	  ,b8=new JButton("8")
	  ,b9=new JButton("9")
	  ,badd=new JButton("+")
	  ,bsub=new JButton("-")
	  ,bmul=new JButton("*")
	  ,bdiv=new JButton("/")
	  ,bdot=new JButton(".")
	  ,bsign=new JButton("+/-")
	  ,bsqrt=new JButton("sqrt")
	  ,b1x=new JButton("1/x")
	  ,beq=new JButton("=")
	  ,bper=new JButton("%")
	  ,bspace=new JButton("Backspace")
	  ,bce=new JButton("CE")
	  ,bc=new JButton("C")
	  ,bmc=new JButton("MC")
	  ,bmr=new JButton("MR")
	  ,bms=new JButton("MS")
	  ,bmadd=new JButton("M+");    //对应26个按钮
  calculator(String s) //构造函数
	{
  super(s);
  
  badd.setForeground(new Color(255, 0, 0));
  b0.setForeground(Color.blue);
  b1.setForeground(Color.blue);
  b2.setForeground(Color.blue);
  b3.setForeground(Color.blue);
  b4.setForeground(Color.blue);
  b5.setForeground(Color.blue);
  b6.setForeground(Color.blue);
  b7.setForeground(Color.blue);
  b8.setForeground(Color.blue);
  b9.setForeground(Color.blue);
  bsign.setForeground(Color.blue);
  bdot.setForeground(Color.blue);
  b1x.setForeground(Color.blue);
  bsqrt.setForeground(Color.blue);
  bper.setForeground(Color.blue);
  bsub.setForeground(new Color(255, 0, 0));
  bdiv.setForeground(new Color(255, 0, 0));
  bmul.setForeground(new Color(255, 0, 0));
  beq.setForeground(new Color(255, 0, 0));
  bc.setForeground(new Color(255, 0, 0));
  bce.setForeground(new Color(255, 0, 0));
  bspace.setForeground(new Color(255, 0, 0));
  bmc.setForeground(new Color(255, 0, 0));
  bmr.setForeground(new Color(255, 0, 0));
  bmadd.setForeground(new Color(255, 0, 0));
  bms.setForeground(new Color(255, 0, 0));  //设置27个按钮的字体颜色
  b0.addActionListener(this);
  b1.addActionListener(this);
	  b2.addActionListener(this);
	  b3.addActionListener(this);
	  b4.addActionListener(this);
	  b5.addActionListener(this);
	  b6.addActionListener(this);
	  b7.addActionListener(this);
	  b8.addActionListener(this);
	  b9.addActionListener(this);
	  badd.addActionListener(this);
	  bsub.addActionListener(this);
	  bmul.addActionListener(this);
	  bdiv.addActionListener(this);
	  bdot.addActionListener(this);
	  bsign.addActionListener(this);
	  bsqrt.addActionListener(this);
	  b1x.addActionListener(this);
	  beq.addActionListener(this);
	  bper.addActionListener(this);
	  bspace.addActionListener(this);
	  bce.addActionListener(this);
	  bc.addActionListener(this);
	  bmc.addActionListener(this);
	  bmr.addActionListener(this);
	  bms.addActionListener(this);
	  bmadd.addActionListener(this);  //27个按钮添加监视器
	   mb0.setAccelerator(KeyStroke.getKeyStroke('0'));
  mb1.setAccelerator(KeyStroke.getKeyStroke('1'));
	  mb2.setAccelerator(KeyStroke.getKeyStroke('2'));
	  mb3.setAccelerator(KeyStroke.getKeyStroke('3'));
	  mb4.setAccelerator(KeyStroke.getKeyStroke('4'));
	  mb5.setAccelerator(KeyStroke.getKeyStroke('5'));
	  mb6.setAccelerator(KeyStroke.getKeyStroke('6'));
	  mb7.setAccelerator(KeyStroke.getKeyStroke('7'));
	  mb8.setAccelerator(KeyStroke.getKeyStroke('8'));
	  mb9.setAccelerator(KeyStroke.getKeyStroke('9'));
	  mbadd.setAccelerator(KeyStroke.getKeyStroke('+'));
	  mbsub.setAccelerator(KeyStroke.getKeyStroke('-'));
	  mbmul.setAccelerator(KeyStroke.getKeyStroke('*'));
	  mbdiv.setAccelerator(KeyStroke.getKeyStroke('/'));
	  mbdot.setAccelerator(KeyStroke.getKeyStroke('.'));
	  mbsign.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F9,0));
	  mbsqrt.setAccelerator(KeyStroke.getKeyStroke('@'));
	  mb1x.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,0));
	  mbeq.setAccelerator(KeyStroke.getKeyStroke('='));
	  mbper.setAccelerator(KeyStroke.getKeyStroke('%'));
	  mbspace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE,0));
	  mbce.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
	  mbc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0));
	  mbmc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,InputEvent.CTRL_MASK));
	  mbmr.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
	  mbms.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M,InputEvent.CTRL_MASK));
	  mbmadd.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
  //27个菜单项添加快捷键
	  mb0.addActionListener(this);
  mb1.addActionListener(this);
	  mb2.addActionListener(this);
	  mb3.addActionListener(this);
	  mb4.addActionListener(this);
	  mb5.addActionListener(this);
	  mb6.addActionListener(this);
	  mb7.addActionListener(this);
	  mb8.addActionListener(this);
	  mb9.addActionListener(this);
	  mbadd.addActionListener(this);
	  mbsub.addActionListener(this);
	  mbmul.addActionListener(this);
	  mbdiv.addActionListener(this);
	  mbdot.addActionListener(this);
	  mbsign.addActionListener(this);
	  mbsqrt.addActionListener(this);
	  mb1x.addActionListener(this);
	  mbeq.addActionListener(this);
	  mbper.addActionListener(this);
	  mbspace.addActionListener(this);
	  mbce.addActionListener(this);
	  mbc.addActionListener(this);
	  mbmc.addActionListener(this);
	  mbmr.addActionListener(this);
	  mbms.addActionListener(this);
	  mbmadd.addActionListener(this);   //27个菜单项添加到监视器

  setBounds(400,200,400,350);
  setVisible(true);
  setResizable(false);
  menubar=new JMenuBar();
  menu=new JMenu("编辑(E)");
  menu.setMnemonic('E');
  help.setMnemonic('H');
  copy=new JMenuItem("复制(C)");
  plaster=new JMenuItem("粘贴(P)");
  helpt.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));
  about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
  copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
  plaster.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));

//复制粘贴添加快捷键
helpt.addActionListener(this);
 about.addActionListener(this);
  plaster.addActionListener(this);
  copy.addActionListener(this);  //复制粘贴添加监视器
  menu.add(copy);
  menu.addSeparator();
  menu.add(plaster);
  help.add(helpt);
  help.addSeparator();
  help.add(about);
  
  menubar.add(menu);  //菜单条添加”编辑”菜单
  menubar.add(help);
  tent.add(mb0);
  tent.add(mb1);
  tent.add(mb2);
  tent.add(mb3);
  tent.add(mb4);
  tent.add(mb5);
  tent.add(mb6);
  tent.add(mb7);
  tent.add(mb8);
  tent.add(mb9);
  tent.add(mbadd);
  tent.add(mbsub);
  tent.add(mbmul);
  tent.add(mbdiv);
  tent.add(mbdot);
  tent.add(mbsign);
  tent.add(mbsqrt);
  tent.add(mbeq);
  tent.add(mb1x);
  tent.add(mbper);
  tent.add(mbspace);
  tent.add(mbce);
  tent.add(mbc);
  tent.add(mbmadd);
  tent.add(mbms);
  tent.add(mbmr);
  tent.add(mbmc);
  tent.setVisible(false);   //加入27个菜单项(对应27个按钮),并设该菜单不可见
  menubar.add(tent);   //不可见菜单tent加入菜单条

  setJMenuBar(menubar);        //菜单条部分完毕
  con=this.getContentPane();
  show=new JTextField(33);  //显示结果的文本框,状态为不可编辑
  show.setEditable(false);
  show.setHorizontalAlignment(JTextField.RIGHT);
  show.setText("0");
  JPanel top=new JPanel();
  FlowLayout toplayout=new FlowLayout();
  toplayout.setAlignment(FlowLayout.CENTER);
  top.setLayout(toplayout);
  top.add(show);
  con.add(top,BorderLayout.NORTH);  //显示文本框添加完毕
  JSplitPane boxmain,boxright;  //boxmain为最下层的容器,分左右两块,boxright为右边的容器,为左右
  JPanel buttonarea=new JPanel()      //主要的按钮区域
	  ,boxhigh=new JPanel()                 //右容器的上部
	  ,boxleft=new JPanel();                  //左边容器中的面板 
  boxleft.setLayout(new GridLayout(5,1,10,10));  //左边面板5行1列
  mem=new JTextField(1);            //用以显示内存状态的组件
  mem.setEditable(false);
  JPanel jj=new JPanel();   //一个小面板
  jj.add(mem);          //添加显示内存状态的文本到面板JJ
  boxleft.add(jj);       //面板JJ添加到左容器的最上格
  boxleft.add(bmc);
  boxleft.add(bmr);
  boxleft.add(bms);
  boxleft.add(bmadd);        //左边面板组建添加完毕
  buttonarea.setLayout(new GridLayout(4,5,10,10));  //主要的按钮区域,4行5列,相邻间隔10像素
  buttonarea.add(b7);
  buttonarea.add(b8);
  buttonarea.add(b9);
  buttonarea.add(bdiv);
  buttonarea.add(bsqrt);
  buttonarea.add(b4);
  buttonarea.add(b5);
  buttonarea.add(b6);
  buttonarea.add(bmul);
  buttonarea.add(bper);
  buttonarea.add(b1);
  buttonarea.add(b2);
  buttonarea.add(b3);
  buttonarea.add(bsub);
  buttonarea.add(b1x);
  buttonarea.add(b0);
  buttonarea.add(bsign);
  buttonarea.add(bdot);
  buttonarea.add(badd);
  buttonarea.add(beq);   //主要的按钮区域,设置完毕
  boxhigh.setLayout(new GridLayout(1,3,15,10));
  boxhigh.add(bspace);
  boxhigh.add(bce);
  boxhigh.add(bc);       //右边上部分容器添加组建完成(3个按钮)
  boxright=new JSplitPane(JSplitPane.VERTICAL_SPLIT,boxhigh,buttonarea); 
  boxright.setDividerLocation(45);//右边分割线设置完毕
  boxmain=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,boxleft,boxright); //低层容器添加分割线位置
  con.add(boxmain,BorderLayout.CENTER);  //中心布局添加完毕
  show.setEnabled(false); //显示结果的文本框不可进行选定,这是为了防止快捷键因选定文本框导致失灵
   show.setBackground(Color.black);
   mem.setEnabled(false);
  validate();    //刷新组建
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public void actionPerformed(ActionEvent e)
	{
	 boolean wrong=false;
	  if(e.getSource()==plaster)   //实现粘贴
		{
		  showtemp.setText("");
	  showtemp.paste();
	  String s=showtemp.getText();
	  try{double temp=Double.parseDouble(s);}  //必要的检查,如果粘贴的不是数字,则粘贴不进行.
      catch(Exception ee) {wrong=true;}
	  if(!wrong) show.setText(s);
      }
     if(e.getSource()==copy)  //实现复制
		{
	 showtemp.setText(show.getText());
	 showtemp.selectAll();
	 showtemp.copy();
	  }
	  if(e.getSource()==helpt)
		{
String s="[%]-- 按百分比的形式显示乘积结果。输入一个数,单击“*”,输入第二个数,然后单击“%”。例如,50 * 25% 将显示为 12.5。也可执行带百分数的运算。输入一个数,单击运算符(“+”、“-”、“*”或“/”),输入第二个数,单击“%”,然后单击“=”。例如,50 + 25%(指的是 50 的 25%)= 62.5。 \n[*]-- 乘法。 \n[+]-- 加法。\n[+/-]--改变显示数字的符号。\n[-]-- 减法。 \n[.]-- 插入小数点。\n[/]-- 除法。 \n[0–9]-- 将此数字置于计算器的显示区。 \n[1/x]-- 计算显示数字的倒数。 \n[=]-- 对上两个数字执行任意运算。若要重复上一次的运算,请再次单击“=”。\n[C]-- 清除当前的计算。\n [CE]-- 清除显示数字。\n[M+]--将显示的数字与内存中已有的任何数字相加,但不显示这些数字的和。\n[MC]-- 清除内存中的所有数字。\n[MR]--重调用存内存中的数字。该数字保留在内存中。 \n[MS]-- 将显示数字保存在内存中。\n[sqrt]-- 计算显示数字的平方根。\n[Backspace]-- 删除当前显示数字的最后一位。\n";
Dialog KKA=new Dialog(this,"帮助主题",s,400,462);
}
if(e.getSource()==about)
		{String s = "版权所有 (R) 2007\r\n制作者:赵智强   班级:06级计算机3班   学号:060140125\r\n如果任何程序问题可发E-mail至532256580@qq.com联系我,谢谢使用:)\n老师可手机联系:13402076757\n";
   Dialog KKB=new Dialog(this,"关于计算器",s,360,122);
}
	  if(e.getSource()==b0 || e.getSource()==mb0)  //实现按钮0及其快捷方式
		{if(yn) show.setText("0");
		else	{if(show.getText().equals("0")) show.setText("");
          show.setText(show.getText()+"0");}
		  yn=false;
		  combo=-1;}
	if(e.getSource()==b1 || e.getSource()==mb1) //实现按钮1及其快捷方式
		{if(yn) show.setText("1");
		else	{
		if(show.getText().equals("0")) show.setText("");
	show.setText(show.getText()+"1");}
	 yn=false;combo=-1;}
     if(e.getSource()==b2 || e.getSource()==mb2) //实现按钮2及其快捷方式

⌨️ 快捷键说明

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