Home > Java > javaTutorial > body text

Sample code for implementing a simple calculator applet in Java

WBOY
Release: 2023-04-23 17:58:07
forward
1382 people have browsed it

Sample code for implementing a simple calculator applet in Java

Figure calculator program framework

The code is as follows:

package tst.jframe;
 
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
 
public class javaAP {
 
 /**
  * 各控件对象的建立
  * 
  * */
 private JFrame frame;
 private JTextField result;
 private JButton button_1;
 private JButton button_2;
 private JButton button_3;
 private JButton button_4;
 private JButton button_5;
 private JButton button_6;
 private JButton button_7;
 private JButton button_8;
 private JButton button_9;
 private JButton button_0;
 private JButton Button_equal;
 private JButton button_dot;
 private JButton button_d0;
 private JButton button_plus;
 private JButton button_sub;
 private JButton button_clr;
 private JButton button_dlt;
 private JButton button_mul;
 private JButton button_div;
 
 /**
  * 几个变量,用于计算、判定,存放结构等。
  * */
 private boolean numflag = false;     //用于标识是否输入数字
 private boolean opflag = false;   //用于标识是否输入运算操作符
 private String txt = null;    //显示结果的文本框的文本内容,用于存放计算结果的字符串形式
 private String num_txt = "";   //每次输入一个数,以字符的形式加到该字符创变量,后面运算直接将该字符创转成double类型进行运算
 private String op = " ";    //存放输入的运算操作符
 private double num1 = 0;    //num1和num2用作运算 
 private double num2 = 0;
 
 
 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     javaAP window = new javaAP();
     window.frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
 
