简单的图形用户界面—简单计算器的实现
来源:互联网 发布:淘宝店铺头像图片大全 编辑:程序博客网 时间:2024/05/22 17:38
简单的图形用户界面—简单计算器的实现
今天把Java实验报告上交了,其中一个是设计一个简单的计算器。现在就发表下自己写的一个简单的计算器,嘿嘿,个人感觉界面挺丑的,不过基本的加减乘除是可以实现的,下面的代码都是自己学过的类和方法,很基础,本人也是刚入门,如果不好,希望大家凑合看下哈,嘿嘿,先上图:
代码附上各位互相交流哈!
/** * @Author Chen Chao in ZJNU * @Date 2014-06-08 * */ import java.awt.Color;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField; public class CalcullatorApp extends JFrame implements ActionListener{String temp ="";float num2;float num1;int flag;JTextField ioput; JButton one,two ,thr,fo,fi,six,seven,ei,nine,z,btn_jia,btn_jian,btn_cheng,btn_chu,btn_deng,c,sin,cos,tan,back,zf,dian;public CalcullatorApp(){setSize(300, 200);setTitle("MyCalculator");setResizable(false);//setBounds(300, 400, 300, 400);setLayout(null);ioput = new JTextField();ioput.setBounds(5,5,285,25);ioput.setHorizontalAlignment(JTextField.RIGHT); ioput.setText(""); ioput.setForeground(Color.GREEN); ioput.setFont(new Font("SansSerif", Font.BOLD, 20)); add(ioput); //输出框布局 one = new JButton("1"); one.addActionListener(this); one.setBounds(10, 35, 45, 16); two = new JButton("2"); two.addActionListener(this); two.setBounds(60, 35, 45, 16); thr = new JButton("3"); thr.addActionListener(this); thr.setBounds(110, 35, 45, 16); btn_jia = new JButton("+"); btn_jia.addActionListener(this); btn_jia.setBounds(245, 35, 45, 16); c = new JButton("C"); c.addActionListener(this); c.setBounds(180, 35, 55, 16); //第一行布局 fo = new JButton("4"); fo.addActionListener(this); fo.setBounds(10, 65, 45, 16); fi = new JButton("5"); fi.addActionListener(this); fi.setBounds(60, 65, 45, 16); six = new JButton("6"); six.addActionListener(this); six.setBounds(110, 65, 45, 16); btn_jian = new JButton("-"); btn_jian.addActionListener(this); btn_jian.setBounds(245, 65, 45, 16); sin = new JButton("sin"); sin.addActionListener(this); sin.setBounds(180, 65, 55, 16); //第二行布局 seven = new JButton("7"); seven.addActionListener(this); seven.setBounds(10, 95, 45, 16); ei = new JButton("8"); ei.addActionListener(this); ei.setBounds(60, 95, 45, 16); nine = new JButton("9"); nine.addActionListener(this); nine.setBounds(110, 95, 45, 16); btn_cheng= new JButton("*"); btn_cheng.addActionListener(this); btn_cheng.setBounds(245, 95, 45, 16); cos= new JButton("cos"); cos.addActionListener(this); cos.setBounds(180, 95, 55, 16); //第三行布局 z = new JButton("0"); z.addActionListener(this); z.setBounds(10, 125, 45, 16); // btn_deng= new JButton("=");// btn_deng.addActionListener(this);// btn_deng.setBounds(60, 125, 45, 16); back= new JButton("←"); back.addActionListener(this); back.setBounds(108, 125, 48, 16); tan= new JButton("tan"); tan.addActionListener(this); tan.setBounds(180, 125, 55, 16); btn_chu= new JButton("/"); btn_chu.addActionListener(this); btn_chu.setBounds(245, 125, 45, 16); //第四行布局 zf= new JButton("+/-"); zf.addActionListener(this); zf.setBounds(10, 150, 70, 20); dian= new JButton("."); dian.addActionListener(this); dian.setBounds(85, 150, 70, 20); btn_deng= new JButton("="); btn_deng.addActionListener(this); btn_deng.setBounds(180, 150, 110, 20); //第五行布局 add(one); add(two); add(thr); add(c); add(btn_jia); add(fo); add(fi); add(six); add(sin); add(btn_jian); add(seven); add(ei); add(nine); add(cos); add(btn_cheng); add(z); add(back); add(tan); add(btn_chu); add(zf); add(dian); add(btn_deng); } //----------------------------------------------------------------------------- @Overridepublic void actionPerformed(ActionEvent e1){JButton btn = (JButton) e1.getSource();if(btn==one||btn==two||btn==thr||btn==fo||btn==fi||btn==six||btn==seven||btn==ei||btn==nine||btn==z||btn==dian){temp = temp +e1.getActionCommand(); ioput.setText(String.valueOf(temp));}if(btn == btn_jia||btn==btn_jian||btn==btn_cheng||btn==btn_chu||btn==cos||btn==sin||btn==tan){if(btn == btn_jia){flag = 1;num1=Float.parseFloat(ioput.getText());}if(btn==btn_jian){flag = 2;num1=Float.parseFloat(ioput.getText());}if(btn==btn_cheng){flag = 3;num1=Float.parseFloat(ioput.getText());}if(btn==btn_chu){flag = 4;num1=Float.parseFloat(ioput.getText());}if(btn==sin){flag= 5;num1=Float.parseFloat(ioput.getText());}if(btn == cos){flag =6;num1 = Float.parseFloat(ioput.getText());}if(btn == tan){flag = 7;num1 = Float.parseFloat(ioput.getText());}temp = "";}if(btn == btn_deng){num2=Float.parseFloat(ioput.getText());if(flag == 1){num1 = num1 + num2;ioput.setText(String.valueOf(num1));}if(flag == 2){num1 = num1-num2;ioput.setText(String.valueOf(num1));}if(flag ==3){num1= num1*num2;ioput.setText(String.valueOf(num1));}if(flag == 4){//try{num1 = num1/num2;if(num2==0){ioput.setText("除数不能为0");}elseioput.setText(String.valueOf(num1));//}//catch(ArithmeticException e)//{//ioput.setText("除数为0无意义");//}}if(flag == 5){num1 = (float) Math.sin(num2);ioput.setText(String.valueOf(num1));}if(flag == 6){num1 = (float) Math.cos(num2);ioput.setText(String.valueOf(num1));}temp="";}//-----------------------------------------if(btn == c){temp="";ioput.setText("");}//置空if(btn == back){try{temp = temp.substring(0,temp.length()-1);ioput.setText(String.valueOf(temp));}catch(StringIndexOutOfBoundsException e){ioput.setText("字符索引越界异常!!!");}} //回格if(btn == zf){if(ioput.getText().isEmpty()==false) { Float lnum=-(Float.parseFloat(ioput.getText())); ioput.setText(Double.toString(lnum)); } else { ioput.setText("-"); }}//输入负数}public static void main(String[] args){CalcullatorApp ca = new CalcullatorApp();ca.setIconImage(new ImageIcon("images/focus.png").getImage());ca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);ca.setVisible(true); }}看的出来,写的很基础把,希望不要嫌弃哟。谢谢参考!生活愉快!
0 0
- 简单的图形用户界面—简单计算器的实现
- 简单的图形用户界面
- 简单的图形用户界面猜数字
- 一种简单的图形用户界面-Java
- 简单的计算器实现
- 简单计算器的实现
- 简单计算器的实现
- 简单计算器的实现
- 计算器的简单实现
- 简单计算器的实现
- 简单计算器的实现
- 简单计算器的实现
- 简单的计算器的实现
- javascript实现的简单计算器
- 简单计算器的java实现
- java实现简单的计算器
- Java实现的简单计算器
- 简单计算器的Java实现
- jquery.validate清除同表单的上次验证结果
- Andriod 拨打电话、读取、拦截短信及发送短信
- 一个简单的java socket编程例子
- ubuntu安装:matlab2012a+flush player+音频播放器
- nginx负载均衡简单配置
- 简单的图形用户界面—简单计算器的实现
- Eclipse的常用设置
- USER 版本与ENG 版本差异
- F12键按下后光标Focus到某TextBox
- Android限制卸载APK
- NOJ1050数的计数——水题
- struts2几种result type探究
- Ubuntu14.04LTS上安装fcitx输入法
- Java中的线程(1)