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

📄 mainframe.java~107~

📁 计算器
💻 JAVA~107~
📖 第 1 页 / 共 2 页
字号:
package calculator;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;

/**
 * <p>Title: Calculator</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author 王刚亮
 * @version 1.0
 */
public class MainFrame extends JFrame {
    public JDesktopPane desktop = new JDesktopPane();
    JPanel contentPane;
    JPanel panel = new JPanel();
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenu1 = new JMenu();
    JMenuItem jMenuItem1 = new JMenuItem();
    JMenuItem jMenuItem2 = new JMenuItem();
    JMenu jMenu3 = new JMenu();
    JMenuItem jMenuItem3 = new JMenuItem();
    JMenuItem jMenuItem4 = new JMenuItem();
    JTextField jTextField1 = new JTextField();
    XYLayout xYLayout1 = new XYLayout();
    JPanel jPanel1 = new JPanel();
    GridLayout gridLayout1 = new GridLayout(5,4,5,5);
    double temp = 0.0;
    int key = 0;
    String copy;
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JButton jButton3 = new JButton();
    JButton jButton4 = new JButton();
    JButton jButton5 = new JButton();
    JButton jButton6 = new JButton();
    JButton jButton7 = new JButton();
    JButton jButton8 = new JButton();
    JButton jButton9 = new JButton();
    JButton jButton10 = new JButton();
    JButton jButton11 = new JButton();
    JButton jButton12 = new JButton();
    JButton jButton13 = new JButton();
    JButton jButton14 = new JButton();
    JButton jButton15 = new JButton();
    JButton jButton16 = new JButton();
    JButton jButton17 = new JButton();
    JButton jButton18 = new JButton();
    JButton jButton19 = new JButton();
    JButton jButton20 = new JButton();
    JPanel jPanel2 = new JPanel();
    GridLayout gridLayout2 = new GridLayout(1,3,5,5);
    JButton jButton21 = new JButton();
    JButton jButton22 = new JButton();
    JButton jButton23 = new JButton();
    javax.swing.JButton jButton24 = new JButton();
    javax.swing.JButton jButton25 = new JButton();
    javax.swing.JButton jButton26 = new JButton();
    javax.swing.JButton jButton27 = new JButton();
    javax.swing.JButton jButton28 = new JButton();
    public MainFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(xYLayout1);
        this.setJMenuBar(jMenuBar1);
        this.setResizable(false);
        setSize(new Dimension(350, 280));
        setTitle("Calculator");
        jMenu1.setFont(new java.awt.Font("Dialog", 0, 13));
        jMenu1.setText("编辑(E)");
        jMenuItem1.setFont(new java.awt.Font("Dialog", 0, 13));
        jMenuItem1.setText("复制(C) Ctrl+C");
        jMenuItem1.addActionListener(new MainFrame_jMenuItem1_actionAdapter(this));
        jMenuItem2.setFont(new java.awt.Font("Dialog", 0, 13));
        jMenuItem2.setText("粘贴(E) Ctrl+E");
        jMenuItem2.addActionListener(new MainFrame_jMenuItem2_actionAdapter(this));
        jMenu3.setFont(new java.awt.Font("Dialog", 0, 13));
        jMenu3.setText("帮助(H)");
        jMenuItem3.setFont(new java.awt.Font("Dialog", 0, 13));
        jMenuItem3.setText("帮助主题(H)");
        jMenuItem3.addActionListener(new MainFrame_jMenuItem3_actionAdapter(this));
        jMenuItem4.setFont(new java.awt.Font("Dialog", 0, 13));
        jMenuItem4.setText("关于计算器(A)");
        jMenuItem4.addActionListener(new MainFrame_jMenuItem4_actionAdapter(this));
        jTextField1.setFont(new java.awt.Font("Dialog", 0, 12));
        jTextField1.setEditable(false);
        jTextField1.setText("");
        jTextField1.setHorizontalAlignment(SwingConstants.RIGHT);
        jPanel1.setLayout(gridLayout1);
        gridLayout1.setColumns(6);
        gridLayout1.setRows(4);
        jButton1.setForeground(Color.blue);
        jButton1.setText("7");
        jButton1.addActionListener(new MainFrame_jButton1_actionAdapter(this));
        jButton2.setForeground(Color.red);
        jButton2.setText("=");
        jButton2.addActionListener(new MainFrame_jButton2_actionAdapter(this));
        jButton3.setForeground(Color.red);
        jButton3.setText("+");
        jButton3.addActionListener(new MainFrame_jButton3_actionAdapter(this));
        jButton4.setForeground(Color.blue);
        jButton4.setText(".");
        jButton4.addActionListener(new MainFrame_jButton4_actionAdapter(this));
        jButton5.setForeground(Color.blue);
        jButton5.setText("+/-");
        jButton5.addActionListener(new MainFrame_jButton5_actionAdapter(this));
        jButton6.setForeground(Color.blue);
        jButton6.setText("0");
        jButton6.addActionListener(new MainFrame_jButton6_actionAdapter(this));
        jButton7.setForeground(Color.blue);
        jButton7.setText("1/x");
        jButton7.addActionListener(new MainFrame_jButton7_actionAdapter(this));
        jButton8.setForeground(Color.red);
        jButton8.setText("-");
        jButton8.addActionListener(new MainFrame_jButton8_actionAdapter(this));
        jButton9.setForeground(Color.red);
        jButton9.setText("*");
        jButton9.addActionListener(new MainFrame_jButton9_actionAdapter(this));
        jButton10.setForeground(Color.blue);
        jButton10.setText("3");
        jButton10.addActionListener(new MainFrame_jButton10_actionAdapter(this));
        jButton11.setForeground(Color.blue);
        jButton11.setText("2");
        jButton11.addActionListener(new MainFrame_jButton11_actionAdapter(this));
        jButton12.setForeground(Color.blue);
        jButton12.setText("1");
        jButton12.addActionListener(new MainFrame_jButton12_actionAdapter(this));
        jButton13.setForeground(Color.blue);
        jButton13.setText("Π");
        jButton13.addActionListener(new MainFrame_jButton13_actionAdapter(this));
        jButton14.setForeground(Color.blue);
        jButton14.setText("6");
        jButton14.addActionListener(new MainFrame_jButton14_actionAdapter(this));
        jButton15.setForeground(Color.blue);
        jButton15.setText("5");
        jButton15.addActionListener(new MainFrame_jButton15_actionAdapter(this));
        jButton16.setForeground(Color.blue);
        jButton16.setText("4");
        jButton16.addActionListener(new MainFrame_jButton16_actionAdapter(this));
        jButton17.setForeground(Color.blue);
        jButton17.setText("sqrt");
        jButton17.addActionListener(new MainFrame_jButton17_actionAdapter(this));
        jButton18.setForeground(Color.red);
        jButton18.setText("/");
        jButton18.addActionListener(new MainFrame_jButton18_actionAdapter(this));
        jButton19.setForeground(Color.blue);
        jButton19.setText("9");
        jButton19.addActionListener(new MainFrame_jButton19_actionAdapter(this));
        jButton20.setForeground(Color.blue);
        jButton20.setText("8");
        jButton20.addActionListener(new MainFrame_jButton20_actionAdapter(this));
        jPanel2.setLayout(gridLayout2);
        gridLayout2.setColumns(3);
        jButton21.setForeground(Color.red);
        jButton21.setHorizontalAlignment(SwingConstants.LEFT);
        jButton21.setHorizontalTextPosition(SwingConstants.CENTER);
        jButton21.setText("Backspace");
        jButton21.addActionListener(new MainFrame_jButton21_actionAdapter(this));
        jButton22.setForeground(Color.red);
        jButton22.setText("C");
        jButton22.addActionListener(new MainFrame_jButton22_actionAdapter(this));
        jButton23.setForeground(Color.red);
        jButton23.setText("CE");
        jButton23.addActionListener(new MainFrame_jButton23_actionAdapter(this));
        contentPane.setMaximumSize(new Dimension(293, 186));
        jButton24.setForeground(Color.magenta);
        jButton24.setText("tan");
        jButton24.addActionListener(new MainFrame_jButton24_actionAdapter(this));
        jButton25.setForeground(Color.magenta);
        jButton25.setText("cos");
        jButton25.addActionListener(new MainFrame_jButton25_actionAdapter(this));
        jButton26.setForeground(Color.magenta);
        jButton26.setText("n!");
        jButton26.addActionListener(new MainFrame_jButton26_actionAdapter(this));
        jButton27.setForeground(Color.magenta);
        jButton27.setText("sin");
        jButton27.addActionListener(new MainFrame_jButton27_actionAdapter(this));
        jButton28.setEnabled(false);
        jButton28.setVerifyInputWhenFocusTarget(false);
        jMenuBar1.add(jMenu1);
        jMenuBar1.add(jMenu3);
        jMenu1.add(jMenuItem1);
        jMenu1.add(jMenuItem2);
        jMenu3.add(jMenuItem3);
        jMenu3.add(jMenuItem4);
        jPanel1.add(jButton27);
        jPanel1.add(jButton1);
        jPanel1.add(jButton20);
        jPanel1.add(jButton19);
        jPanel1.add(jButton18);
        jPanel1.add(jButton17);
        jPanel1.add(jButton25);
        jPanel1.add(jButton16);
        jPanel1.add(jButton15);
        jPanel1.add(jButton14);
        jPanel1.add(jButton9);
        jPanel1.add(jButton13);
        jPanel1.add(jButton24);
        jPanel1.add(jButton12);
        jPanel1.add(jButton11);
        jPanel1.add(jButton10);
        jPanel1.add(jButton8);
        jPanel1.add(jButton7);
        jPanel1.add(jButton26);
        jPanel1.add(jButton6);
        jPanel1.add(jButton5);
        jPanel1.add(jButton4);
        jPanel1.add(jButton3);
        jPanel1.add(jButton2);
        jPanel2.add(jButton21);
        jPanel2.add(jButton23);
        jPanel2.add(jButton22);
        contentPane.add(jTextField1, new XYConstraints(10, 0, 277, -1));
        contentPane.add(jPanel1, new XYConstraints(11, 67, 331, 119));
        contentPane.add(jPanel2, new XYConstraints(68, 34, 274, -1));
        contentPane.add(jButton28, new XYConstraints(11, 35, 50, 24));
    }

    public void jButton1_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(7));
    }

    public void jButton20_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(8));
    }

    public void jButton19_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(9));
    }

    public void jButton16_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(4));
    }

    public void jButton15_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(5));
    }

    public void jButton14_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(6));
    }

    public void jButton12_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(1));
    }

    public void jButton11_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(2));
    }

    public void jButton10_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(3));
    }

    public void jButton6_actionPerformed(ActionEvent e) {
        jTextField1.setText(jTextField1.getText()+Integer.toString(0));
    }

    public void jButton4_actionPerformed(ActionEvent e) {
        boolean isDot = false;
        if (jTextField1.getText().length() == 0)
          isDot = true;
        for (int i = 0; i < jTextField1.getText().length(); i++)
        {
            if ('.' == jTextField1.getText().charAt(i)) {
                isDot = true;
                break;
            }
        }
        if (isDot == false)
          jTextField1.setText(jTextField1.getText() + ".");
    }

    public void jButton18_actionPerformed(ActionEvent e) {

            if (Double.parseDouble(jTextField1.getText()) == 0) {
                jTextField1.setText("除数不能为零");
            } else
                temp = Double.parseDouble(jTextField1.getText());
                jTextField1.setText("");
            key = 1;
        }

    public void jButton9_actionPerformed(ActionEvent e) {
        temp = Double.parseDouble(jTextField1.getText());
        jTextField1.setText("");
      key = 2;

    }

    public void jButton8_actionPerformed(ActionEvent e) {
        temp = Double.parseDouble(jTextField1.getText());
      jTextField1.setText("");
      key = 3;

    }

    public void jButton3_actionPerformed(ActionEvent e) {
        temp = Double.parseDouble(jTextField1.getText());
        jTextField1.setText("");
      key = 4;

    }

    public void jButton2_actionPerformed(ActionEvent e) {
        if( key == 1 )
        {
            jTextField1.setText(String.valueOf(temp/Double.parseDouble(jTextField1.getText())));
        }
        if(key == 2)
        {
            jTextField1.setText(String.valueOf(temp*Double.parseDouble(jTextField1.getText())));
        }
        if(key == 3)
        {
            jTextField1.setText(String.valueOf(temp-Double.parseDouble(jTextField1.getText())));
        }
        if(key == 4)
        {
            jTextField1.setText(String.valueOf(temp+Double.parseDouble(jTextField1.getText())));
        }
    }

    public void jButton17_actionPerformed(ActionEvent e) {
        String s = jTextField1.getText();
        if (s.equalsIgnoreCase("")) {
          jTextField1.setText("操作非法");
        }

        if (s.charAt(0) == '-') {
          jTextField1.setText("负数不能开根号");
        }


        else
          jTextField1.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(jTextField1.getText()))));

    }

    public void jButton13_actionPerformed(ActionEvent e) {
        jTextField1.setText(Double.toString(Math.PI));
    }

    public void jButton22_actionPerformed(ActionEvent e) {
        jTextField1.setText("");
        temp = 0.0;
        key =0;
    }

    public void jButton21_actionPerformed(ActionEvent e) {
        String s = jTextField1.getText();
        jTextField1.setText("");
        for (int i = 0; i < s.length() - 1; i++) {
          char a = s.charAt(i);
          jTextField1.setText(jTextField1.getText() + a);
        }
    }

    public void jButton23_actionPerformed(ActionEvent e) {
        jTextField1.setText("");
    }

    public void jButton7_actionPerformed(ActionEvent e) {
        if (jTextField1.getText().charAt(0) == '0' &&
            jTextField1.getText().length() == 1)
        {
            jTextField1.setText("零不能求倒数");
        }
        else
        {
            String s = Double.toString(1 / Double.parseDouble(jTextField1.getText()));
            jTextField1.setText(s);
        }
    }

    public void jMenuItem1_actionPerformed(ActionEvent e) {
        copy = jTextField1.getText();
    }

    public void jMenuItem2_actionPerformed(ActionEvent e) {
        jTextField1.setText(copy);
    }

    public void jButton5_actionPerformed(ActionEvent e) {
         String s = jTextField1.getText();
         if (s.charAt(0) == '-')
         {
            jTextField1.setText("");
            for (int i = 1; i < s.length(); i++)
            {
                char a = s.charAt(i);
                jTextField1.setText(jTextField1.getText() + a);
            }
         }
         else
            jTextField1.setText('-' + s);
    }

    public void jMenuItem3_actionPerformed(ActionEvent e) {
        try{
            Runtime.getRuntime().exec("cmd /c start D:/Calculator/help.txt");
        }
        catch(Exception err)
        {

⌨️ 快捷键说明

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