如何使用Java对象语言编写一个加减乘除计算器要有代码

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/28 14:53:05
如何使用Java对象语言编写一个加减乘除计算器要有代码

如何使用Java对象语言编写一个加减乘除计算器要有代码
如何使用Java对象语言编写一个加减乘除计算器要有代码

如何使用Java对象语言编写一个加减乘除计算器要有代码

下面文件名要为:JiSuanQi.java

import java.awt.*;

import java.awt.event.*;

public class JiSuanQi

{

   String s="",s1=null,s2=null;

   Frame f=new Frame("计算器");

  TextField tf=new TextField(30);

  Panel p1=new Panel();

  Panel p2=new Panel();

  Panel p3=new Panel();

  Button bt1=new Button("=");

  Button bt2=new Button("删除");

  Button[] bt=new Button[16];

  int id=0;

   public static void main(String[] args)

   {

       new JiSuanQi().init();

   }

    public void init()

    {

       f.setBackground(new Color(85,247,253));

       f.setLayout(new BorderLayout(4,4));

       p2.setLayout(new GridLayout(4,4,4,4));

       p3.setLayout(new BorderLayout(4,4));

       f.setResizable(false);

       f.add(p1,BorderLayout.NORTH);

       f.add(p2);

       p3.add(bt2,BorderLayout.NORTH);

       p3.add(bt1);

       p1.add(tf);

       f.add(p3,BorderLayout.EAST);

       String[] b={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","复位","/"};

       for(int i=0;i<16;i++)

       {

           bt[i]=new Button(b[i]);

           p2.add(bt[i]);

       }

       bt[0].setForeground(Color.blue);

       bt[1].setForeground(Color.blue);

       bt[2].setForeground(Color.blue);

       bt[4].setForeground(Color.blue);

       bt[5].setForeground(Color.blue);

       bt[6].setForeground(Color.blue);

       bt[8].setForeground(Color.blue);

       bt[9].setForeground(Color.blue);

       bt[10].setForeground(Color.blue);

       bt[12].setForeground(Color.blue);

       bt[13].setForeground(Color.blue);

       bt[3].setForeground(Color.red);

       bt[7].setForeground(Color.red);

       bt[11].setForeground(Color.red);

       bt[15].setForeground(Color.red);

       bt[14].setForeground(Color.red);

       bt1.setForeground(Color.red);

       bt2.setForeground(Color.red);

       f.pack();

       f.setVisible(true);

       f.addWindowListener(new WindowAdapter()

       {

          public void windowClosing(WindowEvent e)

          {

             System.exit(0); 

          } 

       }

       );

       bt[0].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=1;

                 s2+=1;

                 tf.setText(s);

           }

       }

       );

          bt[1].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=2;

                 s2+=2;

                 tf.setText(s);

           }

       }

       );

       bt[2].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=3;

                 s2+=3;

                 tf.setText(s);

           }

       }

       );

       bt[4].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=4;

                 s2+=4;

                 tf.setText(s);

           }

       }

       );

       bt[5].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=5;

                 s2+=5;

                 tf.setText(s);

           }

       }

       );

       bt[6].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=6;

                 s2+=6;

                 tf.setText(s);

           }

       }

       );

       bt[8].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=7;

                 s2+=7;

                 tf.setText(s);

           }

       }

       );

       bt[9].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=8;

                 s2+=8;

                 tf.setText(s);

           }

       }

       );

       bt[10].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=9;

                 s2+=9;

                 tf.setText(s);

           }

       }

       );

       bt[12].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=0;

                 s2+=0;

                 tf.setText(s);

           }

       }

       );

       bt[13].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+='.';

                 s2+='.';

                 tf.setText(s);

           }

       }

       );

       bt[3].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+='+';

                 id=1;

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt[7].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+='-';

                 id=2;

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt[11].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+='*';

                 id=3;

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt[14].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s="";

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt[15].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+='/';

                 id=4;

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt1.addActionListener

如何使用Java对象语言编写一个加减乘除计算器要有代码 老师要求张浩使用面向对象的思想编写一个计算器类(Calculator),可以实现两个整数的加减乘除的运算.java java中如何将多个list集合对象拼接成一个集合对象 英语翻译本文阐述了使用JAVA编程语言对基于客户/服务器模式的应用编写网络通信程序,讨论了SOCKET机制、输入输出流以及程序实现代码.java它一个工具,一个用纯java语言写的跨平台的一个通用 用java语言编写:计算并输出一个整数各位数字之和 用java语言编写一个程序对任意三个整数进行排序并输出结果 1.运用java 编写一个复数类,有实部和虚部,并实现复数的加减乘除运算? 编写JAVA程序,将一个圆(Circle)对象的引用”传值”给圆柱体(Cylinder)对象的bottom,求圆柱体的体积. 有谁可以帮我做一个排序算法的题目么?使用java编写 帮忙看一下,这句英文怎么写?我用java语言编写了一个程序,名字叫myApp,我英文标注一下,证明软件是java编写的,应该是myApp for java 还是myApp by java 还是其他写法? 用C语言编写一个简易计算器可实现加减乘除,连加连减,连乖连除. JAVA语言计算表达式的结果用java语言编写一个程序,输入表达式,结算表达式的结果,其中表达式中有括号 用java语言编写:窗口中显示两个按钮,按下“Yellow”按钮则在窗口背景显示黄色,按下“Green” 按钮则可显示绿色 怎样取的事件源对象啊,我初学java. 如何编写一个打印50~100随机数的简单的程序用java写 用JAVA语言编写程序19.编写一个译码程序,把一个英语句子译成数字代码.译码规则是以数字 1代替字母 a,数 请使用java语言编写一段程序,统计这段文字中单词的个数,并输出其中由四个字母组成的单词 lisp语言如何编写圆弧, 单片机用C语言编写计算器如何实现大于65535的数字加减乘除?我使用的单片机的C语言仅仅支持2字节的数据变量,也就是最大值仅为65535,要如何实现大于这个数的四则运算呢?我用的是holtek单片