JAVA 用JAVA设计程序,实现一个计算器,并能实现计算

30岁男人的世界 2021-09-18 16:36 466 次浏览 赞 96

最新问答

  • beyond45678

    package whf;
    import java.awt.*;
    import java.awt.event.*;
    public class Jisuan extends Frame
    implements ActionListener
    {
    TextField tf=new TextField();
    Panel p=new Panel();
    Button a0=new Button("0");
    Button a1=new Button("1");
    Button a2=new Button("2");
    Button a3=new Button("3");
    Button a4=new Button("4");
    Button a5=new Button("5");
    Button a6=new Button("6");
    Button a7=new Button("7");
    Button a8=new Button("8");
    Button a9=new Button("9");
    Button b1=new Button("+");
    Button b2=new Button("-");
    Button b3=new Button("*");
    Button b4=new Button("CE");
    Button b5=new Button("=");
    public Jisuan()
    {

    this.setTitle("我的计算器");
    this.setLayout(new BorderLayout());
    this.add(tf,BorderLayout.NORTH);
    this.add(p,BorderLayout.CENTER);
    p.setLayout(new GridLayout(3,5));
    /*for(int i=0;i<10;i++)
    {
    Button btemp=new Button(""+i);
    p.add(btemp,""+i);
    btemp.addActionListener(this);
    }*/
    p.add(a0);
    p.add(a1);
    p.add(a2);
    p.add(a3);
    p.add(a4);
    p.add(a5);
    p.add(a6);
    p.add(a7);
    p.add(a8);
    p.add(a9);
    p.add(b1);
    p.add(b2);
    p.add(b3);
    p.add(b4);
    p.add(b5);
    a0.addActionListener(this);
    a1.addActionListener(this);
    a2.addActionListener(this);
    a3.addActionListener(this);
    a4.addActionListener(this);
    a5.addActionListener(this);
    a6.addActionListener(this);
    a7.addActionListener(this);
    a8.addActionListener(this);
    a9.addActionListener(this);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    this.setBounds(100,100,400,400);
    this.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
    int ys=0;int sum1=0;int sum2=0;
    if(e.getSource()==a0)
    {
    tf.setText(tf.getText()+0);
    }else if(e.getSource()==a1)
    {
    tf.setText(tf.getText()+1);
    }
    else if(e.getSource()==a2)
    {
    tf.setText(tf.getText()+2);
    }else if(e.getSource()==a3)
    {
    tf.setText(tf.getText()+3);

    }else if(e.getSource()==a4)
    {
    tf.setText(tf.getText()+4);
    }else if(e.getSource()==a5)
    {
    tf.setText(tf.getText()+5);
    }else if(e.getSource()==a6)
    {
    tf.setText(tf.getText()+6);
    }else if(e.getSource()==a7)
    {
    tf.setText(tf.getText()+7);
    }else if(e.getSource()==a8)
    {
    tf.setText(tf.getText()+8);
    }else if(e.getSource()==a9)
    {
    tf.setText(tf.getText()+9);
    }else if(e.getSource()==b1)
    {
    ys=1;
    sum1=Integer.parseInt(tf.getText().trim());
    tf.setText("");
    }else if(e.getSource()==b2)
    {
    ys=2;
    sum1=Integer.parseInt(tf.getText().trim());
    tf.setText("");
    }else if(e.getSource()==b3)
    {
    ys=3;
    sum1=Integer.parseInt(tf.getText().trim());
    tf.setText("");
    }else if(e.getSource()==b4)
    {
    sum=sum1=sum2=0;
    tf.setText("");
    }else if(e.getSource()==b5)
    {
    sum2=Integer.parseInt(tf.getText().trim());
    switch(ys)
    {
    case 1:tf.setText(Int.toString((sum1+sum2)).trim());
    break;
    case 2:tf.setText(Int.toString((sum1-sum2)).trim());
    break;
    case 3:tf.setText(Int.toString((sum1*sum2)).trim());
    break;
    }

    }
    }
    public static void main(String args[])
    {
    new Jisuan();
    }
    }

    浏览 386赞 75时间 2024-01-04
  • 今天属于1

    不知道你想实现什么样的计算。

    浏览 430赞 136时间 2021-09-27

JAVA 用JAVA设计程序,实现一个计算器,并能实现计算