 /**
  * 调用控件初始化函数,创建程序。
  */
 public javaAP() {
  initialize();
 }
 
 
 /**
  * 初始化计算器的各个控件。
  */
 private void initialize() {
  frame = new JFrame();                                   //-----------------------------------------------
  frame.setResizable(false);        //
  frame.setBounds(100, 100, 371, 371);     // 窗口各个属性的初始化
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
  frame.getContentPane().setLayout(null);     //-----------------------------------------------
  
  result = new JTextField();        //-----------------------------------------------
  result.setFont(new Font("Lao UI", Font.BOLD, 15));  //
  result.setHorizontalAlignment(SwingConstants.RIGHT);    //
  result.setBounds(29, 28, 308, 50);      //  显示结果的文本框的各个属性的初始化
  frame.getContentPane().add(result);      //
  result.setColumns(10);         //
  result.setText(txt);         //-----------------------------------------------
  
  
  /**
   * 0-9, 小数点,00 各个控件的初始化,给每个控件添加动作监听事件,每个控件被按下则调用numBtnAction函数。
   * 
   * */
  
  button_0 = new JButton("0");
  button_0.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction("0");
   }
  });
  button_0.setBounds(99, 273, 50, 50);
  frame.getContentPane().add(button_0);
  
  button_1 = new JButton("1");
  button_1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
     numBtnAction("1");
   }
  });
  button_1.setBounds(99, 213, 50, 50);
  frame.getContentPane().add(button_1);
  
  button_2 = new JButton("2");
  button_2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
     numBtnAction("2");
   }
  });
  button_2.setBounds(159, 213, 50, 50);
  frame.getContentPane().add(button_2);
  
  button_3 = new JButton("3");
  button_3.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction("3");
   }
  });
  button_3.setBounds(219, 213, 50, 50);
  frame.getContentPane().add(button_3);
  
  button_4 = new JButton("4");
  button_4.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction("4");
   }
  });
  button_4.setBounds(99, 157, 50, 50);
  frame.getContentPane().add(button_4);
  
  button_5 = new JButton("5");
  button_5.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction("5");
   }
  });
  button_5.setBounds(159, 157, 50, 50);
  frame.getContentPane().add(button_5);
  
  button_6 = new JButton("6");
  button_6.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction("6");
   }
  });
  button_6.setBounds(219, 157, 50, 50);
  frame.getContentPane().add(button_6);
  
  button_7 = new JButton("7");
  button_7.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction("7");
   }
  });
  button_7.setBounds(99, 97, 50, 50);
  frame.getContentPane().add(button_7);
  
  button_8 = new JButton("8");
  button_8.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction("8");
   }
  });
  button_8.setBounds(159, 97, 50, 50);
  frame.getContentPane().add(button_8);
  
  button_9 = new JButton("9");
  button_9.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction("9");
   }
  });
  button_9.setBounds(219, 97, 50, 50);
  frame.getContentPane().add(button_9);
    
  button_dot = new JButton(".");
  button_dot.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction(".");
   }
  });
  button_dot.setFont(new Font("宋体", Font.BOLD, 15));
  button_dot.setBounds(159, 273, 50, 50);
  frame.getContentPane().add(button_dot);
  
  button_d0 = new JButton("00");
  button_d0.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    numBtnAction("00");
   }
  });
  button_d0.setBounds(219, 273, 50, 50);
  frame.getContentPane().add(button_d0);
  
  /**
   * 运算符  =,+,-,*,/ 的初始化。
   * */
  
  Button_equal = new JButton("=");
  Button_equal.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    if(opflag == true){
     num2 = Double.parseDouble(num_txt);  //如果此时opflag为true的话,证明已经输入运算符,这时候刚输入的数为另外一个操作数,所以赋给num2。
     calc(op);       //num1和num2进行运算。
    }
    result.setText(new Double(num1).toString());   //显示结果。
   }
  });
  Button_equal.setBounds(279, 213, 50, 110);
  frame.getContentPane().add(Button_equal);
  
  button_plus = new JButton("+");
  button_plus.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    opBtnAction("+");
   }
  });
  button_plus.setBounds(279, 97, 50, 50);
  frame.getContentPane().add(button_plus);
  
  button_sub = new JButton("-");
  button_sub.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    opBtnAction("-");
   }
  });
  button_sub.setBounds(279, 157, 50, 50);
  frame.getContentPane().add(button_sub);
  
  button_mul = new JButton("*");
  button_mul.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    opBtnAction("*");
   }
  });
  button_mul.setBounds(39, 157, 50, 50);
  frame.getContentPane().add(button_mul);
  
  button_div = new JButton("/");
  button_div.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    opBtnAction("/");
   }
  });
  button_div.setBounds(39, 97, 50, 50);
  frame.getContentPane().add(button_div);
  
  /**
   * 功能键clr:清空   和  dlt回删   两个按键的初始化。
   * 
   * */
  
  button_clr = new JButton("clr");
  button_clr.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    result.setText(null);        //清楚文本框和num_txt,num1和num2全部置为0,
    txt = null;          //numflag和opflag均置为false,全部置为程序开始的默认值。
    numflag = opflag = false;
    num1 = num2 = 0;
    num_txt = "";
   }
  });
  button_clr.setBounds(39, 273, 50, 50);
  frame.getContentPane().add(button_clr);
  
  button_dlt = new JButton("dlt");
  button_dlt.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    if(num_txt != "" && num_txt.length() > 0) {
     num_txt = num_txt.substring(0, num_txt.length() - 1);  //通过截取字符串的方式回删一个字符
    }
    if(txt != null && txt.length() > 0) {
     txt = result.getText().substring(0,txt.length() - 1);  //结果文本框的回删
     result.setText(txt);
    }
   }
  });
  button_dlt.setBounds(39, 213, 50, 50);
  frame.getContentPane().add(button_dlt);
   
 }
 
 /**
  * 每个数字按键按下后触发的功能。
  * 
  * @param num 传入字符串形式的数字
  * 
  * */
 private void numBtnAction(String num) { 
  if(num_txt == "" && opflag == false) {    //判断是否num_txt是否为空,opflag是否为false,若满足这两个
   result.setText(null);      //条件,则为一次运算完毕,切新运算不以该次结果继续做运算。
   num_txt = "";
  } 
  numflag = true;         //只要输入一个数,则把numflag置为true,表示已有数字输入
  num_txt += num;         //把输入的数加入到最终要转换成都double运算的字符串
  txt = result.getText() + num;     //加到txt,使输入的数显示到文本框中。
  result.setText(txt);
  
 }
 
 /**
  * 点下运算符键触发的动作
  * 
  * @param operator 点击的运算符
  * 
  * */
 private void opBtnAction(String operator) {
  
 
  if(opflag == false && num_txt != "") {   //进行判断,如果opflag为false,则表示还没输入运算符,         
   num1 = Double.parseDouble(num_txt);   //这个时候把num_txt转换成double赋给num1。
  }
  if(opflag == true && num_txt != ""){
   num2 = Double.parseDouble(num_txt);   //如果opflag为true,表示第一个数输入完毕,且已输入相应的运算符,这个时候是在输入第二个要参与运算的数,则加到num2
   calc(op);         //先计算两数结果,赋给num1,num2置为0,以便后面多层混合运算使用。
  }
  numflag = false;        //numflag置为0,表示当前需要输入数进行运算
  op = operator;         //把新输入的运算符赋给op
  result.setText(operator + " ");     //在文本框显示
  opflag = true;         //opflag置为true,表示当前已输入一个或多个操作数。
  num_txt = "";         //num_txt置为空,为了存储下一个操作数。
 }
 
 /**
  * 进行  +、-、*、/ 的运算
  * @param op 运算符 
  * 
  * */
 private void calc(String op) {
  switch(op){
  case "+" : 
       num1 = num1 + num2;
       num2 = 0;
       opflag = false;
       num_txt = "";
       break;
       
  case "-" :
       num1 = num1 - num2;
       num2 = 0;
       opflag = false;
       num_txt = "";
       break;
       
  case "*" :
     if(num2 != 0) {
      num1 = num1 * num2;
      num2 = 0;
      opflag = false;
      num_txt = "";
     }
     break;
     
  case "/" : 
     if(num2 != 0) {
      num1 = num1 / num2;
      num2 = 0;
      opflag = false;
      num_txt = "";
     } 
     break;
     
  default:   opflag = false;
       num_txt = "";
       break;
  }
 }
}
Copy after login

The above is the detailed content of Sample code for implementing a simple calculator applet in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template