Topic: 请试试我这个用Java作的计算器,提出建议的有奖

  Print this page

1.请试试我这个用Java作的计算器,提出建议的有奖 Copy to clipboard
Posted by: ljy0000
Posted on: 2005-07-25 10:43

这个计算器用Java制作完成,能完成基本的四则运算,不支持“先乘除后加减”原则,可以将数据暂时存入内存,可设置计算精度,可将数据拷到剪贴板。

请大家帮助找出Bug,或提出建议,如增加什么样的新功能,删除哪个原来有的不常用的旧功能,等等,有如上建议的,如果你要源代码,我就发给你,当然,如果大家强烈要求要源代码,我会公布的。

使用说明:
1、“MC、MR、MS”分别为清除内存、显示内存数据、存入内存。
2、“%”为除以100,“佰”为乘以100。
3、“AC”为归零,“C”为暂时清屏。
4、在显示数据的屏幕上单击右键,出现菜单,可将数据拷到剪贴板和设置计算精度。

(缩略图,点击图片链接看原图)

2.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: ljy0000
Posted on: 2005-07-25 13:54

双击运行

Calculator.jar (22.87k)

3.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: whyuaou
Posted on: 2005-08-15 16:07

很简单啊 还没记事本难呢 放在java基础里还差不多 最好有源代码

4.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: YiOnLine
Posted on: 2005-08-17 16:53

同意楼上的说法,我是新手,不过这程序用其它工具实现不难,所以楼主能提供源码给新手入门就好了.

5.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: tzutolin
Posted on: 2005-08-18 08:04

剛剛 run 了一下, 做的還不錯, 不知道能不能請樓主提供源碼讓各位朋友一起學習?

6.发源码看一下吧 [Re: ljy0000] Copy to clipboard
Posted by: xyhx1982
Posted on: 2005-08-22 09:50

发源码看一下吧

7.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: ljy0000
Posted on: 2005-08-22 09:58


/*
* demo.java
*
* Created on 2005年7月24日, 上午11:35
*/

import java.awt.BorderLayout;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.swing.BorderFactory;

import javax.swing.JButton;
import javax.swing.border.BevelBorder;

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import java.awt.event.ActionEvent;
import java.lang.String;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.event.DocumentListener;

import javax.swing.event.DocumentEvent;

/**
*
* @author root
*/
public class Calculator extends javax.swing.JFrame{

/** Creates new form demo */
public Calculator() {
initComponents();
bigDecimalTextField=new BigDecimal(BigInteger.ZERO);
bigDecimalTemp=new BigDecimal(BigInteger.ZERO);
bigDecimalM=new BigDecimal(BigInteger.ZERO);
flag=0;
usedDot=false;
usingDot=false;
isEditing=true;
jTextFieldM.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
jTextField1.setBorder(BorderFactory.createLineBorder(new Color(102, 102, 153),2));
getContentPane().setBackground(new Color(153,204,255));
s=new java.lang.String[31];
for(int i=0;i<31;i++){
s[i]=java.lang.String.valueOf(i);
}
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jButtonMC = new javax.swing.JButton();
jButtonMR = new javax.swing.JButton();
jButtonMS = new javax.swing.JButton();
jButton010 = new javax.swing.JButton();
jButtonDivide = new javax.swing.JButton();
jButtonFlag = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButtonmultiply = new javax.swing.JButton();
jButtonAC = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButtonSub = new javax.swing.JButton();
jButtonC = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButtonAdd = new javax.swing.JButton();
jButton00 = new javax.swing.JButton();
jButton0 = new javax.swing.JButton();
jButtonDot = new javax.swing.JButton();
jButtonAmount = new javax.swing.JButton();
jTextFieldM = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTextField1 = new javax.swing.JTextArea();

getContentPane().setLayout(new java.awt.GridBagLayout());

setTitle("\u8ba1\u7b97\u5668");
setBackground(new java.awt.Color(153, 204, 255));
setForeground(java.awt.Color.white);
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jButtonMC.setBackground(new java.awt.Color(0, 153, 0));
jButtonMC.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonMC.setForeground(new java.awt.Color(204, 204, 0));
jButtonMC.setText("MC");
jButtonMC.setToolTipText("\u6e05\u9664\u5b58\u50a8\u6570\u636e");
jButtonMC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonMCActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButtonMC, gridBagConstraints);

jButtonMR.setBackground(new java.awt.Color(0, 153, 0));
jButtonMR.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonMR.setForeground(new java.awt.Color(204, 204, 0));
jButtonMR.setText("MR");
jButtonMR.setToolTipText("\u663e\u793a\u5b58\u50a8\u6570\u636e");
jButtonMR.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonMRActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButtonMR, gridBagConstraints);

