博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java计算器 图形用户界面 精简版
阅读量:5278 次
发布时间:2019-06-14

本文共 5881 字,大约阅读时间需要 19 分钟。

package com.rgy.entity;import java.awt.*;import java.awt.event.*;@SuppressWarnings("serial")public class Computer extends Frame implements ActionListener{	private Panel panel_keys;	private TextField text_show;	private String str="";	private double num_record=0;	private int count_cut=0;	private int count_multiplication=0;	private int count_division=0;	private int op=0;	private Button button_num0;private Button button_num1;	private Button button_num2;private Button button_num3;	private Button button_num4;private Button button_num5;	private Button button_num6;private Button button_num7;	private Button button_num8;private Button button_num9;	private Button button_division;	private Button button_multiplication;	private Button button_cut;	private Button button_add;	private Button button_equal;	private Button button_point;		public Computer(){		super("计算器");		this.setVisible(true);		this.setBounds(500,250,300,250);				panel_keys=new Panel();		text_show=new TextField();		text_show.setEditable(false);				button_num0=new Button("0");button_num1=new Button("1");		button_num2=new Button("2");button_num3=new Button("3");		button_num4=new Button("4");button_num5=new Button("5");		button_num6=new Button("6");button_num7=new Button("7");		button_num8=new Button("8");button_num9=new Button("9");		button_division=new Button("/");		button_multiplication=new Button("*");		button_cut=new Button("-");		button_add=new Button("+");		button_equal=new Button("=");		button_point=new Button(".");				this.add(text_show,BorderLayout.NORTH);		this.add(panel_keys,BorderLayout.CENTER);				panel_keys.add(button_num7);panel_keys.add(button_num8);panel_keys.add(button_num9);panel_keys.add(button_division);		panel_keys.add(button_num4);panel_keys.add(button_num5);panel_keys.add(button_num6);panel_keys.add(button_multiplication);		panel_keys.add(button_num1);panel_keys.add(button_num2);panel_keys.add(button_num3);panel_keys.add(button_cut);		panel_keys.add(button_num0);panel_keys.add(button_point);panel_keys.add(button_equal);panel_keys.add(button_add);				panel_keys.setLayout(new GridLayout(4,4));				this.addWindowListener(new WinClose());		button_num0.addActionListener(this);		button_num1.addActionListener(this);		button_num2.addActionListener(this);		button_num3.addActionListener(this);		button_num4.addActionListener(this);		button_num5.addActionListener(this);		button_num6.addActionListener(this);		button_num7.addActionListener(this);		button_num8.addActionListener(this);		button_num9.addActionListener(this);		button_division.addActionListener(this);		button_multiplication.addActionListener(this);		button_add.addActionListener(this);		button_cut.addActionListener(this);		button_equal.addActionListener(this);		button_point.addActionListener(this);	}		//键盘事件监听	public void actionPerformed(ActionEvent ev) {		if(ev.getSource()==button_num0){			str=text_show.getText()+"0";			text_show.setText(str);		}		if(ev.getSource()==button_num1){			str=text_show.getText()+"1";			text_show.setText(str);		}		if(ev.getSource()==button_num2){			str=text_show.getText()+"2";			text_show.setText(str);		}		if(ev.getSource()==button_num3){			str=text_show.getText()+"3";			text_show.setText(str);		}		if(ev.getSource()==button_num4){			str=text_show.getText()+"4";			text_show.setText(str);		}		if(ev.getSource()==button_num5){			str=text_show.getText()+"5";			text_show.setText(str);		}		if(ev.getSource()==button_num6){			str=text_show.getText()+"6";			text_show.setText(str);		}		if(ev.getSource()==button_num7){			str=text_show.getText()+"7";			text_show.setText(str);		}		if(ev.getSource()==button_num8){			str=text_show.getText()+"8";			text_show.setText(str);		}		if(ev.getSource()==button_num9){			str=text_show.getText()+"9";			text_show.setText(str);		}		if(ev.getSource()==button_point){			str=text_show.getText()+".";			text_show.setText(str);		}		if(ev.getSource()==button_add){			if(op==5){}			else{				num_record=num_record+Double.parseDouble(str);			}			str="";			text_show.setText(str);			op=1;		}		if(ev.getSource()==button_cut){			if(op==5){}			else{				if(count_cut==0){					num_record=Double.parseDouble(str);					count_cut++;				}				else{					num_record=num_record-Double.parseDouble(str);				}			}			str="";			text_show.setText(str);			op=2;		}		if(ev.getSource()==button_multiplication){			if(op==5){}			else{				if(count_multiplication==0){					num_record=Double.parseDouble(str);					count_multiplication++;				}				else{					num_record=num_record*Double.parseDouble(str);				}			}			str="";			text_show.setText(str);			op=3;		}		if(ev.getSource()==button_division){			if(op==5){}			else{				if(count_division==0){					num_record=Double.parseDouble(str);					count_division++;				}				else{					num_record=num_record/Double.parseDouble(str);				}			}			str="";			text_show.setText(str);			op=4;		}		if(ev.getSource()==button_equal){			if(op==1){				num_record=num_record+Double.parseDouble(str);			}			if(op==2){				num_record=num_record-Double.parseDouble(str);			}			if(op==3){				num_record=num_record*Double.parseDouble(str);			}			if(op==4){				num_record=num_record/Double.parseDouble(str);			}						if(num_record-(int)num_record==0){				text_show.setText(""+(int)num_record);			}			else{				text_show.setText(""+num_record);			}			op=5;		}	}		//窗体事件监听	public class WinClose implements WindowListener {		public void windowOpened(WindowEvent e) {}		public void windowClosing(WindowEvent e) {			System.exit(0);		}		public void windowClosed(WindowEvent e) {}		public void windowIconified(WindowEvent e) {}		public void windowDeiconified(WindowEvent e) {}		public void windowActivated(WindowEvent e) {}		public void windowDeactivated(WindowEvent e) {}	}}package com.rgy.Test;import com.rgy.entity.*;public class Test {	public static void main(String args[]){		new Computer();	}}

版权声明:本文博主原创文章。博客,未经同意不得转载。

转载于:https://www.cnblogs.com/zfyouxi/p/4876865.html

你可能感兴趣的文章
HttpClient的巨坑
查看>>
关于静态文本框透明度的问题
查看>>
海量数据、高并发的优化方案
查看>>
javascript的发展及个人笔记
查看>>
全选,反全选,反选,获取选中的值,根据子选择控制全选按钮
查看>>
梦断代码读后感01
查看>>
[CF#250 Div.2 D]The Child and Zoo(并查集)
查看>>
博客园博客插入公式
查看>>
spring ioc原理(看完后大家可以自己写一个spring)
查看>>
hdu 1028 Ignatius and the Princess III(母函数入门+模板)
查看>>
Ubuntu下配置安装telnet server
查看>>
Codeforces 235 E Number Challenge
查看>>
ubuntu 常见命令整理
查看>>
关于vue的npm run dev和npm run build
查看>>
Hive架构
查看>>
EJBCA安装教程+postgresql+wildfly10
查看>>
(五十四)涂鸦的实现和截图的保存
查看>>
关于微信暴力加很申请
查看>>
06享元、责任链
查看>>
ubuntu如何部署tftp服务
查看>>