java實現簡單計算機功能

來源:互聯網
上載者:User

標籤:

童鞋們,是不是有使用計算機的時候,還要進入運行,輸入calc,太麻煩了,有時候甚至還忘記單詞怎麼拼字,呵呵
程式員自己寫代碼實現,又簡單,又方便啊

以下為代碼(想要產生可執行工具可參考:http://www.cnblogs.com/holdon521/p/4483966.html)

package com;import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.UIManager;import javax.swing.border.EmptyBorder;public class Test  extends JFrame{/**   *  humphrey   */  private static final long serialVersionUID = -9075562467166618473L;  private JPanel contentPane;  private JTextField display;  private ActionListener insert = new InsertAction();  private ActionListener command = new CommandAction();  private double result = 0;  private String lastCommand = "=";  private boolean start = true;    /**   * Launch the application.   */  public static void main(String[] args) {      try {          UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");      } catch (Throwable e) {          e.printStackTrace();      }      EventQueue.invokeLater(new Runnable() {          public void run() {              try {                  Test frame = new Test();                  frame.setVisible(true);              } catch (Exception e) {                  e.printStackTrace();              }          }      });  }    /**   * Create the frame.   */  public Test() {      setTitle("\u8BA1\u7B97\u5668");      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      setLocationByPlatform(true);      contentPane = new JPanel();      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));      contentPane.setLayout(new BorderLayout(0, 0));      setContentPane(contentPane);            JPanel displayPanel = new JPanel();      contentPane.add(displayPanel, BorderLayout.NORTH);            display = new JTextField();      display.setText("0");      display.setHorizontalAlignment(SwingConstants.RIGHT);      display.setEditable(false);      display.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      displayPanel.add(display);      display.setColumns(13);            JPanel buttonPanel = new JPanel();      contentPane.add(buttonPanel, BorderLayout.CENTER);      buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));            JButton number7Button = new JButton("7");      number7Button.addActionListener(insert);      number7Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number7Button);            JButton number8Button = new JButton("8");      number8Button.addActionListener(insert);      number8Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number8Button);            JButton number9Button = new JButton("9");      number9Button.addActionListener(insert);      number9Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number9Button);            JButton divideButton = new JButton("/");      divideButton.addActionListener(command);      divideButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(divideButton);            JButton number4Button = new JButton("4");      number4Button.addActionListener(insert);      number4Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number4Button);            JButton number5Button = new JButton("5");      number5Button.addActionListener(insert);      number5Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number5Button);            JButton number6Button = new JButton("6");      number6Button.addActionListener(insert);      number6Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number6Button);            JButton multiplyButton = new JButton("*");      multiplyButton.addActionListener(command);      multiplyButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(multiplyButton);            JButton number3Button = new JButton("1");      number3Button.addActionListener(insert);      number3Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number3Button);            JButton number2Button = new JButton("2");      number2Button.addActionListener(insert);      number2Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number2Button);            JButton number1Button = new JButton("3");      number1Button.addActionListener(insert);      number1Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number1Button);            JButton subtractButton = new JButton("-");      subtractButton.addActionListener(command);      subtractButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(subtractButton);            JButton number0Button = new JButton("0");      number0Button.addActionListener(insert);      number0Button.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(number0Button);            JButton dotButton = new JButton(".");      dotButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(dotButton);            JButton equalButton = new JButton("=");      equalButton.addActionListener(command);      equalButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(equalButton);            JButton addButton = new JButton("+");      addButton.addActionListener(command);      addButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));      buttonPanel.add(addButton);      pack();  }    private class InsertAction implements ActionListener {            public void actionPerformed(ActionEvent e) {          String input = e.getActionCommand();          String text = display.getText();          if (start) {              display.setText("");              start = false;          }          if (text.startsWith(".")) {              display.setText("0" + display.getText() + input);          } else if (text.startsWith("-0.") || text.startsWith("0.")) {              display.setText(display.getText() + input);          } else if (text.startsWith("-0")) {              display.setText("-" + input);          } else if (text.startsWith("0")) {              display.setText(input);          } else {              display.setText(display.getText() + input);          }      }  }    private class CommandAction implements ActionListener {            public void actionPerformed(ActionEvent e) {          String command = e.getActionCommand();          if (start) {              if (command.equals("-")) {                  display.setText(command);                  start = false;              } else {                  lastCommand = command;              }          } else {              calculate(Double.parseDouble(display.getText()));              lastCommand = command;              start = true;          }      }  }    public void calculate(double x) {      char operator = lastCommand.charAt(0);      switch (operator) {          case ‘+‘:              result += x;              break;          case ‘-‘:              result -= x;              break;          case ‘*‘:              result *= x;              break;          case ‘/‘:              result /= x;              break;          case ‘=‘:              result = x;              break;      }      display.setText("" + result);  }}

 

java實現簡單計算機功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.