java项目实战-计算器(页面布局学习 添加事件)
2014-09-21 23:32
585 查看
package com.ten.jiauanqi; import java.awt.*; import java.awt.event.*; public class Calculator extends WindowAdapter{ //定义3个面板 Panel p1=new Panel(); Panel p2=new Panel(); Panel p3=new Panel(); TextField txt;//创建文本框对象 private Button[] b=new Button[17];//创建按钮数组 private String ss[]={"7","8","9","+","4","5","6","-","1","2","3","*","清空","0","=","/","关闭"}; static double a;//创建double类型变量 static String s,str;//创建String类型变量 public static void main(String args[]) { (new Calculator()).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.add(txt,"North"); p2.setLayout(new GridLayout(4,4)); p3.setLayout(new BorderLayout()); p3.add(b[16]); //添加各个面板到窗口上 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=""; }else if(btn.getLabel()=="+") { jisuan(); txt.setText(""); s="+"; }else if(btn.getLabel()=="-") { jisuan(); txt.setText(""); s="-"; }else if(btn.getLabel()=="*") { jisuan(); txt.setText(""); s="*"; }else if(btn.getLabel()=="/") { jisuan(); txt.setText(""); s="/"; }else{ txt.setText(txt.getText()+btn.getLabel()); if(btn.getLabel()=="清空") txt.setText(""); } } 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); } } }
效果图:
相关文章推荐
- java实现简单的页面布局和按钮单击事件(学习中)
- 【java项目实战】Servlet详解以及Servlet编写登陆页面(二)
- Java页面布局设计计算器
- 【手把手】JavaWeb 入门级项目实战 -- 文章发布系统 (作者:剽悍一小兔)第六节学习随笔
- react 项目实战(二)创建 用户添加 页面 及 fetch请求 json-server db.json -w -p 8000
- 【java项目实战】Servlet详解以及Servlet编写登陆页面(二)
- java实战项目视频学习
- java架构师、集群、高可扩展、高性能、高并发、性能优化、Spring boot、Dubbo、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战学习架构师之路
- JavaWeb 学习(Java Web 典型模块与项目实战大全)
- java学习笔记之注册页面项目
- 【java项目实战】Servlet详解以及Servlet编写登陆页面(二)
- CocosCreator项目学习系列<二>关于Button(添加事件)输入控制交互条件的触发_实现虚拟按钮控制_JavaScript
- 2,JQuery学习-为页面的元素添加点击事件
- 【Android】利用Java代码布局,按钮添加点击事件
- java基础学习——Java添加事件监听的四种方法代码实例
- JAVA从菜鸟【入门】到新手【实习】一一Python项目实战学习规划
- java项目实战-超市管理系统(四)页面设计源码分析
- 把原来的普通java项目修改成web项目之后,添加jsp页面,从jsp页面发请求还到jsp页面,但出现java.lang.OutOfMemoryError: Java heap space异常
- 打通前后端全栈开发node+vue进阶【课程学习系统项目实战详细讲解】(3):用户添加/修改/删除 vue表格组件 vue分页组件
- 【手把手】JavaWeb 入门级项目实战 -- 文章发布系统 (作者:剽悍一小兔)第七、八、九节学习随笔