欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java awt实现计算器功能

程序员文章站 2022-07-03 19:15:51
本文实例为大家分享了java awt实现计算器的具体代码,供大家参考,具体内容如下上课老师演示了一遍他的写法,由于没给代码,因此按着他的思路撸了一遍,感觉还很简单。代码以及解释如下:import ja...

本文实例为大家分享了java awt实现计算器的具体代码,供大家参考,具体内容如下

java awt实现计算器功能

上课老师演示了一遍他的写法,由于没给代码,因此按着他的思路撸了一遍,感觉还很简单。

代码以及解释如下:

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);
//元素已经定义好了,思路也很简单:(1)jframe定义一个大容器jf,jf= 1个显示框(show)+5个容器,分别定义显示框和容器,
 //(2)类似树形添加元素的数据结构,为jf添加元素
 
 //下面便是时间监听了(又称加载驱动)
 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");
  }
 });
 }

}

关于计算器的精彩文章请查看《计算器专题》 ,更多精彩等你来发现!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。