java 简单的计算器程序实例代码

 更新时间:2017年06月22日 15:31:10   投稿:lqh  
这篇文章主要介绍了java 简单的计算器程序实例代码的相关资料,需要的朋友可以参考下

java 简单的计算器程序

实现实例:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
 
 
public class Calculator 
{ 
  public static void main(String[] args) 
  { 
   EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
        CalculatorFrame frame = new CalculatorFrame(); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
      } 
     }); 
  } 
} 
 
 
/** 
 * A frame with a calculator panel. 
 */ 
class CalculatorFrame extends JFrame 
{ 
  public CalculatorFrame() 
  { 
    setTitle("Calculator"); 
    CalculatorPanel panel=new CalculatorPanel(); 
    add(panel); 
    pack(); 
  } 
} 
 
 
class CalculatorPanel extends JPanel 
{ 
  private JButton display; 
    private JPanel panel; 
    private double result; 
    private String lastCommand; 
    private boolean start; 
  public CalculatorPanel() 
  { 
    setLayout(new BorderLayout()); 
     
    result=0; 
    lastCommand="="; 
    start=true; 
     
     // add the display 
    display=new JButton("0"); 
    display.setEnabled(false); 
    add(display,BorderLayout.NORTH); 
     
    ActionListener insert=new InsertAction(); 
    ActionListener command=new CommandAction(); 
     
    panel=new JPanel(); 
    panel.setLayout(new GridLayout(4,4)); 
     
     addButton("7", insert); 
     addButton("8", insert); 
     addButton("9", insert); 
     addButton("/", command); 
 
 
     addButton("4", insert); 
     addButton("5", insert); 
     addButton("6", insert); 
     addButton("*", command); 
 
 
     addButton("1", insert); 
     addButton("2", insert); 
     addButton("3", insert); 
     addButton("-", command); 
 
 
     addButton("0", insert); 
     addButton(".", insert); 
     addButton("=", command); 
     addButton("+", command); 
 
 
     add(panel, BorderLayout.CENTER); 
     
    } 
  private void addButton(String label,ActionListener listener) 
  { 
    JButton button=new JButton(label); 
    button.addActionListener(listener); 
    panel.add(button); 
  } 
  /** 
    * This action inserts the button action string to the end of the display text. 
    */ 
  private class InsertAction implements ActionListener 
  { 
    public void actionPerformed(ActionEvent event) 
    { 
      String input=event.getActionCommand(); 
      if(start) 
      { 
        display.setText(""); 
        start=false; 
      } 
      display.setText(display.getText()+input); 
    } 
  } 
   /** 
    * This action executes the command that the button action string denotes. 
    */ 
  private class CommandAction implements ActionListener 
  { 
    public void actionPerformed(ActionEvent event) 
    { 
      String command=event.getActionCommand(); 
      if(start) 
      { 
        if (command.equals("-")) 
        { 
          display.setText(command); 
          start = false; 
        } 
        else lastCommand = command; 
      }else { 
        calculate(Double.parseDouble(display.getText())); 
        lastCommand=command; 
        start=true; 
      } 
    } 
  } 
  /** 
    * Carries out the pending calculation. 
    * @param x the value to be accumulated with the prior result. 
    */ 
  public void calculate(double x) 
  { 
     if (lastCommand.equals("+")) result += x; 
     else if (lastCommand.equals("-")) result -= x; 
     else if (lastCommand.equals("*")) result *= x; 
     else if (lastCommand.equals("/")) result /= x; 
     else if (lastCommand.equals("=")) result = x; 
     display.setText("" + result); 
  } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • java中对象和JSON格式的转换方法代码

    java中对象和JSON格式的转换方法代码

    JSON格式可以轻松地以面向对象的方式转换为Java对象,下面这篇文章主要给大家介绍了关于java中对象和JSON格式的转换方法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • 利用ThreadLocal实现一个上下文管理组件

    利用ThreadLocal实现一个上下文管理组件

    本文基于ThreadLocal原理,实现了一个上下文状态管理组件Scope,通过开启一个自定义的Scope,在Scope范围内,可以通过Scope各个方法读写数据,感兴趣的可以了解一下
    2022-10-10
  • Java中将Html转换为PDF的方法和步骤

    Java中将Html转换为PDF的方法和步骤

    这篇文章主要介绍了Java中如何将Html转换为PDF的方法,文中有相关的代码示例和步骤讲解,感兴趣的同学可以参考阅读
    2023-06-06
  • Java之理解Redis回收算法LRU案例讲解

    Java之理解Redis回收算法LRU案例讲解

    这篇文章主要介绍了Java之理解Redis回收算法LRU案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Struts2 $,#,%详解及实例代码

    Struts2 $,#,%详解及实例代码

    这篇文章主要介绍了Struts2 $,#,%详解及实例代码的相关资料,需要的朋友可以参考下
    2016-12-12
  • 解决IDEA中不能正常输入光标变粗的问题

    解决IDEA中不能正常输入光标变粗的问题

    这篇文章主要介绍了在IDEA中不能正常输入光标变粗的解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2020-09-09
  • 在IntelliJ IDEA中配置SSH服务器开发环境并实现固定地址远程连接的操作方法

    在IntelliJ IDEA中配置SSH服务器开发环境并实现固定地址远程连接的操作方法

    本文主要介绍如何在IDEA中设置远程连接服务器开发环境,并结合Cpolar内网穿透工具实现无公网远程连接,然后实现远程Linux环境进行开发,本例使用的是IDEA2023.2.5版本,感兴趣的朋友跟随小编一起看看吧
    2024-01-01
  • Java 讲解两种找二叉树的最近公共祖先的方法

    Java 讲解两种找二叉树的最近公共祖先的方法

    树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合,这篇文章主要给大家介绍了关于Java求解二叉树的最近公共祖先的相关资料,需要的朋友可以参考下
    2022-04-04
  • springBoot集成flowable的流程解析

    springBoot集成flowable的流程解析

    这篇文章主要介绍了springBoot集成flowable的流程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-02-02
  • Springboot中使用lombok的@Data注解方式

    Springboot中使用lombok的@Data注解方式

    这篇文章主要介绍了Springboot中使用lombok的@Data注解方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08

最新评论