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

📄 新建 文本文档 (6).txt

📁 简单的计算器
💻 TXT
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class ddd1 extends Applet implements ActionListener
{private static final long serialVersionUID = 1L;
 String x="",y="",fh="";
 double answer;
 JTextField tfAnswer; //定义用于输入和输出的文本框
 Button btn_int[]=new Button[10]; //定义数字按钮0到9
 Button btn_dian,btn_fu,btn_jia,btn_jian,btn_cheng,btn_chu,btn_pow,btn_sqrt,btn_deng,btn_C;
 //定义按钮“.”,“+/-”,“+” ,“ - ”,“ * ”, “ /”,“ ^ ”,“sqrc”,“ = ”,“C”。
 public void init()
 {
  this.setBackground(Color.lightGray); //设置Applet组件的背景色
  
  setLayout(null);
  tfAnswer=new JTextField();
  tfAnswer.setBounds(5, 5, 220, 30);//定义文本框组件的位置(x,y)和大小(宽width,高度height)
  tfAnswer.setHorizontalAlignment(JTextField.RIGHT); //定义文本框中文本字段的对齐方式
  tfAnswer.setText("0.");
  add(tfAnswer);
  
  //布局开始

  buju_btn(btn_int[7],"7",5,50,40,30,Color.blue);buju_btn(btn_int[8],"8",50,50,40,30,Color.blue);buju_btn(btn_int[9],"9",95,50,40,30,Color.blue);buju_btn(btn_chu,"/",140,50,40,30,Color.red);buju_btn(btn_C,"C",185,50,40,30,Color.red);
  buju_btn(btn_int[4],"4",5,85,40,30,Color.blue);buju_btn(btn_int[5],"5",50,85,40,30,Color.blue);buju_btn(btn_int[6],"6",95,85,40,30,Color.blue);buju_btn(btn_cheng,"*",140,85,40,30,Color.red);buju_btn(btn_pow,"^",185,85,40,30,Color.blue);
  buju_btn(btn_int[1],"1",5,120,40,30,Color.blue);buju_btn(btn_int[2],"2",50,120,40,30,Color.blue);buju_btn(btn_int[3],"3",95,120,40,30,Color.blue);buju_btn(btn_jian,"-",140,120,40,30,Color.red);buju_btn(btn_sqrt,"sqrt",185,120,40,30,Color.blue);
  buju_btn(btn_int[0],"0",5,155,40,30,Color.blue);buju_btn(btn_fu,"+/-",50,155,40,30,Color.blue);buju_btn(btn_dian,".",95,155,40,30,Color.blue);buju_btn(btn_jia,"+",140,155,40,30,Color.red);buju_btn(btn_deng,"=",185,155,40,30,Color.red);
 }

//布局结束

 

//布局方法开始

 public void buju_btn(Button btn_name,String btn_fuhao,int x,int y,int width,int height,Color btn_color)
 {
  setLayout(null);
  btn_name=new Button(btn_fuhao);
  btn_name.setBounds(x,y,width,height);
  btn_name.setBackground(Color.white);
     btn_name.setForeground(btn_color);
     btn_name.addActionListener(this);
     add(btn_name);
 }
 

//布局方法结束


 public void dengyu(String z)
 {
  if(z.equals("+")) answer=Double.parseDouble(x)+Double.parseDouble(y);
  if(z.equals("-")) answer=Double.parseDouble(x)-Double.parseDouble(y);
  if(z.equals("*")) answer=Double.parseDouble(x)*Double.parseDouble(y);
  if(z.equals("/")) answer=Double.parseDouble(x)/Double.parseDouble(y);
  if(z.equals("^")) answer=Math.pow(Double.parseDouble(x),Double.parseDouble(y));
  x=Double.toString(answer);
  tfAnswer.setText(x);
  y="";fh="";
 }
 
 public void actionPerformed(ActionEvent e) throws IndexOutOfBoundsException
 {
  if (e.getActionCommand().equals("0")||
    e.getActionCommand().equals("1")||
    e.getActionCommand().equals("2")||
    e.getActionCommand().equals("3")||
    e.getActionCommand().equals("4")||
    e.getActionCommand().equals("5")||
    e.getActionCommand().equals("6")||
    e.getActionCommand().equals("7")||
    e.getActionCommand().equals("8")||
    e.getActionCommand().equals("9"))
    {
     if (fh.equals(""))
     {
      x=x+e.getActionCommand();
         if (x.startsWith("00"))
         {
          x=x.substring(1);
         }
               tfAnswer.setText(x);
           }
     else
     {
      y=y+e.getActionCommand();
      if (y.startsWith("00"))
         {
          y=y.substring(1);
         }
      tfAnswer.setText(y);
     }
    }
      
  if (e.getActionCommand().equals("."))
  {
   if (fh.equals(""))
   {
    int i=0,j=0;
    for (i=0;i<x.length();i++)
     if (x.charAt(i)=='.')
      j++;
    if (j==0)
     x=x+".";
    tfAnswer.setText(x);
   }
   else
   {
    int i=0,j=0;
    for (i=0;i<y.length();i++)
     if (y.charAt(i)=='.')
      j++;
    if (j==0)
     y=y+".";
    tfAnswer.setText(y);
   }
  }
  
  if (e.getActionCommand().equals("C"))
    {
   x="";y="";fh="";
   tfAnswer.setText("0.");
    }
  
  if (e.getActionCommand().equals("+/-"))
  {
   if (fh.equals(""))
   {
    if(x.substring(0,1).equals("-"))
     x=x.substring(1);
    else
     x="-"+x;
    tfAnswer.setText(x);
   }
   else
   {
    if(y.substring(0,1).equals("-"))
     y=y.substring(1);
    else
     y="-"+y;
    tfAnswer.setText(y);
   }
  }
  
  if(e.getActionCommand().equals("sqrt"))
  {
   if(fh!="") dengyu(fh);
   answer=Math.sqrt(Double.parseDouble(x));
   x=Double.toString(answer);
   tfAnswer.setText(x);
  }
  
  if(e.getActionCommand().equals("+"))
  {
   if(fh!="") dengyu(fh);
   fh="+";
  }
  if(e.getActionCommand().equals("-"))
  {
   if(fh!="") dengyu(fh);
   fh="-";
  }
  if(e.getActionCommand().equals("*"))
  {
   if(fh!="") dengyu(fh);
   fh="*";
  }
  if(e.getActionCommand().equals("/"))
  {
   if(fh!="") dengyu(fh);
   fh="/";
  }
  if(e.getActionCommand().equals("^"))
  {
   if(fh!="") dengyu(fh);
   fh="^";
  }
  
  if(e.getActionCommand().equals("=")) dengyu(fh);
 }
}

⌨️ 快捷键说明

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