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

📄 calculator.java

📁 通过JAVA设计 GUI 界面的计算器程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.awt.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Calculator
    implements ActionListener { //导入动作监听接口
  //设计面板中的单位
  int mid;
  int fchange;
  long ianswer;
  long ivard;
  JFrame frame;
  JTextField textAnswer;//显示数值窗口
  JPanel panel, panel1, panel2, panel3,p4;
  JMenuBar mainMenu;//菜单容器
  JTextField textMemory;//显示当前是否内存有数
  JLabel labelMemSpace; //labelMemSpace单纯做摆设,控制面板的形状
  JButton buttonBk, buttonCe, buttonC;//退格,清除,清0
  JButton button[];//数字键数组
  JButton buttonMC, buttonMR, buttonMS, buttonMAdd;//内存键
  JButton bsin,bcos,bpi,bmod,bxy,bx3,bln,btan,bx2,bn;
  JButton ba,bb,bc,bd,be,bf;
  JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul, buttonDiv, buttonmod;//运算符键
  JButton buttonsqrt, buttonDao, buttonEqual;//平方,倒数,开平方
  ButtonGroup group;//单选组
  JMenu editMenu, viewMenu, helpMenu;//菜单编辑,查看,帮助
  JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal;
  DecimalFormat df; //设置数据输出精度
  JRadioButton rb1,rb2,rb3,rb4;
  boolean clickable; //控制当前能否按键
  boolean frist;//是否第一次按数字键
  double memoryd; //使用内存中存储的数字
  int memoryi;
  double vard, answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd)
  short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
  String copy; //做复制用
  JTextArea help; //帮助
  JScrollPane scrollHelp;
  //构造函数
  public Calculator() {
  	rb1=new JRadioButton("16进制");
  	rb1.addActionListener(this);
  	rb2=new JRadioButton("10进制");
  	rb2.addActionListener(this);
  	rb3=new JRadioButton("2进制");
  	rb3.addActionListener(this);
  	rb4=new JRadioButton("8进制");
  	rb4.addActionListener(this);
  	group=new ButtonGroup();
  	group.add(rb1);
  	group.add(rb2);
  	group.add(rb3);
  	group.add(rb4);
  	rb2.setSelected(true);
  	p4=new JPanel();
  	p4.setLayout(new GridLayout(1,3));
  	p4.add(rb1);
  	p4.add(rb2);
  	p4.add(rb4);
  	p4.add(rb3);
    clickable = true;//当前可以有键按下了
    answerd = 0;
    frist=true;
    frame = new JFrame("王超的计算器");
    df = new DecimalFormat("0.##############"); //设置数据输出精度(对于double型值)
    textAnswer = new JTextField(15);//开辟一个15个字符的文本框空间
    textAnswer.setEditable(false);//不可编辑
    textAnswer.setBackground(new Color(255, 255, 255));
    panel = new JPanel();
    frame.getContentPane().add(panel);
    panel1 = new JPanel();
    panel2 = new JPanel();
    panel.setLayout(new BorderLayout());//边界式布局
    //设计整个面板
    mainMenu = new JMenuBar();//菜单
    editMenu = new JMenu("编辑(E)");
    viewMenu = new JMenu("查看(V)");
    helpMenu = new JMenu("帮助(H)");
    copyItem = new JMenuItem("   复制(C) Ctrl+C");
    copyItem.addActionListener(this);//事件监听复制
    pasteItem = new JMenuItem("   粘贴(V) Ctrl+V");
    pasteItem.addActionListener(this);//事件监听粘贴
    editMenu.add(copyItem);//添加到菜单
    editMenu.add(pasteItem);
    tItem = new JMenuItem("   标准型(T)");//===============
    tItem.addActionListener(this);
    sItem = new JMenuItem("   科学型(S)");
    sItem.addActionListener(this);
    numberGroup = new JMenuItem("   数字分组(I)");
    numberGroup.addActionListener(this);
    viewMenu.add(tItem);
    viewMenu.add(sItem);
    viewMenu.add(numberGroup);
    topHelp = new JMenuItem("   帮助主题(H)");//===============
    topHelp.addActionListener(this);
    help = new JTextArea(5, 20);//帮助主题弹出文本对话框
    scrollHelp = new JScrollPane(help);
    help.setEditable(false);
    help.append("执行简单计算\n");
    help.append("1.  键入计算的第一个数字。\n");
    help.append("2.  单击“+”执行加、“-”执行减、“*”执行乘或“/”执行除。\n");
    help.append("3.  键入计算的下一个数字。\n");
    help.append("4.  输入所有剩余的运算符和数字。\n");
    help.append("5.  单击“=”。\n");
    aboutCal = new JMenuItem("   关于计算器(A)");
    aboutCal.addActionListener(this);
    helpMenu.add(topHelp);
    helpMenu.add(aboutCal);
    mainMenu.add(editMenu);
    mainMenu.add(viewMenu);
    mainMenu.add(helpMenu);
    panel.add(mainMenu, BorderLayout.NORTH);//布局北菜单窗口
    panel.add(textAnswer, BorderLayout.CENTER);//中显示窗口
    panel.add(panel1, BorderLayout.SOUTH);//南按键窗口
    panel1.setLayout(new BorderLayout());
    textMemory = new JTextField(3);
    textMemory.setEditable(false);
    textMemory.setBackground(new Color(217, 217, 217));
    labelMemSpace = new JLabel("      ");
    buttonBk = new JButton("Backspace");
    buttonBk.setForeground(new Color(255, 0, 0));
    buttonCe = new JButton("ce");
    buttonCe.setForeground(new Color(255, 0, 0));
    buttonC = new JButton("c");
    buttonC.setForeground(new Color(255, 0, 0));
    buttonBk.addActionListener(this);
    buttonCe.addActionListener(this);
    buttonC.addActionListener(this);
    panel1.add(panel2, BorderLayout.NORTH);
    panel2.setLayout(new FlowLayout(FlowLayout.LEFT));//左对齐
    panel2.add(textMemory);
    panel2.add(buttonBk);
    panel2.add(buttonCe);
    panel2.add(buttonC); 
    panel2.add(p4);
    panel3 = new JPanel();
    panel1.add(panel3, BorderLayout.CENTER);
    button = new JButton[10];
    for (int i = 0; i < button.length; i++) {
      button[i] = new JButton(Integer.toString(i));
      button[i].setForeground(new Color(0, 0, 255));
     
    }
    bsin=new JButton("sin");
    bsin.setForeground(new Color(0, 0, 255));
    bcos=new JButton("cos");
    bcos.setForeground(new Color(0, 0, 255));
    btan=new JButton("tan");
    btan.setForeground(new Color(0, 0, 255));
    bpi=new JButton("pi");
    bpi.setForeground(new Color(0, 0, 255));
    bxy=new JButton("x^y");/////////////////////////////////////////////////
    bxy.setForeground(new Color(0, 0, 255));
    bx3=new JButton("x^3");
    bx3.setForeground(new Color(0, 0, 255));
    bln=new JButton("ln");
    bln.setForeground(new Color(0, 0, 255));
    bx2=new JButton("x^2");
    bx2.setForeground(new Color(0, 0, 255));
    bmod=new JButton("mod");
    bmod.setForeground(new Color(0, 0, 255));
    bn=new JButton("n!");
    bn.setForeground(new Color(0, 0, 255));
    ba=new JButton("a");
    ba.setForeground(new Color(0, 0, 255));
    bb=new JButton("b");
    bb.setForeground(new Color(0, 0, 255));
    bc=new JButton("c");
    bc.setForeground(new Color(0, 0, 255));
    bd=new JButton("d");
    bd.setForeground(new Color(0, 0, 255));
    be=new JButton("e");
    be.setForeground(new Color(0, 0, 255));
    bf=new JButton("f");
    bf.setForeground(new Color(0, 0, 255));
    buttonMC = new JButton("MC");
    buttonMC.setForeground(new Color(255, 0, 0));
    buttonMR = new JButton("MR");
    buttonMR.setForeground(new Color(255, 0, 0));
    buttonMS = new JButton("MS");
    buttonMS.setForeground(new Color(255, 0, 0));
    buttonMAdd = new JButton("M+");
    buttonMAdd.setForeground(new Color(255, 0, 0));
    buttonDot = new JButton(".");
    buttonDot.setForeground(new Color(0, 0, 255));
    buttonAddAndSub = new JButton("+/-");
    buttonAddAndSub.setForeground(new Color(0, 0, 255));
    buttonAdd = new JButton("+");
    buttonAdd.setForeground(new Color(255, 0, 0));
    buttonSub = new JButton("-");
    buttonSub.setForeground(new Color(255, 0, 0));
    buttonMul = new JButton("*");
    buttonMul.setForeground(new Color(255, 0, 0));
    buttonDiv = new JButton("/");
    buttonDiv.setForeground(new Color(255, 0, 0));
    buttonmod = new JButton("%");
    buttonmod.setForeground(new Color(0, 0, 255));
    buttonsqrt = new JButton("sqrt");
    buttonsqrt.setForeground(new Color(0, 0, 255));
    buttonDao = new JButton("1/x");
    buttonDao.setForeground(new Color(0, 0, 255));
    buttonEqual = new JButton("=");
    buttonEqual.setForeground(new Color(255, 0, 0));
    buttonMC = new JButton("MC");
    buttonMC.setForeground(new Color(255, 0, 0));
    //将所有行为与监听绑定
    panel3.setLayout(new GridLayout(5, 8));
    panel3.add(buttonMC);
    buttonMC.addActionListener(this);
    panel3.add(button[7]);
    button[7].addActionListener(this);
    panel3.add(button[8]);
    button[8].addActionListener(this);
    panel3.add(button[9]);
    button[9].addActionListener(this);
    panel3.add(buttonDiv);
    buttonDiv.addActionListener(this);
    panel3.add(buttonsqrt);
    buttonsqrt.addActionListener(this);
     panel3.add(bsin);
    bsin.addActionListener(this);
     panel3.add(bcos);
    bcos.addActionListener(this);
    panel3.add(buttonMR);
    buttonMR.addActionListener(this);
    panel3.add(button[4]);
    button[4].addActionListener(this);
    panel3.add(button[5]);
    button[5].addActionListener(this);
    panel3.add(button[6]);
    button[6].addActionListener(this);
    panel3.add(buttonMul);
    buttonMul.addActionListener(this);
    panel3.add(buttonmod);
    buttonmod.addActionListener(this);
     panel3.add(btan);
    btan.addActionListener(this);
         panel3.add(bpi);
    bpi.addActionListener(this);
    panel3.add(buttonMS);
    buttonMS.addActionListener(this);
    panel3.add(button[1]);
    button[1].addActionListener(this);
    panel3.add(button[2]);
    button[2].addActionListener(this);
    panel3.add(button[3]);
    button[3].addActionListener(this);
    panel3.add(buttonSub);
    buttonSub.addActionListener(this);
    panel3.add(buttonDao);
    buttonDao.addActionListener(this);
    panel3.add(bmod);
    bmod.addActionListener(this);
     panel3.add(bxy);//////////////////////////////////////////////////////
    bxy.addActionListener(this);
    panel3.add(buttonMAdd);
    buttonMAdd.addActionListener(this);
    panel3.add(button[0]);
    button[0].addActionListener(this);
    panel3.add(buttonAddAndSub);
    buttonAddAndSub.addActionListener(this);
    panel3.add(buttonDot);
    buttonDot.addActionListener(this);
    panel3.add(buttonAdd);
    buttonAdd.addActionListener(this);
    panel3.add(buttonEqual);
    buttonEqual.addActionListener(this);
     panel3.add(bn);
    bn.addActionListener(this);
     panel3.add(bx2);
    bx2.addActionListener(this);
    panel3.add(ba);
    ba.addActionListener(this);
    panel3.add(bb);
    bb.addActionListener(this);
    panel3.add(bc);
    bc.addActionListener(this);
    panel3.add(bd);
    bd.addActionListener(this);
    panel3.add(be);
    be.addActionListener(this);
    panel3.add(bf);
    bf.addActionListener(this);
    panel3.add(bx3);
    bx3.addActionListener(this);
   panel3.add(bln);
    bln.addActionListener(this);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    visi();
    textAnswer.setText("0");
    fsix();
    mid=2;
    frame.pack();
    frame.show();
    fchange=2;
    
    
  }
  //设置各个按钮行为
  public void actionPerformed(ActionEvent event) {
    boolean sign = false; //判断是否是double型数参与运算,是为true,不是为false
    Object temp = event.getSource();
     try {
      //如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后
      for (int i = 0; i <= 9; i++)
        if (temp == button[i] && clickable == true){////0-------9
            String s = textAnswer.getText();
                if (s.charAt(0)== '0' )
        	         frist=false;
        	if(frist==true)
        	textAnswer.setText(textAnswer.getText() + String.valueOf(i));//Integer.toString(i));
        	else 
         { textAnswer.setText("");
      	textAnswer.setText(textAnswer.getText() + String.valueOf(i));//Integer.toString(i));   
           frist=true;}
        }
          if (temp == ba && clickable == true){/////AAAAAAAAAAA
        	String s = textAnswer.getText();
                if (s.charAt(0)== '0' )
        	         frist=false;
        	if(frist==true)
        	textAnswer.setText(textAnswer.getText() + "a");
        	else 
         { textAnswer.setText("");
      	textAnswer.setText(textAnswer.getText() + "a");   
           frist=true;}
        }
        if (temp == bb && clickable == true){/////BBBBBBBBBBBBBBB
           String s = textAnswer.getText();
                if (s.charAt(0)== '0' )
        	         frist=false;
        	if(frist==true)
        	textAnswer.setText(textAnswer.getText() + "b");
        	else 
         { textAnswer.setText("");
      	textAnswer.setText(textAnswer.getText() + "b");   
           frist=true;}
        }
        if (temp == bc && clickable == true){/////CCCCCCCCCCCCCCCCC
        String s = textAnswer.getText();
                if (s.charAt(0)== '0' )
        	         frist=false;
        	if(frist==true)
        	textAnswer.setText(textAnswer.getText() + "c");
        	else 
         { textAnswer.setText("");
      	textAnswer.setText(textAnswer.getText() + "c");   
           frist=true;}
        }
        if (temp == bd && clickable == true){/////DDDDDDDDDDDDDDDDDD
        String s = textAnswer.getText();
                if (s.charAt(0)== '0' )
        	         frist=false;
        	if(frist==true)
        	textAnswer.setText(textAnswer.getText() + "d");
        	else 
         { textAnswer.setText("");
      	textAnswer.setText(textAnswer.getText() + "d");   
           frist=true;}
        }
        if (temp == be && clickable == true){/////EEEEEEEEEEEEEEEEEEE
        String s = textAnswer.getText();
                if (s.charAt(0)== '0' )
        	         frist=false;
        	if(frist==true)
        	textAnswer.setText(textAnswer.getText() + "e");
        	else 
         { textAnswer.setText("");
      	textAnswer.setText(textAnswer.getText() + "e");   
           frist=true;}
        }
        if (temp == bf && clickable == true){/////FFFFFFFFFFFFFFFFFF
       String s = textAnswer.getText();
                if (s.charAt(0)== '0' )
        	         frist=false;
        	if(frist==true)
        	textAnswer.setText(textAnswer.getText() + "f");
        	else 
         { textAnswer.setText("");
      	textAnswer.setText(textAnswer.getText() + "f");   
           frist=true;}
        }
          //按下'.'按钮时,判断当前文本框内字符串中含不含'.',如果已含,则不允许再插入'.'
      if (temp == buttonDot && clickable == true) {
        boolean isDot = false;
        if (textAnswer.getText().length() == 0)
          isDot = true;
        for (int i = 0; i < textAnswer.getText().length(); i++)//小数点判断,有就为TRUE
          if ('.' == textAnswer.getText().charAt(i)) {
            isDot = true;
            break;
          }
        if (isDot == false)
          textAnswer.setText(textAnswer.getText() + ".");
      }
      if ( (temp == buttonAdd || temp == buttonSub || temp == buttonMul ||
            temp == buttonDiv)&& clickable == true) {
        //'+'操作
        if (temp == buttonAdd) {
         sele();
          prekey = key = 0;
        }
        //'-'操作
        if (temp == buttonSub) {
          sele();
          prekey = key = 1;
        }
        //'*'操作
        if (temp == buttonMul) {

⌨️ 快捷键说明

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