java简单计算器,只能鼠标点击数字

包含三个类

1.主类,包括计算器界面和每个键的功能实现calculator.java

package com.bj.jsq;



import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.Dimension;

import java.awt.TextField;

import java.awt.Label;

import java.awt.Button;

import java.awt.Toolkit;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.Color;

import java.awt.SystemColor;

import javax.swing.JMenuBar;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;



public class calculator extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel contentPane;

    public static double n; 

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {

                    calculator frame = new calculator(); 

                    frame.setVisible(true);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        });

    }



    /**

     * Create the frame.

     */

    public calculator() {

        setResizable(false);

        setBackground(SystemColor.inactiveCaptionBorder);  //

        setTitle("\u7B80\u5355\u8BA1\u7B97\u5668");

        Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();

        int x=(int)screen.getWidth();

        int y=(int)screen.getHeight();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setBounds((x-293)/2, (y-333)/2, 293, 315);

        contentPane = new JPanel();

        setContentPane(contentPane);

        contentPane.setLayout(null);

        final TextField txtInput = new TextField();

        

        txtInput.setBounds(62, 10, 205, 23);

        contentPane.add(txtInput);

        final TextField txtOutput = new TextField();

        txtOutput.setEditable(false);

        txtOutput.setBounds(62, 49, 205, 23);

        contentPane.add(txtOutput);

        

        Label label = new Label("input");                 //

        //label.setBackground(UIManager.getColor("window"));

        label.setForeground(new Color(153, 50, 204));

        label.setBounds(15, 10, 41, 23);

        contentPane.add(label);

        

        Label label_1 = new Label("result");             //

        label_1.setForeground(new Color(153, 50, 204));

        label_1.setBounds(15, 49, 41, 23);

        contentPane.add(label_1);

        

        Button btn7 = new Button("7");                  //

        btn7.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"7");

            }

        });

        btn7.setBounds(20, 92, 49, 23);

        contentPane.add(btn7);

        

        Button btn8 = new Button("8");                  ////

        btn8.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"8");

            }

        });

        btn8.setBounds(79, 92, 49, 23);

        contentPane.add(btn8);

        

        Button button_2 = new Button("+");

        button_2.setForeground(new Color(153, 50, 204));

        button_2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+" + ");

                

                

            }

        });

        button_2.setBounds(218, 92, 49, 23);

        contentPane.add(button_2);

        

        Button btn9 = new Button("9");

        btn9.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"9");

            }

        });

        btn9.setBounds(147, 92, 49, 23);

        contentPane.add(btn9);

        

        Button btn5 = new Button("5");

        btn5.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"5");

            }

        });

        btn5.setBounds(79, 136, 49, 23);

        contentPane.add(btn5);

        

        Button btn4 = new Button("4");

        btn4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"4");

            }

        });

        btn4.setBounds(20, 136, 49, 23);

        contentPane.add(btn4);

        

        Button button_6 = new Button("-");

        button_6.setForeground(new Color(153, 50, 204));

        button_6.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+" - ");                

            }

        });

        button_6.setBounds(218, 136, 49, 23);

        contentPane.add(button_6);

        

        Button btn6 = new Button("6");

        btn6.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"6");

            }

        });

        btn6.setBounds(147, 136, 49, 23);

        contentPane.add(btn6);

        

        Button btn2 = new Button("2");

        btn2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"2");

            }

        });

        btn2.setBounds(79, 183, 49, 23);

        contentPane.add(btn2);

        

        Button btn1 = new Button("1");

        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"1");

            }

        });

        btn1.setBounds(20, 183, 49, 23);

        contentPane.add(btn1);

        

        Button button_10 = new Button("cls");

        button_10.setForeground(new Color(153, 50, 204));

        button_10.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText("");

                txtOutput.setText("");

            }

        });

        button_10.setBounds(218, 183, 49, 23);

        contentPane.add(button_10);

        

        Button btn3 = new Button("3");

        btn3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"3");

            }

        });

        btn3.setBounds(147, 183, 49, 23);

        contentPane.add(btn3);

        

        Button btndel = new Button(".");

        btndel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+".");

            }

        });

        btndel.setBounds(79, 230, 49, 23);

        contentPane.add(btndel);

        

        Button btn0 = new Button("0");

        btn0.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                txtInput.setText(txtInput.getText()+"0");

            }

        });

        btn0.setBounds(20, 230, 49, 23);

        contentPane.add(btn0);

    

        Button btne = new Button("=");  //

        btne.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                String str=txtInput.getText();

                String[]ss;

                String regex1="( \\p{Punct} )\\1++";//有若干个标点符号重叠的正则式

                String regex2="( \\p{Punct} )$";//标点符号在行末的正则表达式

                str=str.replaceAll(regex1, "$1");

                str=str.replaceAll(regex2, "");

                int i;

                ss=str.split(" ");

                try{

                n=Double.parseDouble(ss[0]);

                for(i=0;i<ss.length;i++)

                {

                    if(ss[i].equals("+"))

                    {

                        n=n+Double.parseDouble(ss[i+1]);

                    }

                    if(ss[i].equals("-"))

                    {

                        n=n-Double.parseDouble(ss[i+1]);

                    }

                }

                txtOutput.setText(String.valueOf(n));

                }catch(Exception ex){

                    JOptionPane.showMessageDialog(null, "请检查 输入数的格式!!重新输入");

                }

            }

        });

        btne.setForeground(new Color(153, 50, 204));

        btne.setBounds(147, 230, 49, 23);

        contentPane.add(btne);

        

        Button btnSqrt = new Button("sqrt");

        btnSqrt.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                String sqrtValue;

                if(txtInput.getText().contains("+")||txtInput.getText().contains("+"))

                    sqrtValue=txtOutput.getText();

                else

                    sqrtValue=txtInput.getText();

                try{

                txtOutput.setText(String.valueOf(Math.sqrt(Double.parseDouble(sqrtValue))));

                }catch(Exception ex){

                    JOptionPane.showMessageDialog(null, "该数不能开方,请检查数据是否错误!");

                }

                }

        });

        btnSqrt.setForeground(new Color(148, 0, 211));

        btnSqrt.setBounds(218, 230, 49, 23);

        contentPane.add(btnSqrt);

        

        JMenuBar menuBar = new JMenuBar();

        menuBar.setBounds(0,267,281, 21);

        contentPane.add(menuBar);

        

        JMenu mnNewMenu = new JMenu("帮助");//\u67E5\u770B

        menuBar.add(mnNewMenu);

        

        JMenuItem mntmh = new JMenuItem("帮助");//\u5E2E\u52A9

        mntmh.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                calHelp ch=new calHelp();

                //ch.show(false);

                ch.setVisible(true);

            }

        });

        mnNewMenu.add(mntmh);

        

        JMenuItem mntmNewMenuItem = new JMenuItem("关于计算器");

        mntmNewMenuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                calAbout ca=new calAbout();

                ca.setVisible(true);

            }

        });

        mnNewMenu.add(mntmNewMenuItem);

        setVisible(true);

    }

}
View Code

2.帮助类,只是一个窗体,显示一些帮助内容calHelp.java

package com.bj.jsq;



import java.awt.EventQueue;



import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JScrollPane;



public class calHelp extends JFrame {



    /**

     * Launch the application.

     */

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {

                    calHelp frame = new calHelp();

                    frame.setVisible(true);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        });

    }



    /**

     * Create the frame.

     */

    public calHelp() {

        setResizable(false);

        setTitle("\u5E2E\u52A9");

        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        setBounds(100, 100, 293, 372);

        getContentPane().setLayout(null);

        

        JTextArea textArea = new JTextArea();

        textArea.setEditable(false);

        textArea.setText("1.\u529F\u80FD\u4ECB\u7ECD\r\n      (1)\u6570\u5B57\u7684\u52A0\u51CF\uFF1B\u65E0\u4E58\u9664\u529F\u80FD\r\n      (2)\u6570\u5B57\u5F00\u65B9\uFF0C\u4E0D\u8FC7\u6C42\u5F00\u65B9\u7684\u65F6\u5019\u8F93\u5165\u680F\u6700\u597D\u4E0D\u8981\u5B58\u5728\u52A0\u51CF\u7B26\u53F7\uFF0C\u5426\u5219\u4F1A\u63D0\u793A\u4F60\u6570\u5B57\u6837\u5F0F\u9519\u8BEF\r\n      (3)\u6E05\u7A7A\uFF0Ccls\u952E\u53EF\u4EE5\u6E05\u7A7A\u8F93\u5165\u680F\u548C\u7ED3\u679C\u680F\u7684\u6587\u5B57\r\n\r\n2.\u6CE8\u610F\u4E8B\u9879\r\n      (1)\u4E0D\u8981\u7528\u952E\u76D8\u8F93\u5165\u9664\u4E86\u52A0\u51CF\u53F7\u548C\u5C0F\u6570\u70B9\u3001\u6570\u5B57\u5916\u5176\u4ED6\u7684\u5B57\u7B26\u6216\u8005\u6C49\u5B57\t\r\n      (2)\u5982\u679C\u8F93\u5165\u4E86\u91CD\u590D\u7684\u7B26\u53F7\u6216\u8005\u5728\u672B\u5C3E\u51FA\u73B0\u4E86\u7B26\u53F7\uFF0C\u6B64\u8BA1\u7B97\u5668\u80FD\u591F\u6B63\u786E\u5904\u7406\uFF0C\u4F46\u4E5F\u4E0D\u8981\u4E71\r\n\u8F93\u5165\uFF0C\u5F00\u53D1\u8005\u4E5F\u6CA1\u6709\u5168\u9762\u5730\u8003\u8651\r\n      (3)\u7B2C\u4E00\u4E2A\u6570\u5B57\u4E3A\u8D1F\u6570\u7684\u60C5\u51B5\u5F00\u53D1\u8005\u6CA1\u6709\u60F3\u5230\u89E3\u51B3\u65B9\u6848\uFF0C\u6240\u4EE5\u8BF7\u4F7F\u7528\u8005\u5C3D\u91CF\u5C06\u6B63\u6570\u5199\u5728\u7B2C\u4E00\u4E2A\u4F4D\u7F6E\u5427");

        textArea.setBounds(0, 0, 258, 373);

        textArea.setLineWrap(true);

        getContentPane().add(textArea);

        

        JScrollPane scrollPane = new JScrollPane(textArea);

        scrollPane.setBounds(0, 0, 277, 324);

        getContentPane().add(scrollPane);

    }

}
View Code

3.关于计算器

package com.bj.jsq;



import java.awt.EventQueue;



import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.JCheckBox;



public class calAbout extends JFrame {

    /**

     * 

     */

    private static final long serialVersionUID = 1L;

    private JPanel contentPane;



    /**

     * Launch the application.

     */

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {

                    calAbout frame = new calAbout();

                    frame.setVisible(true);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        });

    }



    /**

     * Create the frame.

     */

    public calAbout() {

        setResizable(false);

        setTitle("\u5173\u4E8E\u8BA1\u7B97\u5668");

        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        setBounds(100, 100, 259, 314);

        setVisible(true);

        contentPane = new JPanel();

        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);

        contentPane.setLayout(null);



        JScrollPane scrollPane = new JScrollPane();

        scrollPane.setBounds(5, 5, 233, 266);

        contentPane.add(scrollPane);



        final JTextArea txtrauthor = new JTextArea();

        txtrauthor.setEditable(false);

        txtrauthor

                .setText("\u5173\u4E8E\u8BA1\u7B97\u5668\uFF1A\r\n   @author\t\u5305\u622C\r\n   @version 1.0\r\n   @since 2014\r\n   \u6B64\u8BA1\u7B97\u5668\u53EA\u7528\u4E8E\u4E2A\u4EBA\u6B23\u8D4F\u53CA\u5A31\u4E50\uFF0C\u4E0D\u5F97\u53C2\u4E0E\u5546\u4E1A\u7528\u9014\uFF0C\u5982\u9700\u8F6C\u8F7D\uFF0C\u8BF7\u6CE8\u660E\u51FA\u5904\uFF01\u8C22\u8C22");

        scrollPane.setViewportView(txtrauthor);



        final JCheckBox checkBox = new JCheckBox("\u81EA\u52A8\u6362\u884C");

        checkBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                if (checkBox.isSelected()) {

                    txtrauthor.setLineWrap(true);

                }

                else

                    txtrauthor.setLineWrap(true);

            }

        });

        scrollPane.setColumnHeaderView(checkBox);

    }



}
View Code

你可能感兴趣的:(java)