diff --git "a/\344\272\214\346\211\213\344\271\246\344\272\244\346\230\223\347\275\221" "b/\344\272\214\346\211\213\344\271\246\344\272\244\346\230\223\347\275\221" new file mode 100644 index 0000000..097221e --- /dev/null +++ "b/\344\272\214\346\211\213\344\271\246\344\272\244\346\230\223\347\275\221" @@ -0,0 +1,150 @@ +import java.awt.BorderLayout;import java.awt.Color;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 com.sun.org.apache.xalan.internal.xsltc.runtime.Operators; + +public class Mycalulator extends JFrame implements ActionListener { + private final String[]KEYS={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","="}; + private final String[]Command={"delete一位","归0"}; + private JButton[] keys=new JButton[KEYS.length]; + private JButton[] coms=new JButton[Command.length]; + private JTextField text=new JTextField("0"); + private boolean Firstdigit=true;//是否第一次输入 + private double result=0.0;//结果 + private String operator = "=";//默认操作为"=" + private boolean operateValidFlag = true;//操作是否合法 + public Mycalulator() { + // TODO Auto-generated constructor stub init(); + this.setTitle("只能计算加减乘除的..额东西"); + this.setLocationRelativeTo(null); + this.setVisible(true); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //this.setSize(width, height) + this.pack(); + } + private void init() { + // TODO Auto-generated method stub + text.setHorizontalAlignment(JTextField.RIGHT);//文本右对齐 + text.setEditable(false);//不可编辑 + text.setBackground(Color.WHITE);//背景白色 + JPanel keyspanel=new JPanel();//装按键的画板 + keyspanel.setLayout(new GridLayout(4,4,3,3));//布局为网格布局4行4列 水平竖直间距都为3像素 + for(int i=0;i=0){ + handlenumber(label); + } + else{ + handleOperator(label); + } + } + private void handleOperator(String label) { + // TODO Auto-generated method stub + if(operator.equals("/")){ + if(text.getText()=="0"){ + text.setText("除数不能为0"); + operateValidFlag=false; + handleC(); + } + else{ + result/=Double.valueOf(text.getText()); + } + } + else if(operator.equals("*")){ + result*=Double.valueOf(text.getText()); + } + else if(operator.equals("+")){ + result+=Double.valueOf(text.getText()); + } + else if(operator.equals("-")){ + result-=Double.valueOf(text.getText()); + } + else if(operator.equals("=")){ + result=Double.valueOf(text.getText()); + } + if(operateValidFlag){ + text.setText(String.valueOf(result)); + } + operator=label; + Firstdigit = true;//按完运算符后 在输入数字为第一次输入 + operateValidFlag=true; + } + private void handlenumber(String label) { + // TODO Auto-generated method stub + if(Firstdigit){ + if(label.equals(".")){ + text.setText(text.getText()+"."); + } + else{ + text.setText(label); + } + } + //按的是"."并且字符串里没"." + else if(label.equals(".")&&text.getText().indexOf(".")<0){ + text.setText(text.getText()+"."); + } + //按得是数字 + else if(!label.equals(".")){ + text.setText(text.getText()+label); + } + Firstdigit=false;//输过就不是第一次输入了 } + private void handleC() { + // TODO Auto-generated method stub + text.setText("0"); + Firstdigit=true; + operator="="; + } + private void handledelete() { + // TODO Auto-generated method stub + if(text.getText().length()>1){ + text.setText(text.getText().substring(0, text.getText().length()-1)); + } + else if(text.getText().length()==1){ + text.setText("0"); + } + } + public static void main(String args[]) { + Mycalulator calculator1 = new Mycalulator(); + // Mycalulator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } +} + +