JAVA編寫的簡單計算機

來源:互聯網
上載者:User

標籤:計算機   java   

package com.hellojava.practice.test;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.Panel;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class Numerator<textFiled> extends JFrame implements ActionListener{JFrame frame=new JFrame();TextField textField;private String op1="0";private boolean append=false;private String operator="+";public  Numerator(){frame.setTitle("簡單計算機");frame.setLayout(new BorderLayout());frame.setLocationRelativeTo(null);textField=new TextField();Panel panel1=new Panel();panel1.setLayout(new BorderLayout());panel1.add(textField, BorderLayout.NORTH);frame.add(panel1, BorderLayout.NORTH);JButton btn0=new JButton("0");btn0.addActionListener(this);JButton btn1=new JButton("1");btn1.addActionListener(this);JButton btn2=new JButton("2");btn2.addActionListener(this);JButton btn3=new JButton("3");btn3.addActionListener(this);JButton btn4=new JButton("4");btn4.addActionListener(this);JButton btn5=new JButton("5");btn5.addActionListener(this);JButton btn6=new JButton("6");btn6.addActionListener(this);JButton btn7=new JButton("7");btn7.addActionListener(this);JButton btn9=new JButton("9");btn9.addActionListener(this);JButton btn8=new JButton("8");btn8.addActionListener(this);JButton btna=new JButton("+");btna.addActionListener(this);JButton btnb=new JButton("-");btnb.addActionListener(this);JButton btnc=new JButton("*");btnc.addActionListener(this);JButton btnd=new JButton("/");btnd.addActionListener(this);JButton btne=new JButton("=");btne.addActionListener(this);JPanel panel2=new JPanel();panel2.setLayout(new GridLayout(4, 4));panel2.add(btn0);panel2.add(btn1);panel2.add(btn2);panel2.add(btn3);panel2.add(btn4);panel2.add(btn5);panel2.add(btn6);panel2.add(btn7);panel2.add(btn8);panel2.add(btn9);panel2.add(btna);panel2.add(btnb);frame.add(panel2,BorderLayout.CENTER);JPanel panel3=new JPanel();panel3.add(btnc);panel3.add(btnd);panel3.add(btne);panel3.setLayout(new GridLayout(1, 3));frame.add(panel3, BorderLayout.SOUTH);frame.pack();frame.setVisible(true);}public static void main(String[] args) {new Numerator();}@Overridepublic void actionPerformed(ActionEvent e) {String comm=e.getActionCommand();if("0123456789".indexOf(comm)!=-1){if(append){String temp=textField.getText();textField.setText(temp+comm);}else{textField.setText(comm);append=true;}}else if("+-*/".indexOf(comm)!=-1){op1=textField.getText();textField.setText(comm);operator=comm;append=false;}else if("=".equals(comm)){String op2=textField.getText();double d1=Double.parseDouble(op1);double d2=Double.parseDouble(op2);if(operator.equals("+")){d1=d1+d2;}else if(operator.equals("-")){d1=d1-d2;}else if(operator.equals("*")){d1=d1*d2;}else{d1=d1/d2;}String d3=d1+"";textField.setText(d3);append=false;}else if(".".equals(comm)){String temp=textField.getText();if(temp.indexOf(".")==-1){textField.setText(temp+".");append=true;}}}}


本文出自 “12212886” 部落格,轉載請與作者聯絡!

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.