⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycalculor.java

📁 用java语言编写的简易的适合初学者参考的计算器的源代码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
public class Mycalculor extends Frame implements ActionListener
{
   private TextField screen=new TextField(25);
   private Button n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,mod;
   private Button back,clear,add,sub,qul,div,point,equl;
   private double temp=0;
   private String cha1="";
   private String str,cha;

   public Mycalculor()
   {
      super("计算器");
      setup();
      n1.addActionListener(this);
      n2.addActionListener(this);
      n3.addActionListener(this);
      n4.addActionListener(this);
      n5.addActionListener(this);
      n6.addActionListener(this);
      n7.addActionListener(this);
      n8.addActionListener(this);
      n9.addActionListener(this);
      n0.addActionListener(this);
      mod.addActionListener(this);
      clear.addActionListener(this);
      point.addActionListener(this);
      back.addActionListener(this);
      add.addActionListener(this);
      div.addActionListener(this);
      qul.addActionListener(this);
      sub.addActionListener(this);
      equl.addActionListener(this);
      addWindowListener(new WinClose());
      pack(); show();
  }
  private void setup()
 {
      Panel top=new Panel();
      top.add(screen);
      n1=new Button("1");
      n2=new Button("2");
      n3=new Button("3");
      n4=new Button("4");
      n5=new Button("5");
      n6=new Button("6");
      n7=new Button("7");
      n8=new Button("8");
      n9=new Button("9");
      n0=new Button("0");
      mod=new Button("%");
      add=new Button("+");
      sub=new Button("-");
      qul=new Button("*");
      div=new Button("/");
      clear=new Button("clear");
      back=new Button("back");
      point=new Button(".");
      equl=new Button("=");

      Panel input1=new Panel();
      input1.add(back);
      input1.add(clear);
      input1.add(mod);
      input1.setLayout(new GridLayout(1,3));

      Panel input=new Panel();
      input.add(n1);
      input.add(n2);
      input.add(n3);
      input.add(n4);
      input.add(n5);
      input.add(n6);
      input.add(n7);
      input.add(n8);
      input.add(n9);
      input.add(n0);
      input.add(point);
      input.add(equl);
      input.add(add);
      input.add(div);
      input.add(qul);
      input.add(sub);
      input.setLayout(new GridLayout(4,4));

      setLayout(new BorderLayout());
      add("North",top); add("Center",input1); add("South",input);
   }

   public void actionPerformed(ActionEvent e)
  {

  if(e.getSource()==n1)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="%"||cha=="*")
    screen.setText("");
    screen.setText(screen.getText()+"1");
    cha=e.getActionCommand();
  }
  if(e.getSource()==n2)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="*"||cha=="%")
    screen.setText("");
    screen.setText(screen.getText()+"2");
    cha=e.getActionCommand();
  }
  if(e.getSource()==n3)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="*"||cha=="%")
    screen.setText("");
    screen.setText(screen.getText()+"3");
    cha=e.getActionCommand();
  }
  if(e.getSource()==n4)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="*"||cha=="%")
    screen.setText("");
    screen.setText(screen.getText()+"4");
    cha=e.getActionCommand();
  }
  if(e.getSource()==n5)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="*"||cha=="%")
    screen.setText("");
    screen.setText(screen.getText()+"5");
    cha=e.getActionCommand();
  }
  if(e.getSource()==n6)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="*"||cha=="%")
    screen.setText("");
    screen.setText(screen.getText()+"6");
    cha=e.getActionCommand();
  }
  if(e.getSource()==n7)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="*"||cha=="%")
    screen.setText("");
    screen.setText(screen.getText()+"7");
    cha=e.getActionCommand();
  }
  if(e.getSource()==n8)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="*"||cha=="%")
    screen.setText("");
    screen.setText(screen.getText()+"8");
    cha=e.getActionCommand();
  }
  if(e.getSource()==n9)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="*"||cha=="%")
    screen.setText("");
    screen.setText(screen.getText()+"9");
    cha=e.getActionCommand();
  }
  if(e.getSource()==n0)
  {
    if(cha=="+"||cha=="-"||cha=="/"||cha=="*"||cha=="%")
    screen.setText("");
    screen.setText(screen.getText()+"0");
    cha=e.getActionCommand();
  }
  if(e.getSource()==clear)
    screen.setText("");
  if(e.getSource()==point)
  screen.setText(screen.getText()+".");
  if(e.getSource()==back)
    screen.setText(screen.getText().substring(0,screen.getText().length()-1));
  if(e.getSource()==add||e.getSource()==sub||e.getSource()==qul||e.getSource()==div||e.getSource()==mod)
  {
     temp=Double.parseDouble(screen.getText());
     cha=e.getActionCommand();
     cha1=e.getActionCommand();
  }
     if(e.getSource()==equl)
     {
       if(cha1=="+")
       screen.setText(str.valueOf((temp+Double.parseDouble(screen.getText()))));
       if(cha1=="-")
       screen.setText(str.valueOf((temp-Double.parseDouble(screen.getText()))));
       if(cha1=="/")
       screen.setText(str.valueOf((temp/Double.parseDouble(screen.getText()))));
       if(cha1=="*")
       screen.setText(str.valueOf((temp*Double.parseDouble(screen.getText()))));
       if(cha1=="%")
       screen.setText(str.valueOf(((int)temp%Integer.parseInt(screen.getText()))));
     }

}
public static void main(String args[])
{
   Mycalculor Mycal=new Mycalculor();
}


class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -