• 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("u7B80u5355u8BA1u7B97u5668");
            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("帮助");//u67E5u770B
            menuBar.add(mnNewMenu);
            
            JMenuItem mntmh = new JMenuItem("帮助");//u5E2Eu52A9
            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("u5E2Eu52A9");
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setBounds(100, 100, 293, 372);
            getContentPane().setLayout(null);
            
            JTextArea textArea = new JTextArea();
            textArea.setEditable(false);
            textArea.setText("1.u529Fu80FDu4ECBu7ECD
          (1)u6570u5B57u7684u52A0u51CFuFF1Bu65E0u4E58u9664u529Fu80FD
          (2)u6570u5B57u5F00u65B9uFF0Cu4E0Du8FC7u6C42u5F00u65B9u7684u65F6u5019u8F93u5165u680Fu6700u597Du4E0Du8981u5B58u5728u52A0u51CFu7B26u53F7uFF0Cu5426u5219u4F1Au63D0u793Au4F60u6570u5B57u6837u5F0Fu9519u8BEF
          (3)u6E05u7A7AuFF0Cclsu952Eu53EFu4EE5u6E05u7A7Au8F93u5165u680Fu548Cu7ED3u679Cu680Fu7684u6587u5B57
    
    2.u6CE8u610Fu4E8Bu9879
          (1)u4E0Du8981u7528u952Eu76D8u8F93u5165u9664u4E86u52A0u51CFu53F7u548Cu5C0Fu6570u70B9u3001u6570u5B57u5916u5176u4ED6u7684u5B57u7B26u6216u8005u6C49u5B57	
          (2)u5982u679Cu8F93u5165u4E86u91CDu590Du7684u7B26u53F7u6216u8005u5728u672Bu5C3Eu51FAu73B0u4E86u7B26u53F7uFF0Cu6B64u8BA1u7B97u5668u80FDu591Fu6B63u786Eu5904u7406uFF0Cu4F46u4E5Fu4E0Du8981u4E71
    u8F93u5165uFF0Cu5F00u53D1u8005u4E5Fu6CA1u6709u5168u9762u5730u8003u8651
          (3)u7B2Cu4E00u4E2Au6570u5B57u4E3Au8D1Fu6570u7684u60C5u51B5u5F00u53D1u8005u6CA1u6709u60F3u5230u89E3u51B3u65B9u6848uFF0Cu6240u4EE5u8BF7u4F7Fu7528u8005u5C3Du91CFu5C06u6B63u6570u5199u5728u7B2Cu4E00u4E2Au4F4Du7F6Eu5427");
            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("u5173u4E8Eu8BA1u7B97u5668");
            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("u5173u4E8Eu8BA1u7B97u5668uFF1A
       @author	u5305u622C
       @version 1.0
       @since 2014
       u6B64u8BA1u7B97u5668u53EAu7528u4E8Eu4E2Au4EBAu6B23u8D4Fu53CAu5A31u4E50uFF0Cu4E0Du5F97u53C2u4E0Eu5546u4E1Au7528u9014uFF0Cu5982u9700u8F6Cu8F7DuFF0Cu8BF7u6CE8u660Eu51FAu5904uFF01u8C22u8C22");
            scrollPane.setViewportView(txtrauthor);
    
            final JCheckBox checkBox = new JCheckBox("u81EAu52A8u6362u884C");
            checkBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (checkBox.isSelected()) {
                        txtrauthor.setLineWrap(true);
                    }
                    else
                        txtrauthor.setLineWrap(true);
                }
            });
            scrollPane.setColumnHeaderView(checkBox);
        }
    
    }
    View Code
  • 相关阅读:
    本周个人总结(14周)
    本周个人总结
    排球计分规则
    我与计算机
    Tomcat处理HTTP请求原理
    数据结构与算法之排序算法(四):归并排序
    数据结构与算法之排序算法(三):选择排序
    数据结构与算法之排序算法(二):交换排序
    数据结构与算法之排序算法(一):插入排序
    数据结构与算法:概述+思维导图
  • 原文地址:https://www.cnblogs.com/jamsbwo/p/4587000.html
Copyright © 2020-2023  润新知