import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Calculator extends javax.swing.JFrame {
public Calculator() {
initComponents();
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLayeredPane2 = new javax.swing.JLayeredPane();
jPanel2 = new javax.swing.JPanel();
jSeparator1 = new javax.swing.JSeparator();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
zero = new javax.swing.JButton();
dian = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
dengyu = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();
zhenngfu = new javax.swing.JButton();
jPanel4 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation
(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(204, 0, 204));
setExtendedState(1);
setForeground(new java.awt.Color(255, 153, 255));
setResizable(false);
jPanel2.setOpaque(false);
jSeparator1.setForeground(java.awt.Color.blue);
jTextField1.setEditable(false);
jTextField1.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jTextField1.setForeground(java.awt.Color.blue);
jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jTextField1.setText("0");
jTextField1.setOpaque(false);
jButton1.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton1.setForeground(java.awt.Color.blue);
jButton1.setText("1");
jButton1.setOpaque(false);
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton1MouseReleased(evt);
}
});
jButton2.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton2.setForeground(java.awt.Color.blue);
jButton2.setText("2");
jButton2.setOpaque(false);
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton2MouseReleased(evt);
}
});
jButton3.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton3.setForeground(java.awt.Color.blue);
jButton3.setText("3");
jButton3.setOpaque(false);
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton3MouseReleased(evt);
}
});
jButton4.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton4.setForeground(java.awt.Color.blue);
jButton4.setText("4");
jButton4.setOpaque(false);
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton4MouseReleased(evt);
}
});
jButton5.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton5.setForeground(java.awt.Color.blue);
jButton5.setText("5");
jButton5.setOpaque(false);
jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton5MouseReleased(evt);
}
});
jButton6.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton6.setForeground(java.awt.Color.blue);
jButton6.setText("6");
jButton6.setOpaque(false);
jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton6MouseReleased(evt);
}
});
jButton7.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton7.setForeground(java.awt.Color.blue);
jButton7.setText("7");
jButton7.setOpaque(false);
jButton7.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton7MouseReleased(evt);
}
});
jButton8.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton8.setForeground(java.awt.Color.blue);
jButton8.setText("8");
jButton8.setOpaque(false);
jButton8.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton8MouseReleased(evt);
}
});
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton9.setForeground(java.awt.Color.blue);
jButton9.setText("9");
jButton9.setOpaque(false);
jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton9MouseReleased(evt);
}
});
zero.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
zero.setForeground(java.awt.Color.blue);
zero.setText("0");
zero.setActionCommand("");
zero.setOpaque(false);
zero.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
zeroMouseReleased(evt);
}
});
dian.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
dian.setForeground(java.awt.Color.blue);
dian.setText(".");
dian.setOpaque(false);
dian.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
dianMouseReleased(evt);
}
});
jButton12.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton12.setForeground(java.awt.Color.blue);
jButton12.setText("+");
jButton12.setOpaque(false);
jButton12.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton12MouseReleased(evt);
}
});
jButton13.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton13.setForeground(java.awt.Color.blue);
jButton13.setText("-");
jButton13.setOpaque(false);
jButton13.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton13MouseReleased(evt);
}
});
jButton14.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton14.setForeground(java.awt.Color.blue);
jButton14.setText("*");
jButton14.setOpaque(false);
jButton14.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton14MouseReleased(evt);
}
});
jButton15.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton15.setForeground(java.awt.Color.blue);
jButton15.setText("/");
jButton15.setOpaque(false);
jButton15.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton15MouseReleased(evt);
}
});
dengyu.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
dengyu.setForeground(java.awt.Color.blue);
dengyu.setText("=");
dengyu.setOpaque(false);
dengyu.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
dengyuMouseReleased(evt);
}
});
jButton17.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton17.setForeground(java.awt.Color.blue);
jButton17.setText("←");
jButton17.setOpaque(false);
jButton17.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton17MouseReleased(evt);
}
});
jButton18.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
jButton18.setForeground(java.awt.Color.blue);
jButton18.setText("C");
jButton18.setOpaque(false);
jButton18.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton18MouseReleased(evt);
}
});
zhenngfu.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
zhenngfu.setForeground(java.awt.Color.blue);
zhenngfu.setText("+/-");
zhenngfu.setOpaque(false);
zhenngfu.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
zhenngfuMouseReleased(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(298, Short.MAX_VALUE)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(126, 126, 126))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(zhenngfu))
.addGap(27, 27, 27)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
.addGap(131, 131, 131)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(202, 202, 202)
.addComponent(dian, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(2, 2, 2)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton17, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton18, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(dengyu, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(26, 26, 26))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator1)
.addComponent(jTextField1))
.addContainerGap()))))
);
jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {dengyu, jButton1, jButton12, jButton13, jButton14, jButton15, jButton17, jButton18, jButton2, jButton4, jButton5, jButton6, jButton7, jButton8, jButton9, zhenngfu});
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(282, Short.MAX_VALUE)
.addComponent(jButton12)
.addGap(18, 18, 18))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(17, 17, 17)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButton15)
.addComponent(jButton17))
.addGap(26, 26, 26)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6)
.addComponent(jButton14)
.addComponent(jButton18))
.addGap(29, 29, 29)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton13))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(zero)
.addComponent(dian)
.addComponent(zhenngfu)))
.addComponent(dengyu, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(17, Short.MAX_VALUE)))
);
jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {dian, jButton1, jButton12, jButton13, jButton14, jButton15, jButton17, jButton18, jButton2, jButton3, jButton4, jButton5, jButton6, jButton7, jButton8, jButton9, zhenngfu});
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg")); // NOI18N
jLabel1.setText("jLabel1");
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 495, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 495, javax.swing.GroupLayout.PREFERRED_SIZE))
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 337, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE))
);
javax.swing.GroupLayout jLayeredPane2Layout = new javax.swing.GroupLayout(jLayeredPane2);
jLayeredPane2.setLayout(jLayeredPane2Layout);
jLayeredPane2Layout.setHorizontalGroup(
jLayeredPane2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 495, Short.MAX_VALUE)
.addGroup(jLayeredPane2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jLayeredPane2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jLayeredPane2Layout.setVerticalGroup(
jLayeredPane2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 337, Short.MAX_VALUE)
.addGroup(jLayeredPane2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jLayeredPane2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jLayeredPane2.setLayer(jPanel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane2.setLayer(jPanel4, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLayeredPane2)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLayeredPane2)
);
pack();
}//
private void zhenngfuMouseReleased(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String str = jTextField1.getText();
StringBuffer strBuf = new StringBuffer(str);
Character c = strBuf.charAt(0);
if (c.equals('+')) {
strBuf.setCharAt(0, '-');
} else if (c.equals('-')) {
strBuf.deleteCharAt(0);
} else {
strBuf.insert(0, '-');
}
jTextField1.setText(strBuf.toString());
}
private void jButton18MouseReleased(java.awt.event.MouseEvent evt) {
jTextField1.setText("0");
}
private void jButton17MouseReleased(java.awt.event.MouseEvent evt) {
String str = jTextField1.getText();
// TODO add your handling code here:
StringBuffer strBuf = new StringBuffer(str);
if (str.length() != 1) {
strBuf.deleteCharAt(str.length() - 1);
jTextField1.setText(strBuf.toString());
} else {
jTextField1.setText("0");
}
// TODO add your handling code here:
}
private void dengyuMouseReleased(java.awt.event.MouseEvent evt) {
String str = new String(jTextField1.getText()); // TODO add your handling code here:
if (str.endsWith("+") || str.endsWith("-") || str.endsWith(".")) {
str = str + "0";
} else if (str.endsWith("*") || str.endsWith("/")) {
str = str + "1";
}
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName("js");
Double result = null;
try {
result = (Double) se.eval_r("(" + str + ")*1.0");
} catch (ScriptException ex) {
Logger.getLogger(Calculator.class.getName()).log(Level.SEVERE, null, ex);
}
jTextField1.setText(result.toString());
}
private void jButton15MouseReleased(java.awt.event.MouseEvent evt) {
setM("/");
// TODO add your handling code here:
}
private void jButton14MouseReleased(java.awt.event.MouseEvent evt) {
setM("*");
// TODO add your handling code here:
}
private void jButton13MouseReleased(java.awt.event.MouseEvent evt) {
setM("-");
// TODO add your handling code here:
}
private void jButton12MouseReleased(java.awt.event.MouseEvent evt) {
setM("+");
// TODO add your handling code here:
}
private void dianMouseReleased(java.awt.event.MouseEvent evt) {
point1();
// TODO add your handling code here:
}
private void zeroMouseReleased(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
setT("0");
}
private void jButton9MouseReleased(java.awt.event.MouseEvent evt) {
setT("9");
// TODO add your handling code here:
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton8MouseReleased(java.awt.event.MouseEvent evt) {
setT("8");
// TODO add your handling code here:
}
private void jButton7MouseReleased(java.awt.event.MouseEvent evt) {
setT("7");
// TODO add your handling code here:
}
private void jButton6MouseReleased(java.awt.event.MouseEvent evt) {
setT("6");
// TODO add your handling code here:
}
private void jButton5MouseReleased(java.awt.event.MouseEvent evt) {
setT("5");
// TODO add your handling code here:
}
private void jButton4MouseReleased(java.awt.event.MouseEvent evt) {
setT("4");
// TODO add your handling code here:
}
private void jButton3MouseReleased(java.awt.event.MouseEvent evt) {
setT("3");
// TODO add your handling code here:
}
private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {
setT("2"); // TODO add your handling code here:
}
private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
setT("1");
}
public void setM(String str0) {
String str = jTextField1.getText();
StringBuilder strBuf = new StringBuilder(str);
if (str.endsWith(".") || str.endsWith("+") || str.endsWith("-") || str.endsWith("*") || str.endsWith("/")) {
strBuf.deleteCharAt(str.length() - 1);
strBuf.append(str0);
jTextField1.setText(strBuf.toString());
} else {
// if (!( (str.length() - 1) == strBuf.lastIndexOf("+") || (str.length() - 1) == strBuf.lastIndexOf("-") || (str.length() - 1) == strBuf.lastIndexOf("*") || (str.length() - 1) == strBuf.lastIndexOf("/"))) {
strBuf.append(str0);
jTextField1.setText(strBuf.toString());
//
}
}
}
public void setT(String str0) {
String str = jTextField1.getText();
StringBuffer strBuf = new StringBuffer(str);
if (!str.contains("+") && !str.contains("-") && !str.contains("*") && !str.contains("/")) {
if (!str.equals("0")) {
strBuf.append(str0);
jTextField1.setText(strBuf.toString());
} else {
jTextField1.setText(str0.toString());
}
} else {
if (((str.lastIndexOf("+") == str.length() - 2) || (str.lastIndexOf("-") == str.length() - 2) || (str.lastIndexOf("*") == str.length() - 2) || (str.lastIndexOf("/") == str.length() - 2))) {
if (str.endsWith("0")) {
strBuf.deleteCharAt(str.length() - 1);
strBuf.append(str0);
jTextField1.setText(strBuf.toString());
} else {
strBuf.append(str0);
jTextField1.setText(strBuf.toString());
}
} else {
strBuf.append(str0);
jTextField1.setText(strBuf.toString());
}
}
}
public void point1() {
String str = jTextField1.getText();
StringBuffer strBuf = new StringBuffer(str);
if (!((str.length() - 1) == strBuf.lastIndexOf("."))) {
// if (!((str.length() - 1) == strBuf.lastIndexOf(".") || (str.length() - 1) == strBuf.lastIndexOf("+") || (str.length() - 1) == strBuf.lastIndexOf("-") || (str.length() - 1) == strBuf.lastIndexOf("*") || (str.length() - 1) == strBuf.lastIndexOf("/"))) {
if (!str.contains("+") && !str.contains("-") && !str.contains("*") && !str.contains("/")) {
if (!str.contains(".")) {
strBuf.append(".");
}
jTextField1.setText(strBuf.toString());
} else {
int m = str.lastIndexOf(".");
//
if (str.contains("+") || str.contains("-") || str.contains("*") || str.contains("/")) {
if ((str.lastIndexOf("+") > m) || (str.lastIndexOf("-") > m) || (str.lastIndexOf("*") > m) || (str.lastIndexOf("/") > m)) {
if (str.endsWith("+") || str.endsWith("-") || str.endsWith("*") || str.endsWith("/")) {
strBuf.append("0.");
jTextField1.setText(strBuf.toString());
} else {
strBuf.append(".");
jTextField1.setText(strBuf.toString());
}
//
}
}
}
}
}
public static void main(String args[]) {
//
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels
()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelEx
ception ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton dengyu;
private javax.swing.JButton dian;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
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.JLabel jLabel1;
private javax.swing.JLayeredPane jLayeredPane2;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel4;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField jTextField1;
private javax.swing.JButton zero;
private javax.swing.JButton zhenngfu;
// End of variables declaration
}