免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1034 | 回复: 0
打印 上一主题 下一主题

Java多功能计算器 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-03 14:53 |只看该作者 |倒序浏览
import java.awt.*;
import java.io.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.DecimalFormat;
class Calculator
  
implements ActionListener {
  
  JFrame frame;
  JTextField textAnswer;
  JPanel panel, panel1, panel2, panel3;
  JMenuBar mainMenu;
  JTextField textMemory;
  JLabel labelMemSpace;
  JButton buttonBk, buttonCe, buttonC;
  JButton button[];
  JButton buttonMC, buttonMR, buttonMS, buttonMAdd;
  JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,
      buttonDiv, buttonMod;
  JButton buttonSqrt, buttonDao, buttonEqual;
  JMenu editMenu, viewMenu, helpMenu;
  JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal;
  DecimalFormat df;
  boolean clickable;
  double memoryd;
  int memoryi;
  double vard, answerd;
  short key = -1, prekey = -1;
  String copy;
  JTextArea help;
  JScrollPane scrollHelp;
  
  public Calculator() {
    clickable = true;
    answerd = 0;
    frame = new JFrame("计算器");
    df = new DecimalFormat("0.##############"); //设置数据输出精度(对于double型值)
    textAnswer = new JTextField(15);
    textAnswer.setText("");
    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.RIGHT));
    panel2.add(textMemory);
    panel2.add(labelMemSpace);
    panel2.add(buttonBk);
    panel2.add(buttonCe);
    panel2.add(buttonC);
    panel3 = new JPanel();
    panel1.add(panel3, BorderLayout.CENTER);
    button = new JButton[10];
    for (int i = 0; i
                        
      //开根号运算
      if (temp == buttonSqrt && clickable == true) {
        String s = textAnswer.getText();
        if (s.charAt(0) == '-') {
          textAnswer.setText("负数不能开根号");
          clickable = false;
        }
        else
          textAnswer.setText(Double.toString(java.lang.Math.sqrt(Double.
              parseDouble(textAnswer.getText()))));
      }
      //平方运算
      if (temp == buttonDao && clickable == true)
      {  
          String s = Double.toString( Double.parseDouble(textAnswer.getText())*Double.parseDouble(textAnswer.getText()));
         
            textAnswer.setText(s);
        
      }
      //按下'+/-'按钮时处理
      if (temp == buttonAddAndSub && clickable == true) {
        boolean isNumber = true;
        String s = textAnswer.getText();
        for (int i = 0; i = '0' && s.charAt(i)

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/23031/showart_179627.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP