欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 簡單的計算器程序?qū)嵗a

 更新時間:2017年06月22日 15:31:10   投稿:lqh  
這篇文章主要介紹了java 簡單的計算器程序?qū)嵗a的相關(guān)資料,需要的朋友可以參考下

java 簡單的計算器程序

實現(xiàn)實例:

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); 
  } 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • SpringMVC核心DispatcherServlet處理流程分步詳解

    SpringMVC核心DispatcherServlet處理流程分步詳解

    這篇文章主要介紹了SpringMVC核心之中央調(diào)度器DispatcherServlet的相關(guān)知識,包括SpringMVC請求處理過程及SrpingMVC容器和spring IOC容器關(guān)系,需要的朋友可以參考下
    2023-04-04
  • Java 關(guān)鍵字 速查表介紹

    Java 關(guān)鍵字 速查表介紹

    下面小編就為大家?guī)硪黄狫ava 關(guān)鍵字 速查表介紹。小編覺得聽不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • List集合按某個屬性或者字段進(jìn)行分組的操作

    List集合按某個屬性或者字段進(jìn)行分組的操作

    這篇文章主要介紹了List集合按某個屬性或者字段進(jìn)行分組的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java操作FTP實現(xiàn)上傳下載功能

    Java操作FTP實現(xiàn)上傳下載功能

    這篇文章主要為大家詳細(xì)介紹了Java如何通過操作FTP實現(xiàn)上傳下載的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
    2022-11-11
  • 基于jQuery獲取table數(shù)據(jù)發(fā)送到后端

    基于jQuery獲取table數(shù)據(jù)發(fā)送到后端

    這篇文章主要介紹了基于jQuery獲取table數(shù)據(jù)發(fā)送到后端,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot獲取application.properties文件中文亂碼問題及解決

    SpringBoot獲取application.properties文件中文亂碼問題及解決

    這篇文章主要介紹了SpringBoot獲取application.properties文件中文亂碼問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • java學(xué)習(xí)之理解自動拆裝箱特性

    java學(xué)習(xí)之理解自動拆裝箱特性

    這篇文章主要介紹java自動拆裝箱特性以及java自動拆裝箱的應(yīng)用,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家早日升職加薪
    2021-09-09
  • SpringBoot實現(xiàn)前后端、json數(shù)據(jù)交互以及Controller接收參數(shù)的幾種常用方式

    SpringBoot實現(xiàn)前后端、json數(shù)據(jù)交互以及Controller接收參數(shù)的幾種常用方式

    這篇文章主要給大家介紹了關(guān)于SpringBoot實現(xiàn)前后端、json數(shù)據(jù)交互以及Controller接收參數(shù)的幾種常用方式,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-03-03
  • 簡述Java編程語言中的逃逸分析

    簡述Java編程語言中的逃逸分析

    這篇文章主要介紹了簡述Java編程語言中的逃逸分析,包括其定義、作用、類型及理論基礎(chǔ)等相關(guān)內(nèi)容,十分具有參考價值,需要的朋友可以了解下。
    2017-09-09
  • Java實現(xiàn)幾種常見排序算法代碼

    Java實現(xiàn)幾種常見排序算法代碼

    排序(Sorting) 是計算機(jī)程序設(shè)計中的一種重要操作,它的功能是將一個數(shù)據(jù)元素(或記錄)的任意序列,重新排列成一個關(guān)鍵字有序的序列
    2013-09-09

最新評論