/*
* 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)