最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java awt實現(xiàn)計算器功能

 更新時間:2020年12月14日 14:10:17   作者:Mr順  
這篇文章主要為大家詳細介紹了java awt實現(xiàn)計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java awt實現(xiàn)計算器的具體代碼,供大家參考,具體內(nèi)容如下

上課老師演示了一遍他的寫法,由于沒給代碼,因此按著他的思路擼了一遍,感覺還很簡單。

代碼以及解釋如下:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Caculate1 {
 static String s1="";
 static String s2="";
 static int f=9;
 static int num1=0,num2=0;
 public static void main(String[] args) {
 int x=0,y=0,z=0;
 JFrame jf=new JFrame( "我的計算器");
 jf.setBounds(0,0,300,400);
// jf.setBackground(bgColor);
 jf.setVisible(true);
 jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 
 JTextField show=new JTextField("0");
 //此處為布局:也就是定義五個容器,(也可以理解為將定義的JFrame空間分為5個容器)
 JPanel jp1=new JPanel(new GridLayout(1,3));//存儲:
 JPanel jp2=new JPanel(new GridLayout(1,3));
 JPanel jp3=new JPanel(new GridLayout(1,3));
 JPanel jp4=new JPanel(new GridLayout(1,3));
 JPanel jp5=new JPanel(new GridLayout(1,3));
 
 JButton bt_add=new JButton("+");
 JButton bt_sub=new JButton("-");
 JButton bt_mul=new JButton("*");
 JButton bt_div=new JButton("/");
 JButton bt_7=new JButton("7");
 JButton bt_8=new JButton("8");
 JButton bt_9=new JButton("9");
 JButton bt_4=new JButton("4");
 JButton bt_5=new JButton("5");
 JButton bt_6=new JButton("6");
 JButton bt_1=new JButton("1");
 JButton bt_2=new JButton("2");
 JButton bt_3=new JButton("3");
 JButton bt_0=new JButton("0");
 JButton bt_c=new JButton("C");
 JButton bt_equal=new JButton("=");
 
 jf.setLayout(new GridLayout(6,1));
 //容器1 :添加 '+','-','*','/'按鈕
 jp1.add(bt_add);
 jp1.add(bt_sub);
 jp1.add(bt_mul);
 jp1.add(bt_div);
 //容器2:添加'7','8','9'按鈕
 jp2.add(bt_7);
 jp2.add(bt_8);
 jp2.add(bt_9);
 //容器3:添加'4','5','6'按鈕
 jp3.add(bt_4);
 jp3.add(bt_5);
 jp3.add(bt_6);
 //容器4:添加'1','2','3'按鈕
 jp4.add(bt_1);
 jp4.add(bt_2);
 jp4.add(bt_3);
 //容器5:添加'0','C','='
 jp5.add(bt_0);
 jp5.add(bt_c);
 jp5.add(bt_equal);
 
 jf.add(show);
 jf.add(jp1);
 jf.add(jp2);
 jf.add(jp3);
 jf.add(jp4);
 jf.add(jp5);
//元素已經(jīng)定義好了,思路也很簡單:(1)JFrame定義一個大容器jf,jf= 1個顯示框(show)+5個容器,分別定義顯示框和容器,
 //(2)類似樹形添加元素的數(shù)據(jù)結(jié)構(gòu),為jf添加元素
 
 //下面便是時間監(jiān)聽了(又稱加載驅(qū)動)
 bt_9.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+9;
  show.setText(s1);
  }
  
 });
 
 bt_8.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+8;
  show.setText(s1);
  }
  
 });
 
 bt_7.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+7;
  show.setText(s1);
  }
  
 });
 
 bt_6.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+6;
  show.setText(s1);
  }
  
 });
 
 bt_5.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+5;
  show.setText(s1);
  }
  
 });
 
 bt_4.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+4;
  show.setText(s1);
  }
  
 });
 
 bt_3.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+3;
  show.setText(s1);
  }
  
 });
 
 bt_0.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+0;
  show.setText(s1);
  }
  
 });
 
 bt_2.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+2;
  show.setText(s1);
  }
  
 });
 
 bt_1.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+1;
  show.setText(s1);
  }
  
 });
 
 bt_0.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  
  s1=s1+0;
  show.setText(s1);
  }
  
 });
 
 
 bt_equal.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  num1=Integer.valueOf(s2);
  num2=Integer.valueOf(s1);
  int z=0;
  char fg=' ';
  
  switch(f){
  case 0:
   z=num1+num2;
   fg='+';
   break;
  case 1:
   z=num1-num2;
   fg='-';
   break;
  case 2:
   z=num1*num2;
   fg='*';
   break;
  case 3:
   fg='/';
   if(num2==0)
   num2=num2+1;
   z=num1/num2;
   break;
  default:
   break;
  }
  show.setText(s2+" "+fg+" "+s1+" = "+z);
  }
  
 });
 bt_add.addActionListener(new ActionListener(){

  @Override
  public void actionPerformed(ActionEvent e) {
  s2=s1;
  s1="";
  f=0;
  show.setText(s2+"+");
  }
  
 });
 bt_sub.addActionListener(new ActionListener() {

  @Override
  public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  s2 = s1;
  s1 = "";
  f = 1;
  show.setText(s2 + " - ");
  }
 });
 bt_mul.addActionListener(new ActionListener() {

  @Override
  public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  s2 = s1;
  s1 = "";
  f = 2;
  show.setText(s2 + " * ");
  }
 });
 bt_div.addActionListener(new ActionListener() {

  @Override
  public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  s2 = s1;
  s1 = "";
  f = 3;
  show.setText(s2 + " / ");
  }
 });
 bt_c.addActionListener(new ActionListener() {

  @Override
  public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  s2 = "";
  s1 = "";
  num1 = 0;
  num2 = 0;
  f = 9;
  show.setText("0");
  }
 });
 }

}

