标签:c style class blog code java
package com.cal; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class cal extends JFrame implements ActionListener{ private static JTextField tf = new JTextField(); private static JButton[] btn= new JButton[17]; //17 buttons private double num1=0.0 ,num2=0.0,result=0.0; String opt = null; //consduct method public cal(){ //使用父类构造方法方法子类构造方法的第一行 super("CalculatorHaha"); } public void launch(){ Font f = new Font("f",Font.BOLD,20); tf.setFont(f); tf.setEditable(false); GridBagLayout gb = new GridBagLayout(); setBounds(700,100,300,310); setLayout(gb); String[] str={"7","8","9","/","4","5","6","*","1","2","3","-",".","0","=","+"}; GridBagConstraints c = new GridBagConstraints(); //set c.weightx = 0.2; c.weighty = 0.2; c.gridy = 0; c.gridwidth = 3 ; c.fill =GridBagConstraints.BOTH; gb.setConstraints(tf, c); add(tf); c.gridx = GridBagConstraints.RELATIVE; c.gridwidth = 1 ; btn[16] = new JButton("CE"); // gb.setConstraints(btn[16], c); add(btn[16]); c.gridy = 1 ; for(int i = 0;i<4;i++){ btn[i] = new JButton(str[i]); gb.setConstraints(btn[i], c); add(btn[i]); } c.gridy = 2 ; for(int i = 4;i<8;i++){ btn[i] = new JButton(str[i]); gb.setConstraints(btn[i], c); add(btn[i]); } c.gridy = 3 ; for(int i = 8;i<12;i++){ btn[i] = new JButton(str[i]); gb.setConstraints(btn[i], c); add(btn[i]); } c.gridy = 4 ; for(int i =12;i<16;i++){ btn[i] = new JButton(str[i]); gb.setConstraints(btn[i], c); add(btn[i]); } /*** * ActionEvent * getActionCo返回与命令相关的字符串 * 添加事件 * */ for(int i= 0;i<btn.length;i++){ btn[i].addActionListener(this); } setVisible(true); } @Override public void actionPerformed(ActionEvent e) { //获取事件源 Object temp = e.getSource(); //789 for(int i = 0 ;i<3;i++){ if(temp == btn[i]){ tf.setText(tf.getText()+btn[i].getActionCommand()); } } //456 for(int i = 4 ;i<7;i++){ if(temp == btn[i]){ tf.setText(tf.getText()+btn[i].getActionCommand()); } } //123 for(int i = 8 ;i<11;i++){ if(temp == btn[i]){ tf.setText(tf.getText()+btn[i].getActionCommand()); } } // . 0 需要先判读是不是存在小数点假如存在小数点则不处理 /** * api 中看到方法 * boolean contains(CharSequence s) * 当且仅当此字符串包含指定的 char 值序列时,返回 true。 *先转换成char数组然后 判断是否存在 * */ if(!iscontains(tf.getText(),‘.‘)){ if(temp.equals(btn[12])){ tf.setText(tf.getText()+btn[12].getActionCommand()); } } if(temp.equals(btn[13])){ tf.setText(tf.getText()+btn[13].getActionCommand()); } //CE if(temp.equals(btn[16])){ tf.setText(""); } //运算 //3 7 11 15 /*-+ if(temp == btn[3]){ //获取数字进行计算 num1 = Double.parseDouble(tf.getText()); opt = "/"; tf.setText(""); } if(temp == btn[7]){ num1 = Double.parseDouble(tf.getText()); opt = "*"; tf.setText(""); } if(temp == btn[11]){ num1 = Double.parseDouble(tf.getText()); opt = "-"; tf.setText(""); } if(temp == btn[15]){ num1 = Double.parseDouble(tf.getText()); opt = "+"; tf.setText(""); } if(temp == btn[14]){ num2 = Double.parseDouble(tf.getText()); switch(opt){ case "+":result = num1+num2; break; case "-" :result = num1-num2; break; case "*" :result = num1*num2; break; case "/" :result = num1/num2;break; default:tf.setText("运算符出错");break; } tf.setText(result+""); } } /** * 判断字符串是否包含指定字符方法 * */ boolean iscontains(String str,char ch){ //拆分字符串 char c[] = str.toCharArray(); for(int i =0;i<c.length;i++){ if(c[i] == ‘.‘){ return true; } } return false; } public static void main(String[] args) { cal c = new cal(); c.launch(); } }
标签:c style class blog code java
原文地址:http://www.cnblogs.com/oaks/p/3778995.html