Java基础篇(一)-----计算器
2016-06-19 23:41
411 查看
package caculator; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Caculator extends WindowAdapter{ //定义3个面板 Panel p1=new Panel(); //创建面板1 Panel p2=new Panel(); //创建面板2 Panel p3=new Panel(); //创建面板3 TextField txt; //创建文本框对象 private Button[] b=new Button[17];//创建按钮数组 private String ss[]={"7","8","9","+","4","5","6","-","1","2","3","*","清空","0","=","/","关闭"};//创建字符串数组 static double a=0.0; //创建double类型变量 static String s,str,s1=" "; //创建String类型变量 public static void main(String[] args) throws Exception { (new Caculator()).frame(); } public void frame() {//实现界面 Frame fm=new Frame("计算器"); //创建窗口对象 for(int i=0;i<=16;i++) { b[i]=new Button(ss[i]); //为按钮数组赋值 } for(int i=0;i<=15;i++){ p2.add(b[i]); //添加按钮到面板上去 } //b[16].setBackground(Color.yellow);//设置按钮的背景色为黄色 //创建和设置文本框 txt=new TextField(15); txt.setEditable(false); for(int i=0;i<=16;i++) { b[i].addActionListener(new buttonlistener());//为按钮添加监听器 } b[16].addActionListener(new close()); //为按钮添加关闭监听器 fm.addWindowListener(this); fm.setBackground(Color.red); //设置窗口背景为红色 p1.setLayout(new BorderLayout());//设置面板p1布局管理器 p1.add(txt, "North");//添加文本框到北面部分 p2.setLayout(new GridLayout(4,4));//设置面板p2布局管理器 p3.setLayout(new BorderLayout()); //设置面板p3布局管理器 p3.add(b[16]); //添加按钮到面板p3 //添加各个面板到窗口 fm.add(p1,"North"); fm.add(p2, "Center"); fm.add(p3, "South"); fm.pack(); fm.setVisible(true); } public void windowClosing(WindowEvent e) { System.exit(0); //退出系统 } //编写事件监听器 class buttonlistener implements ActionListener { public void actionPerformed(ActionEvent e){ Button btn=(Button)e.getSource(); //获取发生事件按钮 if(btn.getLabel()=="="){ jisuan(); str=String.valueOf(a); txt.setText(str); s=""; s1=""; }else if(btn.getLabel()=="+") { jisuan(); txt.setText(""); s="+"; s1=""; }else if(btn.getLabel()=="-"){ jisuan(); txt.setText(""); s="-"; s1=""; }else if(btn.getLabel()=="*"){ jisuan(); txt.setText(""); s="*"; s1=""; }else if(btn.getLabel()=="/"){ jisuan(); txt.setText(""); s="/"; s1=""; }else if(btn.getLabel()=="0"){ s1+=btn.getLabel(); txt.setText(s1); }else if(btn.getLabel()=="1"){ s1+=btn.getLabel(); txt.setText(s1); }else if(btn.getLabel()=="2"){ s1+=btn.getLabel(); txt.setText(s1); }else if(btn.getLabel()=="3"){ txt.setText("3"); }else if(btn.getLabel()=="4"){ s1+=btn.getLabel(); txt.setText(s1); }else if(btn.getLabel()=="5"){ s1+=btn.getLabel(); txt.setText(s1); }else if(btn.getLabel()=="6"){ s1+=btn.getLabel(); txt.setText(s1); }else if(btn.getLabel()=="7"){ s1+=btn.getLabel(); txt.setText(s1); }else if(btn.getLabel()=="8"){ s1+=btn.getLabel(); txt.setText(s1); } else if(btn.getLabel()=="9"){ s1+=btn.getLabel(); txt.setText(s1); }else { txt.setText("0"); s1=""; } } public void jisuan(){ if(s=="+") a+=Double.parseDouble(txt.getText()); else if(s=="-") a-=Double.parseDouble(txt.getText()); else if(s=="*") a*=Double.parseDouble(txt.getText()); else if(s=="/") a/=Double.parseDouble(txt.getText()); else a=Double.parseDouble(txt.getText()); } } class close implements ActionListener { public void actionPerformed(ActionEvent e){ System.exit(0); } } }简单演示:8+456=464.0
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树