關(guān)于計算器的精彩文章請查看《計算器專題》 ,更多精彩等你來發(fā)現(xiàn)!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決springboot接入springfox-swagger2遇到的一些問題

    解決springboot接入springfox-swagger2遇到的一些問題

    這篇文章主要介紹了解決springboot接入springfox-swagger2遇到的一些問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • MyBatis-Plus多數(shù)據(jù)源的示例代碼

    MyBatis-Plus多數(shù)據(jù)源的示例代碼

    本文主要介紹了MyBatis-Plus多數(shù)據(jù)源的示例代碼,包括依賴配置、數(shù)據(jù)源配置、Mapper 和 Service 的定義,具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • Java由淺入深細數(shù)數(shù)組的操作下

    Java由淺入深細數(shù)數(shù)組的操作下

    數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲固定大小的同類型元素
    2022-04-04
  • 實例講解Java HashSet

    實例講解Java HashSet

    這篇文章主要介紹了Java HashSet的相關(guān)資料,文中示例代碼非常詳細,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Java實現(xiàn)九九乘法表的完整實例(對齊版)

    Java實現(xiàn)九九乘法表的完整實例(對齊版)

    這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)九九乘法表(對齊版)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 多模塊項目引入SpringSecurity后一直報404的解決方案

    多模塊項目引入SpringSecurity后一直報404的解決方案

    這篇文章主要介紹了多模塊項目引入SpringSecurity后一直報404的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java連接zookeeper實現(xiàn)zookeeper教程

    java連接zookeeper實現(xiàn)zookeeper教程

    這篇文章主要介紹了java連接zookeeper實現(xiàn)zookeeper教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Spring學(xué)習(xí)教程之AOP模塊的概述

    Spring學(xué)習(xí)教程之AOP模塊的概述

    AOP 從功能的角度來講,可能看作OOP編程方式的一種補充,提供了一種不同的代碼或者系統(tǒng)組織方式,下面這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)教程之AOP模塊的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2018-05-05
  • java中的Io(input與output)操作總結(jié)(一)

    java中的Io(input與output)操作總結(jié)(一)

    所謂IO,也就是Input與Output的縮寫。在java中,IO涉及的范圍比較大,這里主要討論針對文件內(nèi)容的讀寫,感興趣的朋友可以了解下
    2013-01-01
  • Java實現(xiàn)Excel數(shù)據(jù)驗證功能

    Java實現(xiàn)Excel數(shù)據(jù)驗證功能

    在Java中,開發(fā)者可以使用一些開源的庫(如Apache POI)來添加、修改和處理Excel中的數(shù)據(jù),下面我們就來看看如何使用Java實現(xiàn)添加,修改和刪除Excel數(shù)據(jù)驗證吧
    2023-10-10

最新評論

左云县| 南丹县| 鸡泽县| 上饶县| 澄江县| 读书| 威信县| 乌鲁木齐市| 鄂托克旗| 合水县| 揭西县| 越西县| 沅江市| 广平县| 聊城市| 武平县| 安多县| 芦山县| 荔浦县| 诸城市| 内丘县| 荔波县| 永宁县| 宜兰县| 景洪市| 曲阳县| 德钦县| 肇州县| 弋阳县| 乡城县| 大冶市| 隆化县| 长岛县| 正阳县| 昆明市| 绥滨县| 长沙县| 凯里市| 且末县| 教育| 永靖县|