| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 576 人关注过本帖
标题:我用java做的计算器,请大虾们帮忙优化一下
只看楼主 加入收藏
m8oo
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-11-28
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:1 
我用java做的计算器,请大虾们帮忙优化一下
package Jisuanqi;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class calculate extends JFrame implements ActionListener
{
    private final String [] a= {"7","8","9","C","4","5","6","/","1","2","3","*","0","-","+","="};
    private Button b[] = new Button[a.length];
    private JTextField ipt;
    String ch ="";
    String ch1="";
    String ch2 ="";
    long result = 0;
   
    private int i = 0;

    public calculate()
    {
        super("计算器");
        this.setBounds(300,240,250,200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new BorderLayout());
        this.getContentPane().add(panel,"North");
        ipt = new JTextField();
        ipt.setHorizontalAlignment(JTextField.RIGHT);
        panel.add(ipt);
        JPanel panel1 = new JPanel(new GridLayout(4,4,3,3));
        this.getContentPane().add(panel1);
        for(int i = 0;i<a.length;i++)
        {
            b[i]=new Button(a[i]);
            panel1.add(b[i]);
            b[i].addActionListener(this);
        }
        this.setVisible(true);
    }
   
    public void actionPerformed(ActionEvent e)
    {   
        if(e.getSource()==b[0])
        {
            ch="7";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[1])
        {
            ch="8";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[2])
        {
            ch="9";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[3])
        {
            ipt.setText("");
        }if(e.getSource()==b[4])
        {
            ch="4";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[5])
        {
            ch="5";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[6])
        {
            ch="6";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[7])
        {
            i=1;
            ch1=ipt.getText();
            ipt.setText("");
        }if(e.getSource()==b[8])
        {
            ch="1";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[9])
        {
            ch="2";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[10])
        {
            ch="3";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[11])
        {   
            i=2;
            ch1=ipt.getText();
            ipt.setText("");
        }if(e.getSource()==b[12])
        {
            ch="0";
            ipt.setText(ipt.getText()+ch);
        }if(e.getSource()==b[13])
        {
            i=3;
            ch1=ipt.getText();
            ipt.setText("");
        }if(e.getSource()==b[14])
        {
            i=4;
            ch1=ipt.getText();
            ipt.setText("");
        }if(e.getSource()==b[15])
        {
            switch(i)
            {
            case 1:chu();break;
            case 2:cheng();break;
            case 3:jian();break;
            case 4:jia();break;
            }
        }
    }
    private void chu()
        {
        if(Integer.parseInt(ipt.getText())==0)
        {
            ipt.setText("除数不能为零!");
        }
            result = Integer.parseInt(ch1)/Integer.parseInt(ipt.getText());
            ipt.setText(""+result);
        }
    private void cheng()
    {
        result = Integer.parseInt(ch1)*Integer.parseInt(ipt.getText());
        ipt.setText(""+result);
    }
    private void jia()
    {
        result = Integer.parseInt(ch1)+Integer.parseInt(ipt.getText());
        ipt.setText(""+result);
    }
    private void jian()
    {
        result = Integer.parseInt(ch1)-Integer.parseInt(ipt.getText());
        ipt.setText(""+result);
    }
    public static void main(String args[])
    {
        new calculate();
    }
}
搜索更多相关主题的帖子: 计算器 java 
2010-11-29 14:59
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:20 
数值可以优化成Double

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2010-11-29 17:29
快速回复:我用java做的计算器,请大虾们帮忙优化一下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012688 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved