| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1909 人关注过本帖
标题:简单计算器..
取消只看楼主 加入收藏
zacom
Rank: 2
等 级:论坛游民
威 望:2
帖 子:381
专家分:15
注 册:2007-9-15
结帖率:88.89%
收藏
 问题点数:0 回复次数:3 
简单计算器..
自己边学边弄的程序...在代码和结构思想上有什么可以改正的请告诉下..
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
zacom
Rank: 2
等 级:论坛游民
威 望:2
帖 子:381
专家分:15
注 册:2007-9-15
收藏
得分:0 
没半点建议。。晕

没有最好只有更好
2008-10-15 19:55
zacom
Rank: 2
等 级:论坛游民
威 望:2
帖 子:381
专家分:15
注 册:2007-9-15
收藏
得分:0 
回复 6# nuciewth 的帖子
什么叫做大数版

没有最好只有更好
2008-10-21 12:27
zacom
Rank: 2
等 级:论坛游民
威 望:2
帖 子:381
专家分:15
注 册:2007-9-15
收藏
得分:0 
回复 11# 的帖子
我也刚学。。哈哈。。这些我还没发现。。过几天再改下。。还有没别的毛病。。

没有最好只有更好
2008-10-30 00:53
快速回复:简单计算器..
数据加载中...
 
   



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

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