java程序计算器简略的能算加减乘除就行

traveler0723 2021-09-18 16:36 180 次浏览 赞 69

最新问答

  • 贪玩欢子

    package time;
    import java.applet.*;
    import java.awt.*;
    import java.awt.Event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class TestInnerClass extends Applet{
    TextField text1,text2,text3;
    Button button1,button2;
    CheckboxGroup select;
    Checkbox a,s,m,d;
    public void init()
    {
    text1=new TextField(10);
    text2=new TextField(10);
    text3=new TextField(10);
    button1=new Button("计算");
    button2=new Button("重置");
    select=new CheckboxGroup();
    a=new Checkbox("加",true,select);
    s=new Checkbox("减",true,select);
    m=new Checkbox("乘",true,select);
    d=new Checkbox("除",true,select);
    add(a);add(m);add(s);add(d);
    add(text1);add(text2);add(text3);
    add(button1);add(button2);
    text1.addActionListener(new TextAct());
    text2.addActionListener(new TextAct());
    button1.addActionListener(new ButtonAct());
    button2.addActionListener(new ButtonAct());
    }
    class TextAct implements ActionListener{
    public void actionPerformed(ActionEvent e)
    {
    TextField text;
    int operand;
    text=(TextField)(e.getSource());
    operand=Integer.parseInt(text.getText());
    if(operand<0|operand>100)
    text.setText("输入整据越界");
    }
    }
    class ButtonAct implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    int op1,op2,op3;
    if(e.getSource()==button1)
    {
    op1=Integer.parseInt(text1.getText());
    op2=Integer.parseInt(text2.getText());
    if(a.getState())
    op3=op1+op2;
    else if(s.getState())
    op3=op1-op2;
    else if(m.getState())
    op3=op1*op2;
    else
    op3=op1/op2;
    text3.setText(Integer.toString(op3));
    }
    else
    {
    text1.setText("");
    text2.setText("");
    text3.setText("");
    a.setState(true);
    }
    }
    }}

    浏览 171赞 156时间 2024-01-11

java程序计算器简略的能算加减乘除就行