您的位置:首页 > 其它

一个非常非常简单的计算器

2011-12-08 22:32 302 查看
       今天被要求写个iphone上的计算器,毕竟还不是很熟,就先花一个小时用Java写了个,过下流程,非常非常简单。代码如下:

/*
* 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]);
}

}
}
}


最后运行的结果是:





界面很丑吧,我也觉的很丑,无所谓啦。稍微测试了下,应该没什么问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: