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

📄 jisuanqi.java

📁 写了一个简单的计算器
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class jisuanqi extends JFrame {
    private  JTextField textfield;
    private  JButton[] button ;
    private  JCheckBox checkbox;
    int    oper;
    String   stri;
    public  StringBuffer buf;
    public  StringBuffer buf2;
    public  StringBuffer buf3;
    private Container container;
    private  String[] mark = {"Backspace","   CE   ","    C    ","MC","MR","MS","M+"," /"," *"," -"," +","sprt"," %","1/x"," ="};
    private  String[] numarry = {"7","8","9","4","5","6","1","2","3"};
    public jisuanqi(){
       super("Calculator");
       setResizable(false);
       buf = new StringBuffer();
       buf2 = new StringBuffer();
       buf3 = new StringBuffer();
       container = getContentPane();
       container.setLayout(new FlowLayout());
       textfield = new JTextField(28);
       textfield.setText("0.");
       checkbox = new JCheckBox(" ");
       button = new JButton[27];

       for(int i=0;i<3;i++)
       {
          button[i] = new JButton(mark[i]);
       }

       for(int j=3,k=3;j<7;j++,k+=6)
       {

          button[k] = new JButton(mark[j]);
          button[k+4] = new JButton(mark[j+4]);
          button[k+5] = new JButton(mark[j+8]);
       }
       for(int w=4;w<7;w++)
       {
          button[w] = new JButton(numarry[w-4]);
          button[w+6] = new JButton(numarry[w-1]);
          button[w+12] = new JButton(numarry[w+2]);
       }
       button[22] = new JButton("0");
       button[23] = new JButton("+/-");
       button[24] = new JButton(".");

       container.add(textfield);
       container.add(checkbox);

      for(int t=0;t<27;t++)
       {
         container.add(button[t]);
       }

      button[10].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                  buf.append(button[10].getText());
                  textfield.setText(buf.toString());
               }
            });
      button[11].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                  buf.append(button[11].getText());
                  textfield.setText(buf.toString());
               }
            });
      button[12].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                  buf.append(button[12].getText());
                  textfield.setText(buf.toString());
               }
            });
       button[16].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                  buf.append(button[16].getText());
                  textfield.setText(buf.toString());
               }
            });
      button[4].addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                  buf.append(button[4].getText());
                  textfield.setText(buf.toString());
               }
            });
      button[5].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.append(button[5].getText());
                  textfield.setText(buf.toString());
               }
            });
      button[6].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.append(button[6].getText());
                  textfield.setText(buf.toString());
               }
            });
      button[17].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.append(button[17].getText());
                  textfield.setText(buf.toString());
               }
            });
      
     button[18].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.append(button[18].getText());
                  textfield.setText(buf.toString());
               }
            });
     button[22].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.append(button[22].getText());
                  textfield.setText(buf.toString());
               }
            });
    button[24].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.append(button[24].getText());
                  textfield.setText(buf.toString());
               }
            });
    button[0].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.delete(buf.length()-1,buf.length());
                  textfield.setText(buf.toString());
               }
            });
    button[1].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.delete(0,buf.length());
                  textfield.setText(buf.toString());
               }
            });

    button[15].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf3.delete(0,buf.length());
                  buf3.append(textfield.getText());
                  checkbox.setText("M");
               }
            });

    button[21].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  if(buf.length()==0)
                  {
                     double ff = 2.0*(Double.parseDouble(buf3.toString()));
                     buf3.delete(0,buf3.length());
                     buf3.append(ff);
                  }
                  else
                  {
                     double ff1 = 2.0*(Double.parseDouble(buf.toString()));
                     double ff2 = 2.0*(Double.parseDouble(buf3.toString()));
                     double ff3 = ff1 + ff2;
                     buf3.delete(0,buf3.length());
                     buf3.append(ff3);
                  }
               }
            });

    button[9].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.delete(0,buf.length());
                  buf.append(buf3.toString());
                  buf3.delete(0,buf3.length());
               }
            });


    button[3].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf3.delete(0,buf.length());
                  checkbox.setText("");
               }
            });



    button[2].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.delete(0,buf.length());
                  textfield.setText(buf.toString());
               }
            });
    button[0].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf.delete(buf.length()-1,buf.length()-1);
                  textfield.setText(buf.toString());
               }
            });

    button[8].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  double f = Math.sqrt(Double.parseDouble(buf.toString()));
                  if(f>=0.0)
                  {
                    buf.delete(0,buf.length());
                    buf.append(Double.toString(f));
                    textfield.setText(buf.toString());
                   }
                  else
                   textfield.setText("error");
               }
            });


    button[14].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  double f = 100.0*(Double.parseDouble(buf.toString()));
                  if(f>=0.0)
                  {
                    buf.delete(0,buf.length());
                    buf.append(Double.toString(f));
                    buf.append("%");
                    textfield.setText(buf.toString());
                    buf.delete(buf.length()-1,buf.length()-1);
                   }
               }
            });
    button[20].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  double f = Double.parseDouble(buf.toString());
                  if(f!=0.0)
                  {
                    f=(1.0/f);
                    buf.delete(0,buf.length());
                    buf.append(Double.toString(f));
                    textfield.setText(buf.toString());
                   }
                  else
                   textfield.setText("error");
               }
            });
    button[23].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  double f = Double.parseDouble(buf.toString());
                    f=(-1.0*f);
                    buf.delete(0,buf.length());
                    buf.append(Double.toString(f));
                    textfield.setText(buf.toString());
               }
            });



   button[25].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf2.delete(0,buf2.length());
                  textfield.setText(buf2.toString());
                  buf2.append(buf.toString());
                  buf.delete(0,buf.length());
                  oper = 1;
               }
            });
    button[19].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf2.delete(0,buf2.length());
                  textfield.setText(buf2.toString());
                  buf2.append(buf.toString());
                  buf.delete(0,buf.length());
                  oper = 2;
               }
            });
    button[13].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf2.delete(0,buf2.length());
                  textfield.setText(buf2.toString());
                  buf2.append(buf.toString());
                  buf.delete(0,buf.length());
                  oper = 3;
               }
            });
    button[7].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  buf2.delete(0,buf2.length());
                  textfield.setText(buf2.toString());
                  buf2.append(buf.toString());
                  buf.delete(0,buf.length());
                  oper = 4;
               }
            });
    button[26].addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  double f1 = Double.parseDouble(buf.toString());
                  double f2 = Double.parseDouble(buf2.toString());
                  switch(oper)
                  {
                  case 1:  double f3 = f1+f2;
                             buf.delete(0,buf.length());
                             textfield.setText(buf.toString());
                             buf.append(f3);
                             textfield.setText(buf.toString());
                             break;
                  case 2:  double f4 = (-1)*(f1-f2);
                             buf.delete(0,buf.length());
                             textfield.setText(buf.toString());
                             buf.append(f4);
                             textfield.setText(buf.toString());
                             break;

                 case 3:  double f5 = f1*f2;
                             buf.delete(0,buf.length());
                             textfield.setText(buf.toString());
                             buf.append(f5);
                             textfield.setText(buf.toString());
                             break;
                 case 4:  if(f1==0)
                              textfield.setText("除数不能为零“);
                          else
                             double f6 = f2/f1;
                             buf.delete(0,buf.length());
                             textfield.setText(buf.toString());
                             buf.append(f6);
                             textfield.setText(buf.toString());
                             break;
                 }
               }
            });

       pack();
       setSize(325,270);
       setVisible(true);
    }
    public static void main(String[] args) {
        jisuanqi test = new jisuanqi();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

⌨️ 快捷键说明

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