| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1908 人关注过本帖
标题:简单计算器..
只看楼主 加入收藏
zacom
Rank: 2
等 级:论坛游民
威 望:2
帖 子:381
专家分:15
注 册:2007-9-15
结帖率:88.89%
收藏
 问题点数:0 回复次数:14 
简单计算器..
自己边学边弄的程序...在代码和结构思想上有什么可以改正的请告诉下..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cal extends JFrame implements ActionListener
{
    private JButton[] buttons;
    private int i,k;
    private boolean point;
    private boolean yunsuan;
    private int m;
    private double j,p;
    private String str;
    private JTextField text;
    public cal() {
        super("MZP计算器");
        this.setSize(220,180);
        this.setLocation(400,300);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        i=0;
        k=1;
        point=false;
        yunsuan=false;
        buttons=new JButton[20];
        text=new JTextField("0");
        text.setHorizontalAlignment(JTextField.RIGHT);
        text.setEditable(false);
        this.add(text,"North");
        JPanel panel=new JPanel(new GridLayout(5,4));
        this.add(panel,"Center");
        buttons[0]=new JButton("sqrt");
        buttons[1]=new JButton("+/-");
        buttons[2]=new JButton("CE");
        buttons[3]=new JButton("C");
        buttons[4]=new JButton("7");
        buttons[5]=new JButton("8");
        buttons[6]=new JButton("9");
        buttons[7]=new JButton("/");
        buttons[8]=new JButton("4");
        buttons[9]=new JButton("5");
        buttons[10]=new JButton("6");
        buttons[11]=new JButton("*");
        buttons[12]=new JButton("1");
        buttons[13]=new JButton("2");
        buttons[14]=new JButton("3");
        buttons[15]=new JButton("-");
        buttons[16]=new JButton("0");
        buttons[17]=new JButton(".");
        buttons[18]=new JButton("=");
        buttons[19]=new JButton("+");
        for(int j=0;j<20;j++)
        {
            panel.add(buttons[j]);
            buttons[j].addActionListener(this);
        }
        this.setVisible(true);
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==buttons[0])
            {
                text.setText(""+Math.sqrt(Double.parseDouble(text.getText())));
                point=true;
            }
            if(e.getSource()==buttons[1])
            {
                if(i%2==0)
                {
                    text.setText("-"+text.getText());
                    i=1;
                }
                else
                {
                    str=text.getText();
                    str=str.substring(1);
                    text.setText(str);
                    i=0;
                }
            }
            if(e.getSource()==buttons[2]||e.getSource()==buttons[3])
            {
                text.setText("0");
            }
            if(e.getSource()==buttons[4]||e.getSource()==buttons[5]||e.getSource()==buttons[6]||e.getSource()==buttons[8]||e.getSource()==buttons[9]||e.getSource()==buttons[10]||
               e.getSource()==buttons[12]||e.getSource()==buttons[13]||e.getSource()==buttons[14]||e.getSource()==buttons[16])
               {
                    str=text.getText()+e.getActionCommand();
                    if(str.charAt(0)=='0'&&str.charAt(1)!='.'&&str.length()>2)
                       str=str.substring(1);
                    if(!yunsuan)
                       {
                        str=e.getActionCommand();
                        yunsuan=true;
                        }
                text.setText(str);            
            }
            if(e.getSource()==buttons[7]||e.getSource()==buttons[11]||e.getSource()==buttons[15]||e.getSource()==buttons[18]||e.getSource()==buttons[19])
            {
                if(k==1)
                {
                  yunsuan=true;
                  j=Double.parseDouble(text.getText());
                  if(e.getSource()==buttons[7])
                       m=0;
                  if(e.getSource()==buttons[11])
                      m=1;
                  if(e.getSource()==buttons[15])
                      m=2;
                  if(e.getSource()==buttons[19])
                      m=3;
                  text.setText(" ");
                  k++;
                }
                else
                {
                  str=text.getText();
                  if(str.equals(" "))
                       p=j;
                  else
                       p=Double.parseDouble(text.getText());
                  switch(m)
                  {
                      case 0:j=j/p;break;
                      case 1:j=j*p;break;
                      case 2:j=j-p;break;
                      case 3:j=j+p;break;
                  }
                  text.setText(""+j);
                  k=1;
                  point=true;
                  yunsuan=false;
                }
            }
            if(e.getSource()==buttons[17])
                if(!point)
                {
                    text.setText(text.getText()+".");
                    point=true;
                }
        }
        public static void main(String args[])
        {
            new cal();
        }
}
搜索更多相关主题的帖子: 计算器 
2008-10-12 11:52
b393993150
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-10-4
收藏
得分:0 
先谢谢了。我想要。地址:mrbingliang@
2008-10-13 13:04
longrm
Rank: 1
等 级:新手上路
帖 子:129
专家分:0
注 册:2007-6-18
收藏
得分:0 
自己直接拷贝源码就是啊,还留邮箱干嘛...

java群: 55919698

My blog: http://hi.baidu.com/longrm
2008-10-14 09:29
zacom
Rank: 2
等 级:论坛游民
威 望:2
帖 子:381
专家分:15
注 册:2007-9-15
收藏
得分:0 
没半点建议。。晕

没有最好只有更好
2008-10-15 19:55
longrm
Rank: 1
等 级:新手上路
帖 子:129
专家分:0
注 册:2007-6-18
收藏
得分:0 
这么简单的你想要什么建议???你能做成windows自带的那种就ok了,包括科学型界面

java群: 55919698

My blog: http://hi.baidu.com/longrm
2008-10-21 08:36
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
升级做成大数版。

倚天照海花无数,流水高山心自知。
2008-10-21 10:05
zacom
Rank: 2
等 级:论坛游民
威 望:2
帖 子:381
专家分:15
注 册:2007-9-15
收藏
得分:0 
回复 6# nuciewth 的帖子
什么叫做大数版

没有最好只有更好
2008-10-21 12:27
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
回复 7# zacom 的帖子
大数版就是能算N位数相加减乘除等等等等……
2008-10-21 16:50
chen_pei
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-7-24
收藏
得分:0 
我想要
2008-10-25 15:33
蓝色神话
Rank: 2
等 级:论坛游民
威 望:1
帖 子:404
专家分:24
注 册:2006-5-11
收藏
得分:0 
最好做成像微软Math那样的软件就好了,呵呵~!
2008-10-25 22:33
快速回复:简单计算器..
数据加载中...
 
   



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

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