jButtonMS.setBackground(new java.awt.Color(0, 153, 0));
jButtonMS.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonMS.setForeground(new java.awt.Color(204, 204, 0));
jButtonMS.setText("MS");
jButtonMS.setToolTipText("\u5b58\u50a8\u6570\u636e");
jButtonMS.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonMSActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButtonMS, gridBagConstraints);

jButton010.setBackground(new java.awt.Color(0, 153, 0));
jButton010.setFont(new java.awt.Font("Dialog", 0, 14));
jButton010.setForeground(new java.awt.Color(204, 204, 0));
jButton010.setText("\u5e73\u65b9");
jButton010.setToolTipText("\u6c42\u5e73\u65b9");
jButton010.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton010ActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton010, gridBagConstraints);

jButtonDivide.setBackground(new java.awt.Color(0, 153, 0));
jButtonDivide.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonDivide.setForeground(new java.awt.Color(204, 204, 0));
jButtonDivide.setText("\u9664");
jButtonDivide.setToolTipText("\u4e24\u6570\u76f8\u9664");
jButtonDivide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDivideActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipadx = 20;
gridBagConstraints.ipady = 15;
gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
getContentPane().add(jButtonDivide, gridBagConstraints);

jButtonFlag.setBackground(new java.awt.Color(0, 153, 0));
jButtonFlag.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonFlag.setForeground(new java.awt.Color(204, 204, 0));
jButtonFlag.setText("+/-");
jButtonFlag.setToolTipText("\u6c42\u76f8\u53cd\u6570");
jButtonFlag.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonFlagActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButtonFlag, gridBagConstraints);

jButton7.setBackground(new java.awt.Color(0, 153, 0));
jButton7.setFont(new java.awt.Font("Dialog", 0, 18));
jButton7.setForeground(new java.awt.Color(204, 204, 0));
jButton7.setText("\u67d2");
jButton7.setToolTipText("\u6570\u5b577");
jButton7.setActionCommand("7");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton7, gridBagConstraints);

jButton8.setBackground(new java.awt.Color(0, 153, 0));
jButton8.setFont(new java.awt.Font("Dialog", 0, 18));
jButton8.setForeground(new java.awt.Color(204, 204, 0));
jButton8.setText("\u634c");
jButton8.setToolTipText("\u6570\u5b578");
jButton8.setActionCommand("8");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton8, gridBagConstraints);

jButton9.setBackground(new java.awt.Color(0, 153, 0));
jButton9.setFont(new java.awt.Font("Dialog", 0, 18));
jButton9.setForeground(new java.awt.Color(204, 204, 0));
jButton9.setText("\u7396");
jButton9.setToolTipText("\u6570\u5b579");
jButton9.setActionCommand("9");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton9, gridBagConstraints);

jButtonmultiply.setBackground(new java.awt.Color(0, 153, 0));
jButtonmultiply.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonmultiply.setForeground(new java.awt.Color(204, 204, 0));
jButtonmultiply.setText("\u4e58");
jButtonmultiply.setToolTipText("\u4e24\u6570\u76f8\u4e58");
jButtonmultiply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonmultiplyActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipadx = 20;
gridBagConstraints.ipady = 15;
getContentPane().add(jButtonmultiply, gridBagConstraints);

jButtonAC.setBackground(new java.awt.Color(0, 153, 0));
jButtonAC.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonAC.setForeground(new java.awt.Color(204, 204, 0));
jButtonAC.setText("AC");
jButtonAC.setToolTipText("\u5f52\u96f6");
jButtonAC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonACActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButtonAC, gridBagConstraints);

jButton4.setBackground(new java.awt.Color(0, 153, 0));
jButton4.setFont(new java.awt.Font("Dialog", 0, 18));
jButton4.setForeground(new java.awt.Color(204, 204, 0));
jButton4.setText("\u8086");
jButton4.setToolTipText("\u6570\u5b574");
jButton4.setActionCommand("4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton4, gridBagConstraints);

jButton5.setBackground(new java.awt.Color(0, 153, 0));
jButton5.setFont(new java.awt.Font("Dialog", 0, 18));
jButton5.setForeground(new java.awt.Color(204, 204, 0));
jButton5.setText("\u4f0d");
jButton5.setToolTipText("\u6570\u5b575");
jButton5.setActionCommand("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton5, gridBagConstraints);

jButton6.setBackground(new java.awt.Color(0, 153, 0));
jButton6.setFont(new java.awt.Font("Dialog", 0, 18));
jButton6.setForeground(new java.awt.Color(204, 204, 0));
jButton6.setText("\u9646");
jButton6.setToolTipText("\u6570\u5b576");
jButton6.setActionCommand("6");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton6, gridBagConstraints);

jButtonSub.setBackground(new java.awt.Color(0, 153, 0));
jButtonSub.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonSub.setForeground(new java.awt.Color(204, 204, 0));
jButtonSub.setText("\u51cf");
jButtonSub.setToolTipText("\u4e24\u6570\u76f8\u51cf");
jButtonSub.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSubActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipadx = 20;
gridBagConstraints.ipady = 15;
getContentPane().add(jButtonSub, gridBagConstraints);

jButtonC.setBackground(new java.awt.Color(0, 153, 0));
jButtonC.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonC.setForeground(new java.awt.Color(204, 204, 0));
jButtonC.setText("C");
jButtonC.setToolTipText("\u6e05\u9664\u5c4f\u5e55");
jButtonC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonCActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButtonC, gridBagConstraints);

jButton1.setBackground(new java.awt.Color(0, 153, 0));
jButton1.setFont(new java.awt.Font("Dialog", 0, 18));
jButton1.setForeground(new java.awt.Color(204, 204, 0));
jButton1.setText("\u58f9");
jButton1.setToolTipText("\u6570\u5b571");
jButton1.setActionCommand("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton1, gridBagConstraints);

jButton2.setBackground(new java.awt.Color(0, 153, 0));
jButton2.setFont(new java.awt.Font("Dialog", 0, 18));
jButton2.setForeground(new java.awt.Color(204, 204, 0));
jButton2.setText("\u8d30");
jButton2.setToolTipText("\u6570\u5b572");
jButton2.setActionCommand("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton2, gridBagConstraints);

jButton3.setBackground(new java.awt.Color(0, 153, 0));
jButton3.setFont(new java.awt.Font("Dialog", 0, 18));
jButton3.setForeground(new java.awt.Color(204, 204, 0));
jButton3.setText("\u53c1");
jButton3.setToolTipText("\u6570\u5b573");
jButton3.setActionCommand("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton3, gridBagConstraints);

jButtonAdd.setBackground(new java.awt.Color(0, 153, 0));
jButtonAdd.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonAdd.setForeground(new java.awt.Color(204, 204, 0));
jButtonAdd.setText("\u52a0");
jButtonAdd.setToolTipText("\u4e24\u6570\u76f8\u52a0");
jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonAddActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipadx = 20;
gridBagConstraints.ipady = 15;
getContentPane().add(jButtonAdd, gridBagConstraints);

jButton00.setBackground(new java.awt.Color(0, 153, 0));
jButton00.setFont(new java.awt.Font("Dialog", 0, 18));
jButton00.setForeground(new java.awt.Color(204, 204, 0));
jButton00.setText("\u4f70");
jButton00.setToolTipText("\u4e58100");
jButton00.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton00ActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton00, gridBagConstraints);

jButton0.setBackground(new java.awt.Color(0, 153, 0));
jButton0.setFont(new java.awt.Font("Dialog", 0, 18));
jButton0.setForeground(new java.awt.Color(204, 204, 0));
jButton0.setText("\u96f6");
jButton0.setToolTipText("\u6570\u5b570");
jButton0.setActionCommand("0");
jButton0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNumberActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButton0, gridBagConstraints);

jButtonDot.setBackground(new java.awt.Color(0, 153, 0));
jButtonDot.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonDot.setForeground(new java.awt.Color(204, 204, 0));
jButtonDot.setText("\u70b9");
jButtonDot.setToolTipText("\u5c0f\u6570\u70b9");
jButtonDot.setActionCommand(".");
jButtonDot.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDotActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 20;
getContentPane().add(jButtonDot, gridBagConstraints);

jButtonAmount.setBackground(new java.awt.Color(0, 153, 0));
jButtonAmount.setFont(new java.awt.Font("Dialog", 0, 18));
jButtonAmount.setForeground(new java.awt.Color(204, 204, 0));
jButtonAmount.setText("\u7b49\u4e8e");
jButtonAmount.setToolTipText("\u6c42\u503c");
jButtonAmount.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonAmountActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 5;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipadx = 20;
gridBagConstraints.ipady = 15;
getContentPane().add(jButtonAmount, gridBagConstraints);

jTextFieldM.setBackground(new java.awt.Color(0, 204, 102));
jTextFieldM.setEditable(false);
jTextFieldM.setFont(new java.awt.Font("Dialog", 0, 30));
jTextFieldM.setForeground(new java.awt.Color(255, 0, 0));
jTextFieldM.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextFieldM.setToolTipText("\u663e\u793a\u5b58\u50a8\u72b6\u6001");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 10;
gridBagConstraints.ipady = 20;
getContentPane().add(jTextFieldM, gridBagConstraints);

jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jTextField1.setBackground(new java.awt.Color(230, 230, 230));
jTextField1.setEditable(false);
jTextField1.setFont(new java.awt.Font("Dialog", 0, 24));
jTextField1.setForeground(new java.awt.Color(153, 0, 0));
jTextField1.setText("0");
jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jTextField1MouseReleased(evt);
}
});

jScrollPane1.setViewportView(jTextField1);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
getContentPane().add(jScrollPane1, gridBagConstraints);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-450)/2, (screenSize.height-400)/2, 450, 400);
}
// </editor-fold>//GEN-END:initComponents

private void jTextField1MouseReleased (java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MouseReleased
// TODO 将在此处增加您的处理代码:
if(evt.isPopupTrigger()){
JPopupMenu popup=new JPopupMenu();
JMenuItem item=new JMenuItem("复制到剪贴板");
JMenuItem item2=new JMenuItem("设置精度");
item.setFont(new Font("宋体",Font.PLAIN,14));
item2.setFont(new Font("宋体",Font.PLAIN,14));
item.setBackground(Color.white);
item2.setBackground(Color.white);
item.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jTextField1.selectAll();
jTextField1.copy();
jTextField1.select(0, 0);
}
});
item2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
java.lang.String temp=(java.lang.String)JOptionPane.showInputDialog(Calculator.this,
"请选择小数点后的位数","设置精度",JOptionPane.PLAIN_MESSAGE,null,s, s[0]);
for(int i=0;i<31;i++){
if(s[i].equals(temp)){
DotNumber=i;
}
}
}
});
popup.add(item);
popup.addSeparator();
popup.add(item2);
popup.show(jTextField1,evt.getX(),evt.getY());
System.out.println(DotNumber);
}
}//GEN-LAST:event_jTextField1MouseReleased

private void jButtonAmountActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAmountActionPerformed
// TODO 将在此处增加您的处理代码:
switch(flag){
case 1:{
bigDecimalTextField=bigDecimalTemp.add(bigDecimalTextField).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
break;
}
case 2:{
bigDecimalTextField=bigDecimalTemp.subtract(bigDecimalTextField).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
break;
}
case 3:{
bigDecimalTextField=bigDecimalTemp.multiply(bigDecimalTextField).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
break;
}
case 4:{
if(bigDecimalTextField.signum()==0) break;
bigDecimalTextField=bigDecimalTemp.divide(bigDecimalTextField,DotNumber,BigDecimal.ROUND_HALF_UP);
break;
}
}
isEditing=false;
usedDot=false;
usingDot=false;
jTextField1.setText(bigDecimalTextField.setScale(DotNumber,BigDecimal.ROUND_HALF_UP).toString());
bigDecimalTemp=new BigDecimal(jTextField1.getText()).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
flag=5;
}//GEN-LAST:event_jButtonAmountActionPerformed

private void jButtonDotActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDotActionPerformed
// TODO 将在此处增加您的处理代码:
if(!usedDot){
usedDot=true;
usingDot=true;
}
}//GEN-LAST:event_jButtonDotActionPerformed

private void jButton00ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton00ActionPerformed
// TODO 将在此处增加您的处理代码:
bigDecimalTextField=bigDecimalTextField.multiply(new BigDecimal("100")).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
bigDecimalTemp=bigDecimalTextField.multiply(new BigDecimal("100")).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
jTextField1.setText(bigDecimalTextField.setScale(DotNumber,BigDecimal.ROUND_HALF_UP).toString());
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButton00ActionPerformed

private void jButtonNumberActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNumberActionPerformed
// TODO 将在此处增加您的处理代码:
String s;
if(isEditing){
s=jTextField1.getText();
}else{
s="0";
}
StringBuffer buffer=new StringBuffer(s);
if(usingDot){
bigDecimalTextField=new BigDecimal(buffer.append(".").append(evt.getActionCommand()).toString());
usingDot=false;
}else{
bigDecimalTextField=new BigDecimal(buffer.append(evt.getActionCommand()).toString());
}
jTextField1.setText(bigDecimalTextField.toString());
isEditing=true;
}//GEN-LAST:event_jButtonNumberActionPerformed

private void jButtonAddActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
// TODO 将在此处增加您的处理代码:
if(flag!=5&&isEditing)
jButtonAmountActionPerformed(evt);
bigDecimalTemp=new BigDecimal(jTextField1.getText()).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
flag=1;
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonAddActionPerformed

private void jButtonSubActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSubActionPerformed
// TODO 将在此处增加您的处理代码:
if(flag!=5&&isEditing)
jButtonAmountActionPerformed(evt);
bigDecimalTemp=new BigDecimal(jTextField1.getText()).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
flag=2;
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonSubActionPerformed

private void jButtonmultiplyActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonmultiplyActionPerformed
// TODO 将在此处增加您的处理代码:
if(flag!=5&&isEditing)
jButtonAmountActionPerformed(evt);
bigDecimalTemp=new BigDecimal(jTextField1.getText()).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
flag=3;
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonmultiplyActionPerformed

private void jButtonDivideActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDivideActionPerformed
// TODO 将在此处增加您的处理代码:
if(flag!=5&&isEditing)
jButtonAmountActionPerformed(evt);
bigDecimalTemp=new BigDecimal(jTextField1.getText()).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
flag=4;
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonDivideActionPerformed

private void jButtonCActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCActionPerformed
// TODO 将在此处增加您的处理代码:
bigDecimalTextField=new BigDecimal(BigInteger.ZERO);
jTextField1.setText(bigDecimalTextField.toString());
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonCActionPerformed

private void jButtonACActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonACActionPerformed
// TODO 将在此处增加您的处理代码:
bigDecimalTextField=new BigDecimal(BigInteger.ZERO);
bigDecimalTemp=new BigDecimal(BigInteger.ZERO);
jTextField1.setText(bigDecimalTextField.toString());
isEditing=true;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonACActionPerformed

private void jButtonFlagActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFlagActionPerformed
// TODO 将在此处增加您的处理代码:
bigDecimalTextField=bigDecimalTextField.negate().setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
jTextField1.setText(bigDecimalTextField.setScale(DotNumber,BigDecimal.ROUND_HALF_UP).toString());
bigDecimalTemp=new BigDecimal(jTextField1.getText()).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonFlagActionPerformed

private void jButtonMSActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMSActionPerformed
// TODO 将在此处增加您的处理代码:
bigDecimalM=new BigDecimal(jTextField1.getText()).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
jTextFieldM.setText("M");
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonMSActionPerformed

private void jButton010ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton010ActionPerformed
// TODO 将在此处增加您的处理代码:
bigDecimalTextField=bigDecimalTextField.multiply(bigDecimalTextField);
jTextField1.setText(bigDecimalTextField.setScale(DotNumber,BigDecimal.ROUND_HALF_UP).toString());
bigDecimalTemp=new BigDecimal(jTextField1.getText());
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButton010ActionPerformed

private void jButtonMRActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMRActionPerformed
// TODO 将在此处增加您的处理代码:
jTextField1.setText(bigDecimalM.setScale(DotNumber,BigDecimal.ROUND_HALF_UP).toString());
bigDecimalTextField=new BigDecimal(jTextField1.getText()).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonMRActionPerformed

private void jButtonMCActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMCActionPerformed
// TODO 将在此处增加您的处理代码:
jTextFieldM.setText(" ");
bigDecimalM=new BigDecimal(BigInteger.ZERO).setScale(DotNumber,BigDecimal.ROUND_HALF_UP);
isEditing=false;
usedDot=false;
usingDot=false;
}//GEN-LAST:event_jButtonMCActionPerformed

/** Exit the Application */
private void exitForm (java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new Calculator().show();
}

// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JButton jButton0;
private javax.swing.JButton jButton00;
private javax.swing.JButton jButton010;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JButton jButtonAC;
private javax.swing.JButton jButtonAdd;
private javax.swing.JButton jButtonAmount;
private javax.swing.JButton jButtonC;
private javax.swing.JButton jButtonDivide;
private javax.swing.JButton jButtonDot;
private javax.swing.JButton jButtonFlag;
private javax.swing.JButton jButtonMC;
private javax.swing.JButton jButtonMR;
private javax.swing.JButton jButtonMS;
private javax.swing.JButton jButtonSub;
private javax.swing.JButton jButtonmultiply;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextField1;
private javax.swing.JTextField jTextFieldM;
// 变量声明结束//GEN-END:variables
private BigDecimal bigDecimalTextField;
private BigDecimal bigDecimalTemp;
private BigDecimal bigDecimalM;
private int flag;
private boolean usedDot;
private boolean usingDot;
private boolean isEditing;
private int DotNumber=15;
private java.lang.String[] s;

}



calculator_src.zip (6.92k)

8.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: ljy0000
Posted on: 2005-08-22 10:02

程序用Netbean4.1完成,下面是修改后的版本

Calculator.jar (23.26k)

9.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: Leonardo
Posted on: 2005-09-30 13:34

不错。

10.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: Learner2005
Posted on: 2005-10-12 20:38

谢谢!

11.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: 此时彼刻
Posted on: 2005-11-16 17:59

我上学期也做了一个,当时的JAVA作业,请指教

jisuanqi.java (16.48k)

12.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: tzutolin
Posted on: 2005-11-16 22:16

不錯不錯, 值得嘉獎, 希望你再接再厲.

13.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: zhangminamen
Posted on: 2005-11-17 08:50

有个地方需要改进一下,就是点了小数点的时候,不能立即显视出来,要点下一个数的时候才会出现,这里要改一下

14.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: zhangminamen] Copy to clipboard
Posted by: ysg0222
Posted on: 2005-11-17 10:36

对方随风倒似

15.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: zhangminamen] Copy to clipboard
Posted by: ljy0000
Posted on: 2005-11-17 21:48

zhangminamen wrote:
有个地方需要改进一下,就是点了小数点的时候,不能立即显视出来,要点下一个数的时候才会出现,这里要改一下


谢谢这位同学指出程序中的问题,下面是修改后的版本,请试用,程序是用BigDecimal类存储和运算,只要不进行除法运算,就不会出现精度丢失,请大家对此提出自己的看法和意见。

JavaApplication2.jar (30.8k)

16.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: yushang0824
Posted on: 2005-11-18 10:10

你的代码为什么只有一个Class?
还有几百个函数,别人无法看你的代码的,

建议你把按钮上的汉字换成数字和符号吧,要不然着太不习惯呢,

再就是,不要无缘无故的在小数点后,显示一堆零,看得我眼花

17.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: tzutolin
Posted on: 2005-11-18 12:14

ljy0000 wrote:
谢谢这位同学指出程序中的问题,下面是修改后的版本,请试用,程序是用BigDecimal类存储和运算,只要不进行除法运算,就不会出现精度丢失,请大家对此提出自己的看法和意见。

Good Job, 又比上一版好多了! 如果能接受鍵盤輸入的話就更完美了. 加分 Big Smile

18.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: tzutolin
Posted on: 2005-11-18 12:18

還有介面如果能夠更美觀的話, 比如說加入 L&F 的支援, 那就更好了!

19.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: ljy0000
Posted on: 2005-11-19 09:27

yushang0824 wrote:
你的代码为什么只有一个Class?
还有几百个函数,别人无法看你的代码的,

建议你把按钮上的汉字换成数字和符号吧,要不然着太不习惯呢,

再就是,不要无缘无故的在小数点后,显示一堆零,看得我眼花


由于Calculator类中有好多整体控制变量,要是用多个类的话,当然会增加程序的可维护性和代码的可阅读性,但同时增加了变量传递带来的接口的复杂性,而且相比之下用单个类好一些,其实内部有好多内部类的,看看编译后的代码就知道了。若大家有什么降低接口复杂性的建议,我非常乐意听取,并加入到程序中。

tzutolin wrote:
Good Job, 又比上一版好多了! 如果能接受鍵盤輸入的話就更完美了. 加分 Big Smile


接受意见,键盘导航已经加入,可以用键盘的按钮为:
0
1
2
3
4
5
6
7
8
9
+
-
*
/
=
.
共计15个,用相应的键盘上的按键即可。

tzutolin wrote:
還有介面如果能夠更美觀的話, 比如說加入 L&F 的支援, 那就更好了!


接受意见,程序中已经加入Look And Feel切换支持,支持的LAF在Java1.5下有八种LAF,其它平台没试过。

程序在Java 1.5下编译通过,在其它平台的话需要自己编译源代码,源代码在压缩包里有。

JavaApplication2.jar (363.85k)

20.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: tzutolin
Posted on: 2005-11-19 12:00

不錯不錯, 介面好漂亮, 而且鍵盤輸入也很方便, 越來越實用了 Big Smile

21.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: tzutolin
Posted on: 2005-11-19 12:03

貼個圖, 讓大家瞧瞧 Big Smile

22.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: lovelymiss
Posted on: 2005-12-20 19:36

有一个问题:
比如我要求 0.2+0.3=?
我直接输入 .2+.3
结果=3.2
第二个数字0.3,直接输入.3时是不支持的,程序认为是3而不是0.3

23.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: lovelymiss] Copy to clipboard
Posted by: ljy0000
Posted on: 2005-12-22 22:01

lovelymiss wrote:
有一个问题:
比如我要求 0.2+0.3=?
我直接输入 .2+.3
结果=3.2
第二个数字0.3,直接输入.3时是不支持的,程序认为是3而不是0.3


已经修正上述问题。

JavaApplication2.jar (364.32k)

24.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: qqwww666
Posted on: 2006-03-15 21:02

最好数字能从右面显示

25.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: JavaandC
Posted on: 2006-03-16 12:44

不支持小键盘,建议增加对小键盘的支持。

26.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: yagebuqq
Posted on: 2006-05-04 22:39

你的程序我还没有测试!
我想问你一个问题.
最多可以输入多少位操作数??
如果我要用的操作数在进行相加,相乘,相除时超出最大了怎么办?

27.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: prince1234
Posted on: 2006-06-05 16:48

显示的数值不符合显示里计算器的显示习惯哦
嘿嘿
可以把功能做大点
十进制和十六进制转换
xp里面那个计算器,不支持十进制浮点数到十六进制的转换哦
支持搂主

28.Re:请试试我这个用Java作的计算器,提出建议的有奖 [Re: ljy0000] Copy to clipboard
Posted by: yuxiangyu
Posted on: 2006-06-23 21:45

速度慢了,优化一下吧,不然跑起来太慢了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923