一个非常非常简单的计算器
2011-12-08 22:32
302 查看
今天被要求写个iphone上的计算器,毕竟还不是很熟,就先花一个小时用Java写了个,过下流程,非常非常简单。代码如下:
最后运行的结果是:
![](http://images.cnblogs.com/cnblogs_com/wihoho/201112/201112082231509603.png)
界面很丑吧,我也觉的很丑,无所谓啦。稍微测试了下,应该没什么问题。
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package calculator; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * * @author GL */ public class Calculator extends JFrame{ /** * @param args the command line arguments */ int first = 0; int second = 0; int select = 0; int counter = 0; input in; JLabel out = new JLabel("0"); public static void main(String[] args) { // TODO code application logic here JFrame j = new Calculator(); } public Calculator(){ in = new input(); setLayout(new BorderLayout()); add(in,BorderLayout.CENTER); add(out,BorderLayout.NORTH); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Calculator"); } class input extends JPanel{ public input(){ setLayout(new GridLayout(5,4)); JButton[] enter = new JButton[20]; enter[0] = new JButton("c"); enter[1] = new JButton("<-"); enter[2] = new JButton("/"); enter[3] = new JButton("*"); enter[4] = new JButton("7"); enter[5] = new JButton("8"); enter[6] = new JButton("9"); enter[7] = new JButton("-"); enter[8] = new JButton("4"); enter[9] = new JButton("5"); enter[10] = new JButton("6"); enter[11] = new JButton("+"); enter[12] = new JButton("1"); enter[13] = new JButton("2"); enter[14] = new JButton("3"); enter[15] = new JButton("="); enter[16] = new JButton("0"); enter[17] = new JButton("."); enter[18] = new JButton("+/-"); enter[19] = new JButton("f"); enter[0].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); first = 0; second = 0; select = 0; out.setText("0"); } }); enter[1].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); } }); enter[2].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); select = 4; counter = 0; out.setText("/"); } }); enter[3].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); select = 3; counter = 0; out.setText("*"); } }); enter[4].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 7 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 7 + (int)(second*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[5].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 8 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 8 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[6].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 9 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 9 + (int)(second*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[7].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); select = 2; counter = 0; out.setText("-"); } }); enter[8].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 4 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 4 + (int)(second*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[9].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 5 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 5 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[10].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 6 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 6 + (int)(second*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[11].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); select = 1; counter = 0; out.setText("+"); } }); enter[12].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 1 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 1 + (int)(second*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[13].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 2 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 2 + (int)(second*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[14].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 3 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 3 + (int)(second*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[15].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); switch(select){ case 1: out.setText(String.valueOf(first+second)); break; case 2: out.setText(String.valueOf(first-second)); break; case 3: out.setText(String.valueOf(first*second)); break; case 4: out.setText(String.valueOf(first/second)); break; } first = 0; second = 0; select = 0; counter = 0; } }); enter[16].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 0 + (int)(first*Math.pow(10, counter)); counter++; out.setText(String.valueOf(first)); } else{ second = 0 + (int)(second*Math.pow(10, counter)); counter++; out.setText(String.valueOf(second)); } } }); enter[17].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); } }); enter[18].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); if(select == 0){ first = 0 -first; out.setText(String.valueOf(first)); } else { second = 0- second; out.setText(String.valueOf(second)); } } }); enter[19].addActionListener(new ActionListener( ) { @Override public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); } }); for(int i = 0; i < 20; i++){ add(enter[i]); } } } }
最后运行的结果是:
![](http://images.cnblogs.com/cnblogs_com/wihoho/201112/201112082231509603.png)
界面很丑吧,我也觉的很丑,无所谓啦。稍微测试了下,应该没什么问题。
相关文章推荐
- 一个比较简单、非常简单的计算器
- 用JS 写个非常简单的计算器 算是学习JS的一个开头
- 一个非常简单的计算器
- AutoTRACE是分析SQL的执行计划,执行效率的一个非常简单方便的工具
- 一个能够实现简单功能的计算器
- 用C语言实现一个简单的计算器代码
- 用arduino做一个简单的计算器
- 一个非常简单的算法题是否愿意挑战一下呢
- VS2010 C++ MFC框架学习笔记2 - 创建一个简单的加法计算器(2)
- 用java编写一个简单的计算器
- 用JS,做一个简单的计算器
- 实现一个简单的计算器
- 编写一个简单的计算器使用图形用户界面
- 一个简单不过却很非常实用的PHP加密字符串方法
- 一个非常简单的求随机数的算法
- 做一个简单的计算器
- mips汇编简单实例——一个小计算器
- 一个简单的计算器
- 【设计模式】简单工厂模式——以一个简单的计算器为例
- [Android开源]一个非常简单易用用来花式展示二维码样式生成的库QRCodeStyle