} else if(op.equals(\ { result = d1 / d2; if(d2 == 0) result = 0; } else if(op.equals(\ { result = d1 % d2; if(d2 == 0) result = 0; } jTextField1.setText(\ flag = true; } “Back”退格运算 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //if语句判定当前字符串是否为空,若不为空,则将字符串长度减1之后,再赋值给原字符串;否则,将0复制给原字符串。 String s = jTextField1.getText(); if(!s.equals(\ s = s.substring(0,s.length()-1); if(s.equals(\ s = \ jTextField1.setText(s); } “CE”复位运算 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //复位操作 jTextField1.setText(\ d1 = 0; d2 = 0; op = \ flag = false; } “C”清零运算 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //清零操作 jTextField1.setText(\ } “$”千位符分隔符private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) { 运算 // TODO add your handling code here:
6
//千位符分隔符 d1 = Double.parseDouble(jTextField1.getText()); flag = true; op = \ DecimalFormat f = new DecimalFormat(\ jTextField1.setText(\ }
3. 程序的调用关系图
NewJFrame Back CE C 1/x = sqrt $ 0~9 . + - * / % ±
四、全部源程序清单
/* Calculator.java GUI简单计算器 */
package javacalculator; /** *
* @author */
import java.awt.datatransfer.*; import java.text.DecimalFormat;
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame */
7
public NewJFrame() { initComponents(); flag = false; d1 = 0; d2 = 0; op = \
jTextField1.setText(\
clipboard = getToolkit().getSystemClipboard(); }
private Clipboard clipboard; private boolean flag; private double d1; private double d2; private String op;
@SuppressWarnings(\
// jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); 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(); jButton10 = new javax.swing.JButton(); jButton11 = new javax.swing.JButton(); jButton12 = new javax.swing.JButton(); jButton13 = new javax.swing.JButton(); jButton14 = new javax.swing.JButton(); jButton15 = new javax.swing.JButton(); jButton16 = new javax.swing.JButton(); jButton17 = new javax.swing.JButton(); jButton18 = new javax.swing.JButton(); jButton19 = new javax.swing.JButton(); jButton20 = new javax.swing.JButton(); jButton21 = new javax.swing.JButton(); jButton22 = new javax.swing.JButton(); jButton23 = new javax.swing.JButton(); jButton24 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); 8 jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton4.setText(\ jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setText(\ jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setText(\ jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } }); jButton7.setText(\ jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton7ActionPerformed(evt); } }); jButton8.setText(\ jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton8ActionPerformed(evt); } }); jButton9.setText(\ jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton9ActionPerformed(evt); } 9 }); jButton10.setText(\ jButton10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton10ActionPerformed(evt); } }); jButton11.setText(\ jButton11.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton11ActionPerformed(evt); } }); jButton12.setText(\ jButton12.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton12ActionPerformed(evt); } }); jButton13.setText(\ jButton13.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton13ActionPerformed(evt); } }); jButton14.setText(\ jButton14.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton14ActionPerformed(evt); } }); jButton15.setText(\\ jButton15.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton15ActionPerformed(evt); } }); 10