Java实用程序--计算器的两种设计方法

//2015年11月25日18:21:59
//计算器设计代码1:
import java.awt.*;
import java.awt.event.*;


public class zhang8
{
   public static void main(String[] args)
   {
     new A().f();
   
   }
}
class A
{
    String s1 = "";
    String s2 = "";
    TextField t1;
    TextField t2;
    TextField t3;
    
    public class B implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
       double num1 = Double.valueOf(t1.getText());
       double num2 = Double.valueOf(t2.getText());
       double num3 = 0;
       
       if (e.getActionCommand().equals("+"))
{
num3 = num1 + num2;
}
if (e.getActionCommand().equals("-"))
{
num3 = num1 - num2;
}
if (e.getActionCommand().equals("*"))
{
num3 = num1 * num2;
}
if (e.getActionCommand().equals("/"))
{
num3 = num1 / num2;
}
String s3 = String.valueOf(num3);
t3.setText(s3);
if(e.getActionCommand().equals("清零"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
       
    }
   
    }
    public void f()//本函数先设计好界面
    {
        Frame f = new Frame("张明阳设计的计算器");
        f.setVisible(true);
        f.setLayout(new GridLayout(3,1));
    Panel p1 = new Panel();
    p1.setLayout(new FlowLayout(FlowLayout.CENTER,100,2));
    Panel p2 = new Panel();
    p2.setLayout(new FlowLayout(FlowLayout.CENTER,70,2));
    Panel p3 = new Panel();
    p3.setLayout(new GridLayout(2,3,50,50));
    f.add(p1);
    f.add(p2);
    f.add(p3);
    for(int i=0;i<3;++i)
    {
    Label La = new Label("运算数");
    p1.add(La);
   
    }
   
   
        t1 = new TextField(20);
        p2.add(t1);
        t2 = new TextField(20);
        p2.add(t2);
        t3 = new TextField(20);
        p2.add(t3);
   
    Button b1 = new Button("+");
    p3.add(b1);
    Button b2 = new Button("-");
    p3.add(b2);
    Button b3 = new Button("*");
    p3.add(b3);
    Button b4 = new Button("/");
    p3.add(b4);
    Button b5 = new Button("清零");
    b5.setBackground(Color.BLUE);
    p3.add(b5);
    Button b6 = new Button("清零");
    b6.setBackground(Color.BLUE);
    p3.add(b6);
    Font aa = new Font("楷体",Font.BOLD,36);//通过Font类设置文本框字体的格式
        b1.setFont(aa);
        b2.setFont(aa);
        b3.setFont(aa);
        b4.setFont(aa);
        b5.setFont(aa);
        b6.setFont(aa);
        
        b1.addActionListener(new B());//为每一个按钮都安装了事件监听器
        b2.addActionListener(new B());
        b3.addActionListener(new B());
        b4.addActionListener(new B());
        b5.addActionListener(new B());
        b6.addActionListener(new B());
        
    f.pack();
    }

}
//计算器设计代码2
//计算器设计
import java.awt.*;
import java.awt.event.*;


public class zhang1
{
   public static void main(String[] args)
   {
     new A().f();
   }

}


class A  //本类当中为每一个按钮设置了事件监听器已经更新了作用指令信息
{
   public boolean firstFlag = true;
   public String s1 = "";
   public String s2 = "";
   public TextField t ;
   public String strOper;
   
   public void f()//本函数用于设计界面
   {
     Frame f = new Frame("数据分析玩家设计的计算器");
      f.setBackground(Color.BLUE);
      f.setVisible(true);
      f.setLayout(new GridLayout(2,1));
      f.addWindowListener(new B());//为关闭按钮设计了一个事件模型
      t = new TextField(40);
      //t = new TextField("使用之前点击请清零");
      Font aa = new Font("楷体",Font.BOLD,36);//通过Font类设置文本框字体的格式
      t.setFont(aa);
      t.setBackground(Color.white);
      Panel p = new Panel();
      p.setSize(40,80);
      f.add(t);
      f.add(p);
      p.setLayout(new GridLayout(4,4,20,20));
      for(int i=0;i<=9;++i)
      {
      Button b = new Button(""+i);
      b.setActionCommand("数字");
      b.addActionListener(new B());//为按钮添加了事件监听器
      p.add(b);      
      }
      Button jia = new Button("+");
      jia.setActionCommand("算数操作");
      jia.addActionListener(new B());//为按钮添加了事件监听器
      
      Button jian = new Button("-");
      jian.setActionCommand("算数操作");
      jian.addActionListener(new B());//为按钮添加了事件监听器
      
      Button cheng = new Button("*");
      cheng.setActionCommand("算数操作");
      cheng.addActionListener(new B());//为按钮添加了事件监听器
      
      Button chu = new Button("/");
      chu.setActionCommand("算数操作");
      chu.addActionListener(new B());//为按钮添加了事件监听器
      
      Button deng  = new Button("=");
      deng.addActionListener(new B());//为按钮添加了事件监听器
      
      Button qling = new Button("清零");
      qling.addActionListener(new B());//为按钮添加了事件监听器
      
      p.add(jia);
      p.add(jian);
      p.add(cheng);
      p.add(chu);
      p.add(deng);
      p.add(qling);
      
      f.pack();  
   }
   public class B extends WindowAdapter implements ActionListener
   {
     
     @Override
     public void windowClosing(WindowEvent e)
     {
      System.exit(-1);    
     }
     
     public void actionPerformed(ActionEvent e)//接下来根据接收到的作用指令信息进行操作
     {
        String zyIf = e.getActionCommand();//获取发生事件的事件源的作用指令信息
        Button bn = (Button)e.getSource();//获取发生事件的事件源按钮
        String zfIf = bn.getLabel();
     
        if(zyIf.equals("数字"))//如果事件源是数字键
        {
           if(firstFlag)
           {
            s1 = s1 + zfIf;
               t.setText(s1);
              
           }
           else
           {
             s2 = s2 + zfIf;
             t.setText(s2); 
                       
           }
        }
        else if(zyIf.equals("算数操作"))
        {
        strOper = zfIf;
        firstFlag = false;
        }
        else if(zyIf.equals("="))
        {
        result();//调用运算函数
        }
        else if(zyIf.equals("清零"))
        {
        s1 = s2 = "";
        t.setText("");
        firstFlag = true;
       
        }
     } 
     public void result()
     {
      double num1 = Double.valueOf(s1);
      double num2 = Double.valueOf(s2);
      double num3 = 0;
     
     
if (strOper.equals("+"))
{
num3 = num1 + num2;
}
else if (strOper.equals("-"))
{
num3 = num1 - num2;
}
else if (strOper.equals("*"))
{
num3 = num1 * num2;
}
else if (strOper.equals("/"))
{
num3 = num1 / num2;
}
t.setText(""+num3);
s1 = String.valueOf(num3);
s2= "";
     
     }  
   }   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只懒得睁眼的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值