| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1106 人关注过本帖
标题:求最简单计算器
只看楼主 加入收藏
liuhuobi
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-7-25
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:5 
求最简单计算器
哪位大妈给个只能加减乘除的计算器,男人简单就好!!
搜索更多相关主题的帖子: 求最简单计算器代码 
2009-07-25 18:57
pywepe
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:296
专家分:483
注 册:2009-4-5
收藏
得分:1 
回复 楼主 liuhuobi
随便写了一个
ps:有错误,我已没心思修复了,你自己按这样的思想去改吧

package pcenshao.samplecalculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainWindow extends JFrame implements ActionListener{
    
    private JPanel mainPanel;
    private JButton one;
    private JButton two;
    private JButton three;
    private JButton four;
    private JButton five;
    private JButton six;
    private JButton seven;
    private JButton eight;
    private JButton nine;
    private JButton zero;
    private JButton plus;
    private JButton minus;
    private JButton multiply;
    private JButton divide;
    private JButton c;
    private JButton result;
    private JTextField text;
    private double num1=0;
    private double num2=0;
    private double rst=0;
    
    private Object[] o;
    private int index=-1;
    
    private boolean haveComputer=false;
    
    public MainWindow(String title){
        super(title);
        this.setVisible(true);
        this.setResizable(false);
        Toolkit tk=Toolkit.getDefaultToolkit();
        Dimension size=tk.getScreenSize();
        int w=(int)size.getWidth();
        int h=(int)size.getHeight();
        this.setBounds(w/3,h/4,260,300);
        this.text=new JTextField();
        text.setSize(256,40);
        text.setHorizontalAlignment(JTextField.RIGHT);
       
        this.getContentPane().add(this.text);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.intiButtons();
        
        o=new Object[10];
        for(int i=0;i<10;i++){
            o[i]=null;
        }
    
    }
    
    private void intiButtons(){
        this.mainPanel=new JPanel();
        mainPanel.setSize(260,220);
        this.one=new JButton("1");
        this.two=new JButton("2");
        this.three=new JButton("3");
        this.four=new JButton("4");
        this.five=new JButton("5");
        this.six=new JButton("6");
        this.seven=new JButton("7");
        this.eight=new JButton("8");
        this.nine=new JButton("9");
        this.zero=new JButton("0");
        this.plus=new JButton("+");
        this.minus=new JButton("-");
        this.multiply=new JButton("*");
        this.divide=new JButton("/");
        this.c=new JButton("C");
        this.result=new JButton("=");
        
        
        GridLayout layout=new GridLayout(4,5);
        mainPanel.setLayout(layout);
        mainPanel.add(this.plus);
        mainPanel.add(this.minus);
        mainPanel.add(this.multiply);
        mainPanel.add(this.divide);
        mainPanel.add(this.c);
        mainPanel.add(this.one);
        mainPanel.add(this.two);
        mainPanel.add(this.three);
        mainPanel.add(this.four);
        mainPanel.add(this.five);
        mainPanel.add(this.six);
        mainPanel.add(this.seven);
        mainPanel.add(this.eight);
        mainPanel.add(this.nine);
        mainPanel.add(this.zero);
        mainPanel.add(this.result);
        this.getContentPane().add(mainPanel);
        mainPanel.setLocation(0,48);
        
        one.addActionListener(this);
        two.addActionListener(this);
        three.addActionListener(this);
        four.addActionListener(this);
        five.addActionListener(this);
        six.addActionListener(this);
        seven.addActionListener(this);
        eight.addActionListener(this);
        nine.addActionListener(this);
        zero.addActionListener(this);
        
        plus.addActionListener(this);
        minus.addActionListener(this);
        this.divide.addActionListener(this);
        this.multiply.addActionListener(this);
        c.addActionListener(this);
        result.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent e){
        JButton tmp=(JButton)e.getSource();
        if(!(tmp.getText().equals("=")||tmp.getText().equals("C")||tmp.getText().equals("+")
                ||tmp.getText().equals("-")||tmp.getText().equals("*")||tmp.getText().equals("/")))
        {
            String str="";
            if(text.getText().equals("")){
                str=tmp.getText();
                text.setText(str);
            }else{
                str=text.getText()+tmp.getText();
                text.setText(str);
            }
        }else if(tmp.getText().equals("=")){
            double tmp1=Double.valueOf(text.getText());
            this.inQ(new Double(tmp1));
            this.inQ(tmp.getText());
            text.setText(new Double(()).toString());
        }else{
            text.setText("");
            this.index=-1;
        }
    }
    
    public void inQ(Object o){  //模拟入队
        this.o[this.index+1]=o;
        this.index++;
    }
    public Object outQ(){//模拟出队
        Object d;
        d=this.o[this.index];
        this.index--;
        return d;
    }
   
    public double compute(){
        if(this.index==-1)return 0;
        double result=0;
        String m=null;
        result=((Double)this.outQ()).doubleValue();
        for(int i=0;i<this.index-1;i++){
           m=(String)this.outQ();
           char[] ch=m.toCharArray();
           char c=ch[0];
           switch(c){
               case '+':result+=((Double)this.outQ()).doubleValue();
               case '-':result-=((Double)this.outQ()).doubleValue();
               case '*':result*=((Double)this.outQ()).doubleValue();
               case '/':result/=((Double)this.outQ()).doubleValue();
           }
           
        }
        return result;
    }
    
    public static void main(String args[]){
        new MainWindow("SampleCalculator");
    }

}

java群
62635216
欢迎加入
2009-07-25 22:32
windizual
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:124
专家分:186
注 册:2009-7-1
收藏
得分:1 
呵呵,楼上的仁兄很是强悍啊

Java要学的东西好多~~~~~
2009-08-05 22:11
liuhuobi
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-7-25
收藏
得分:0 
那么长呀 给个3个JTextField  1个JComboBox(+ - * /) 1个JButton(=)  就OK了
2009-08-06 18:29
jackhome
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2009-7-6
收藏
得分:1 
曾经也写过,但是都忘记了
2009-08-10 17:00
wyp0927
Rank: 2
等 级:论坛游民
帖 子:15
专家分:32
注 册:2009-2-10
收藏
得分:1 
我写了一个,但编译时中间有个错误,我改了几次还是通过不了编译,我已标出。希望大家也帮我改改,谢了。
import java.awt.*;
import java.awt.event.*;
public class C3 extends Frame implements ActionListener
{
    static C3 frm=new C3();
    static TextField txf1=new TextField("");
    static TextField txf2=new TextField("");
    static TextField txf3=new TextField("");
    static Button btn1=new Button("加");
    static Button btn2=new Button("减");
    static Button btn3=new Button("乘");
    static Button btn4=new Button("除");
public static void mian(String args[])
                {
    btn1.addActionListener(frm);
    btn2.addActionListener(frm);
    btn3.addActionListener(frm);   
    btn4.addActionListener(frm);
    frm.setSize(300,150);
    frm.setLayout(null);
    frm.setBackground(Color.yellow);
    frm.setTitle("简易计算器");
    txf1.setBounds(30,40,70,30);
    txf2.setBounds(110,40,70,30);
    txf3.setBounds(190,40,70,30);
    btn1.setBounds(30,100,50,30);
    btn2.setBounds(90,100,50,30);
    btn3.setBounds(150,100,50,30);
    btn4.setBounds(210,100,50,30);
    frm.add(txf1);
    frm.add(txf2);
    frm.add(txf3);
    frm.add(btn1);
    frm.add(btn2);
    frm.add(btn3);
    frm.add(btn4);
    frm.setVisible(true);
              }
    Double A1,A2,C1,C2,C3;
    public void actionPerformed(ActionEvent e)
              {
    A1=((Double)valueOf(txf1.getText())).doubleValue();        //此处提示编译出错
    A2=((Double)valueOf(txf2.getText())).doubleValue();
    C1=A1+A2;
    C2=A1-A2;
    C3=A1*A2;
    C4=A1/A2;
    Button btn=(Button)e.getSource();
    if(btn==btn1)
        txf3.setText(String.valueOf(C1));
    else if(btn==btn2)
        txf3.setText(String.valueOf(C2));
    else if(btn==btn3)
        txf3.setText(String.valueOf(C3));
    else if(btn==btn4)
        txf3.setText(String.valueOf(C4));


              }
}
2009-08-25 21:23
快速回复:求最简单计算器
数据加载中...
 
   



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

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