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

📄 main.java

📁 JAVA做的比较简单的计算器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * jisuanqi.java
 *
 * Created on 2007年11月19日, 下午11:12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jisuanqi;import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
class MyFrame extends Frame implements ActionListener
{
    private double temp=0.0;                             //temp 寄存器
    private String sysMR="0";                             //sysMR
    private String MR="0.";                                //MR
    private boolean f=false,y=false,d=false;          //标记有没有运算符作用,false为没有运算符,y为刚刚有运算符 ,d为是不是0.n
    int flag=0;                                          //标记是哪种运算,+=1,*=2,/=3,-=4;
    MenuBar menubar;
    Menu menu;
    MenuItem item1;
    MenuItem item2;
    TextField text,text2;
    MyWindowListener listener=new MyWindowListener();  // 这里对象一定要实例化,监视器是一个对象
    MyFrame()
    {
        setLayout(null);
        addWindowListener(listener);
        setResizable(false);
        setBounds(50,50,343,255);
        setVisible(true);
        //End frame
        item1=new MenuItem("复制");
        item1.addActionListener(this);
        item1.setShortcut(new MenuShortcut(KeyEvent.VK_C));
        item2=new MenuItem("粘贴");
        item2.setShortcut(new MenuShortcut(KeyEvent.VK_V));
        item2.addActionListener(this);
        menu=new Menu("编辑");
        menu.add(item1);
        menu.add(item2);
        menubar=new MenuBar();
        menubar.add(menu);
        setMenuBar(menubar);
        //End menuBar
        text=new TextField("0.",35);        
        text.setBounds(20,50,303,20);
        add(text);
        //End textField
        Panel panel0=new Panel();
        panel0.setLayout(null);
        panel0.setBounds(23,75,300,30);     
        text2=new TextField(3);
        text2.setBounds(0,2,32,26); 
        panel0.add(text2);        
        Button button1=new Button("Backspace");button1.addActionListener(this);
        Button button2=new Button("CE");button2.addActionListener(this);
        Button button3=new Button("C");button3.addActionListener(this);
        button1.setBounds(52,2,80,26);
        button2.setBounds(146,2,70,26);
        button3.setBounds(230,2,70,26);
        panel0.add(button1);
        panel0.add(button2);
        panel0.add(button3);
        add(panel0);
        //End panel0
        MyPanel panel1,panel2,panel3,panel4;
        panel1=new MyPanel();
        panel1.button1.setLabel("MC");panel1.button1.addActionListener(this);
        panel1.button2.setLabel("7");panel1.button2.addActionListener(this);
        panel1.button3.setLabel("8");panel1.button3.addActionListener(this);
        panel1.button4.setLabel("9");panel1.button4.addActionListener(this);
        panel1.button5.setLabel("/");panel1.button5.addActionListener(this);
        panel1.button6.setLabel("sqrt");panel1.button6.addActionListener(this);
        panel2=new MyPanel();
        panel2.button1.setLabel("MR");panel2.button1.addActionListener(this);
        panel2.button2.setLabel("4");panel2.button2.addActionListener(this);
        panel2.button3.setLabel("5");panel2.button3.addActionListener(this);
        panel2.button4.setLabel("6");panel2.button4.addActionListener(this);
        panel2.button5.setLabel("*");panel2.button5.addActionListener(this);
        panel2.button6.setLabel("%");panel2.button6.addActionListener(this);
        panel3=new MyPanel();
        panel3.button1.setLabel("MS");panel3.button1.addActionListener(this);
        panel3.button2.setLabel("1");panel3.button2.addActionListener(this);
        panel3.button3.setLabel("2");panel3.button3.addActionListener(this);
        panel3.button4.setLabel("3");panel3.button4.addActionListener(this);
        panel3.button5.setLabel("-");panel3.button5.addActionListener(this);
        panel3.button6.setLabel("1/x");panel3.button6.addActionListener(this);
        panel4=new MyPanel();
        panel4.button1.setLabel("M+");panel4.button1.addActionListener(this);
        panel4.button2.setLabel("0");panel4.button2.addActionListener(this);
        panel4.button3.setLabel("+/-");panel4.button3.addActionListener(this);
        panel4.button4.setLabel(".");panel4.button4.addActionListener(this);
        panel4.button5.setLabel("+");panel4.button5.addActionListener(this);
        panel4.button6.setLabel("=");panel4.button6.addActionListener(this);
        add(panel1);add(panel2);add(panel3);add(panel4);
        panel1.setLocation(20,110);
        panel2.setLocation(20,145);
        panel3.setLocation(20,180);
        panel4.setLocation(20,215);
        validate();
    }
    public void actionPerformed(ActionEvent e) 
    {   
        int l=e.getSource().toString().length();
        if(e.getSource()==item1)
        {
            sysMR=text.getText().toString();
            text.setText(sysMR);
        }
        if(e.getSource()==item2)
        {
            text.setText(sysMR);
        }
        if(e.getSource().toString().charAt(l-2)=='1')
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("1");
                else
                    text.setText(text.getText().concat("1"));
            }
            else
            {
                if(y==true)
                {
                    text.setText("1");
                    y=false;
                }
                else
                    text.setText(text.getText().concat("1"));
            }

        }
        if(e.getSource().toString().charAt(l-2)=='2')
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("2");
                else
                    text.setText(text.getText().concat("2"));
            }
            else
            {
                if(y==true)
                {
                    text.setText("2");
                    y=false;
                }
                else
                    text.setText(text.getText().concat("2"));
            }
        }
        if(e.getSource().toString().charAt(l-2)=='3')
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("3");
                else
                    text.setText(text.getText().concat("3"));
            }
            else
            {
                if(y==true)
                {
                    text.setText("3");
                    y=false;
                }
                else
                    text.setText(text.getText().concat("3"));
            }
        }
        if(e.getSource().toString().charAt(l-2)=='4')
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("4");
                else
                    text.setText(text.getText().concat("4"));
            }
            else
            {
                if(y==true)
                {
                    text.setText("4");
                    y=false;
                }
                else
                    text.setText(text.getText().concat("4"));
            }
        }
        if(e.getSource().toString().charAt(l-2)=='5')
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("5");
                else
                    text.setText(text.getText().concat("5"));
            }
            else
            {
                if(y==true)
                {
                    text.setText("5");
                    y=false;
                }
                else
                    text.setText(text.getText().concat("5"));
            }
        }
        if(e.getSource().toString().charAt(l-2)=='6')
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("6");
                else
                    text.setText(text.getText().concat("6"));
            }
            else
            {
                if(y==true)
                {
                    text.setText("6");
                    y=false;
                }
                else
                    text.setText(text.getText().concat("6"));
            }
        }
        if(e.getSource().toString().charAt(l-2)=='7')
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("7");
                else
                    text.setText(text.getText().concat("7"));
            }
            else
            {
                if(y==true)
                {
                    text.setText("7");
                    y=false;
                }
                else
                    text.setText(text.getText().concat("7"));
            }
        }
        if(e.getSource().toString().charAt(l-2)=='8')
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("8");
                else
                    text.setText(text.getText().concat("8"));
            }
            else
            {
                if(y==true)
                {
                    text.setText("8");
                    y=false;
                }
                else
                    text.setText(text.getText().concat("8"));
            }
        }
        if(e.getSource().toString().charAt(l-2)=='9')
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("9");
                else
                    text.setText(text.getText().concat("9"));
            }
            else
            {
                if(y==true)
                {
                    text.setText("9");
                    y=false;
                }
                else
                    text.setText(text.getText().concat("9"));
            }
        }
        if(e.getSource().toString().charAt(l-2)=='0')    //temporay ok~~
        {
            if(f==false)
            {
                if(text.getText().equals("0.")==true && d==false) text.setText("0.");
                else
                    text.setText(text.getText().concat("0"));
            }
            else
            {
                if(y)
                {
                    text.setText("0.");
                    y=false;
                }    
                else
                    text.setText(text.getText().concat("0"));
            }            
        }
        if(e.getSource().toString().charAt(l-2)=='E'           //   CE....C...%...MC
          || e.getSource().toString().charAt(l-2)=='C' 

⌨️ 快捷键说明

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