java计算器 图形用户界面 精简版
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();
}
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
java计算器 图形用户界面 精简版的更多相关文章
- java计算器 图形用户界面 升级版 v1.02
package com.rgy.entity; import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayo ...
- java GUI(图形用户界面)
GUI Graphical User Interface(图形用户接口). 用图形的方式,来显示计算机操作的界面,这样更方便更直观. CLI Command line User Interface ( ...
- 借助WindowBuilder插件轻松完成JAVA图形用户界面编辑
如果以纯代码的形式进行JAVA的图形用户界面编辑,将是一件非常痛苦的事,博主在学习过程中发现了JAVA GUI编辑神器——WindowBuilder,提供可视化的编辑界面,控件的添加.排版只需使用鼠标 ...
- java第八节 GUI/图形用户界面
/* *第8讲 GUI/图形用户界面 * AWT的基础知识 * GUI全称是Graphical User Interface,即图形用户界面 * JDK中提供了AWT和Swing两个包,用于GUI程序 ...
- 黑马程序员——【Java基础】——GUI(图形用户界面)
---------- android培训.java培训.期待与您交流! ---------- 一.概述 1.GUI(GraphicalUser Interface):又称图形用户界面,是计算机用户与计 ...
- HTML5技术实现Web图形图像处理——WebPhotoshop精简版
WebPhotoshop精简版是利用HTML5技术在Web上实现对图形图像的处理,构建易维护.易共享.易于拓展.实时性的Web图形图像处理平台. 精简版功能包括:图形绘制.图像处理.图像操作.完整版包 ...
- 抽象窗口工具包AWT (Abstract Window Toolkit) 是 API为Java 程序提供的建立 图形用户界面
抽象窗口工具包AWT (Abstract Window Toolkit) 是 API为Java 程序提供的建立 图形用户界面GUI (Graphics User Interface)工具集,AWT可用 ...
- AWT是Java基础类 (JFC)的一部分,为Java程序提供图形用户界面(GUI)的标准API
抽象窗口工具包 (Abstract Windowing Toolkit) (AWT)是Java的平台独立的窗口系统,图形和用户界面器件工具包. AWT是Java基础类 (JFC)的一部分,为Java程 ...
- Java图形用户界面编程
1.Java图形用户界面编程概述 JavaAPI中提供了两套组件用于支持编写图形用户界面:AWT(抽象窗口包)和Swing 2. 容器(Container):重量级容器和轻量级容器(一个容器可以放置 ...
随机推荐
- NetBeans工具学习之道:NetBeans的(默认)快捷键
没什么好介绍的,是netbeans的快捷键,比較全面.看到好多坛子里还在问eclipse下的这个快捷键怎么netbeans下没有呢.曾经收集的,如今列在以下: 事实上,在当前安装的netbeans的 ...
- 数据挖掘 决策树算法 ID3 通俗演绎
决策树是对数据进行分类,以此达到预測的目的.该决策树方法先依据训练集数据形成决策树,假设该树不能对全部对象给出正确的分类,那么选择一些例外添�到训练集数据中,反复该过程一直到形成正确的决策集.决策树代 ...
- mysql+ssh整合样例,附源代码下载
项目引用jar下载:http://download.csdn.net/detail/adam_zs/7262727 项目源代码下载地址:http://download.csdn.net/detail/ ...
- JavaScript 中的事件类型1(读书笔记思维导图)
Web 浏览器中可能发生的事件有很多类型.如前所述,不同的事件类型具有不同的信息,而“ DOM3级事件”规定了以下几类事件. UI(User Interface,用户界面)事件:当用户与页面上的元素交 ...
- POJ 1325 ZOJ 1364 最小覆盖点集
题意:有A,B两台机器, 机器A 有 n个模式(0, 1, 2....n-1),同样机器B有m个模式, 两个机器一开始的模式都为0,有k个作业(id,x,y) 表示作业编号id, 该作业必须在A机器在 ...
- selenium让人摸不着头脑的问题
selenium让人摸不着头脑的问题 问题一 使用webdriver驱动firefox浏览器时如果不设置参数,默认使用的Firefox的profile和平时打开浏览器使用的firefox不一样,如果要 ...
- cocos2D(四)---- CCSprite
在介绍CCSprite之前,先要理解游戏开发中的一个核心概念:精灵.精灵也称为游戏对象,它能够用来表示游戏中的不论什么物体,比方敌人.子弹.甚至是一个背景图片.一段文字.CCSprite能够说是在co ...
- [ACM] poj 1258 Agri-Net (最小生成树)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37131 Accepted: 14998 Descri ...
- Laravel 中国 - 巨匠级PHP开发框架 Laravel 中国社区
http://m.baidu.com/from=844b/bd_page_type=1/ssid=0/uid=3151E6C0905477A13653132D762BB6FB/pu=sz%401320 ...
- python手记(46)
#!/usr/bin/env python # -*- coding: utf-8 -*- #http://blog.csdn.net/myhaspl #code:myhaspl@qq.com ...