第一次写(JAVA的计算器,新手勿喷)。 哈哈啊哈哈
class Proformed { public static void main(String[] args) { // TODO 自动生成的方法存根 NewCal cal = new NewCal("Calculator"); } } //上边是个主函数哈哈哈哈哈 //下边是正文,我记得以前是上传过文件夹的,可是怎么找不到了。 所以把源码贴出来。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class NewCal implements ActionListener{ JTextArea showText = new JTextArea(1 ,30); JButton zero = new JButton("0"); JButton one = new JButton("1"); JButton two = new JButton("2"); JButton three = new JButton("3"); JButton four = new JButton("4"); JButton five = new JButton("5"); JButton six = new JButton("6"); JButton seven = new JButton("7"); JButton eight = new JButton("8"); JButton nine = new JButton("9"); JButton pluse = new JButton("+"); JButton subtract = new JButton("—"); JButton multiplication = new JButton("×"); JButton devisionMethod = new JButton("/"); JButton equal = new JButton("="); JButton pointer = new JButton("."); JPanel panel = new JPanel(); protected NewCal(String str) { JFrame frame = new JFrame(str); panel.setLayout(new GridLayout(4 , 4)); panel.add(one); panel.add(two); panel.add(three); panel.add(pluse); panel.add(four); panel.add(five); panel.add(six); panel.add(subtract); panel.add(seven); panel.add(eight); panel.add(nine); panel.add(multiplication); panel.add(zero); panel.add(pointer); panel.add(equal); panel.add(devisionMethod); Container container =frame.getContentPane(); container.add(showText , BorderLayout.NORTH); container.add(panel , BorderLayout.CENTER); frame.setVisible(true); frame.setSize(300, 300); zero.addActionListener(this); one.addActionListener(this); two.addActionListener(this); three.addActionListener(this); four.addActionListener(this); five.addActionListener(this); six.addActionListener(this); seven.addActionListener(this); eight.addActionListener(this); nine.addActionListener(this); pluse.addActionListener(this); subtract.addActionListener(this); multiplication.addActionListener(this); devisionMethod.addActionListener(this); pointer.addActionListener(this); equal.addActionListener(this); } protected void windowClosing(WindowEvent e) { System.exit(0); } double count = 0; boolean divisor=false;//目的在于检测有没有第一次就按运算符导致错误(这个变量现在看不懂了) boolean zeroAfterEqual=false; public void actionPerformed(ActionEvent e) { if(e.getSource()==zero) zeroMethod(); if(e.getSource()==one) oneMethod(); if(e.getSource()==two) twoMethod(); if(e.getSource()==three) threeMethod(); if(e.getSource()==four) fourMethod(); if(e.getSource()==five) fiveMethod(); if(e.getSource()==six) sixMethod(); if(e.getSource()==seven) sevenMethod(); if(e.getSource()==eight) eightMethod(); if(e.getSource()==nine) nineMethod(); if(e.getSource()==pluse) try { pluseMethod(); } catch(Exception m) { showText.setText("请先输入数字"); b=1; } if(e.getSource()==subtract) try { subtractMethod(); } catch(Exception m) { showText.setText("请先输入数字"); b=1; } if(e.getSource()==multiplication) try { multiplicationMethod(); } catch(Exception m) { showText.setText("请先输入数字"); b=1; } if(e.getSource()==devisionMethod) try { devisionMethods(); } catch(Exception m) { showText.setText("请先输入数字"); b=1; } if(e.getSource()==equal) equalMethod(); if(e.getSource()==pointer) pointerMethod(); } boolean cm=false; int b=0; int test=0; boolean clean= false; double a=0; protected void zeroMethod() { b(); showText.append("0"); cm=true; } protected void oneMethod() { b(); showText.append("1"); } protected void twoMethod() { b(); showText.append("2"); } protected void threeMethod() { b(); showText.append("3"); } protected void fourMethod() { b(); showText.append("4"); } protected void fiveMethod() { b(); showText.append("5"); } protected void sixMethod() { b(); showText.append("6"); } protected void sevenMethod() { b(); showText.append("7"); } protected void eightMethod() { b(); showText.append("8"); } protected void nineMethod() { b(); showText.append("9"); } int p=0; protected void pluseMethod() { if(p==0) { count+=Double.parseDouble(showText.getText()); showText.setText("+"); clean=true; test=1; p++; } if(p==1) {p--;}//有错代码 } protected void subtractMethod() { count=Double.parseDouble(showText.getText()); showText.setText("—"); clean=true; test=2; } protected void multiplicationMethod() { if(count==0) { count=1.0; count*=Double.parseDouble(showText.getText()); showText.setText("×"); } else { count*=Double.parseDouble(showText.getText()); showText.setText("×"); } test=3; clean=true; } protected void devisionMethods() { count=Double.parseDouble(showText.getText()); showText.setText("/"); test=4; divisor=true; clean=true; } protected void pointerMethod() { showText.append("."); } protected void equalMethod() { if(cm==true) { showText.setText("1"); cm=false; } switch(test) { case 1 : {count+=Double.parseDouble(showText.getText()); a=count;} break ; case 2 : {count-=Double.parseDouble(showText.getText()); a=count; } break ; case 3 : {count*=Double.parseDouble(showText.getText()); a=count; } break ; case 4 : {count/=Double.parseDouble(showText.getText()); a=count; } break ; default : showText.setText(""); } showText.setText(String.valueOf(a)); count=0; b++; } protected void b() { if(b==1) {showText.setText(""); b=0;} if(clean==true) showText.setText(""); if(divisor==true) showText.setText("除数不能为"); divisor=false; clean=false; } }