《Java课程设计 计算器simplecalculator.docx》由会员分享,可在线阅读,更多相关《Java课程设计 计算器simplecalculator.docx(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Java课程设计 计算器simplecalculator import java.awt.*;import java.awt.event.*;import javax.swing.*;class simplecalculator static String point=new String();static String Amal=new String();static String ONE=new String();static String TWO=new String();static String THREE=new String();static String FOUR=new St
2、ring();static String FIVE=new String();static String SIX=new String();static String SEVEN=new String();static String EIGHT=new String();static String NINE=new String();static String ZERO=new String();static String ResultState=new String();static Double QF;static JButton zero=new JButton(0);static JB
3、utton one=new JButton(1);static JButton two=new JButton(2);static JButton three=new JButton(3);static JButton four=new JButton(4);static JButton five=new JButton(5);static JButton six=new JButton(6);static JButton seven=new JButton(7);static JButton eight=new JButton(8);static JButton nine=new JButt
4、on(9);static JButton add=new JButton(+);static JButton sub=new JButton(-);static JButton mul=new JButton(*);static JButton div=new JButton(/);static JButton QuFan=new JButton(+/-);static JButton Dian=new JButton(.);static JButton equal=new JButton(=);static JButton clear=new JButton(C);static JButto
5、n BaiFen=new JButton(%);static JButton FenZhiYi=new JButton(1/x);static int i=0;static Double addNumber;static Double subNumber;static Double mulNumber;static Double divNumber;static Double equalNumber;static Double temp;static JTextArea result=new JTextArea(1,20);public static void main(String args
6、)JFrame frame=new JFrame(计算器);result.setEditable(false);result.setText();ResultState=窗口空;JPanel ForResult=new JPanel();JPanel ForButton7_clear=new JPanel();JPanel ForButton4_mul=new JPanel();JPanel ForButton1_sub=new JPanel();JPanel ForButton0_equal=new JPanel();FlowLayout FLO=new FlowLayout();ForRe
7、sult.add(result);ForButton7_clear.setLayout(FLO);ForButton7_clear.add(seven);ForButton7_clear.add(eight);ForButton7_clear.add(nine);ForButton7_clear.add(div);ForButton7_clear.add(clear);ForButton4_mul.setLayout(FLO);ForButton4_mul.add(four);ForButton4_mul.add(five);ForButton4_mul.add(six);ForButton4
8、_mul.add(mul);ForButton4_mul.add(BaiFen);ForButton1_sub.setLayout(FLO);ForButton1_sub.add(one);ForButton1_sub.add(two);ForButton1_sub.add(three);ForButton1_sub.add(sub);ForButton1_sub.add(FenZhiYi);ForButton0_equal.setLayout(FLO);ForButton0_equal.add(zero);ForButton0_equal.add(QuFan);ForButton0_equa
9、l.add(Dian);ForButton0_equal.add(add);ForButton0_equal.add(equal);frame.getContentPane().setLayout(FLO);frame.getContentPane().add(ForResult);frame.getContentPane().add(ForButton7_clear);frame.getContentPane().add(ForButton4_mul);frame.getContentPane().add(ForButton1_sub);frame. getContentPane().add
10、(ForButton0_equal);frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);frame.setBounds(250,250,245,245);frame.setResizable(false);frame.setVisible(true);clear.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)result.setText();ZERO=;ONE=;TWO=;THREE=;FOUR=;FIVE=;SIX=;SEV
11、EN=;EIGHT=;NINE=;ResultState=窗口空;point=;i=0;);zero.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)ZERO=已经点击;ResultState=窗口不为空;if(ONE=已经点击|TWO=已经点击|THREE=已经点击|FOUR=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击)result.append(0);if(ResultState=窗口空)result.setT
12、ext(0););one.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)ONE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(1);if(ResultState=窗口空)result.setText(1);
13、);two.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)TWO=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(2);if(ResultState=窗口空)result.setText(2););three
14、.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)THREE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(3);if(ResultState=窗口空)result.setText(3); );four.ad
15、dActionListener( new ActionListener()public void actionPerformed(ActionEvent e)FOUR=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击| |SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(4);if(ResultState=窗口空)result.setText(4););five.addAc
16、tionListener( new ActionListener()public void actionPerformed(ActionEvent e)FIVE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(5);if(ResultState=窗口空)result.setText(6););six.addActionLi
17、stener( new ActionListener()public void actionPerformed(ActionEvent e)SIX=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(6);if(ResultState=窗口空)result.setText(6););seven.addActionListene
18、r( new ActionListener()public void actionPerformed(ActionEvent e)SEVEN=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(7);if(ResultState=窗口空)result.setText(7););eight.addActionListener(
19、new ActionListener()public void actionPerformed(ActionEvent e)EIGHT=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(8);if(ResultState=窗口空)result.setText(8););nine.addActionListener( new
20、ActionListener()public void actionPerformed(ActionEvent e)NINE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(9);if(ResultState=窗口空)result.setText(9););Dian.addActionListener( n ew Acti
21、onListener()public void actionPerformed(ActionEvent e)point=已经点击;i=i+1;if(ResultState=窗口不为空&i=1)result.append(.););add.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择加号;addNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);sub.addAc
22、tionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择减号;subNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);mul.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择乘号;mulNumber=Double.valueOf(result.getT
23、ext().doubleValue();result.setText();i=0;);div.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择除号;divNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);QuFan.addActionListener( new ActionListener()public void actionPerformed(ActionEv
24、ent e)QF=new Double(Double.valueOf(result.getText().doubleValue();QF=QF*(-1);result.setText(QF.toString(););equal.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)equalNumber=Double.valueOf(result.getText().doubleValue();if(Amal=已经选择加号)temp=addNumber+equalNumber;resul
25、t.setText(temp.toString();if(Amal=已经选择减号)temp=subNumber-equalNumber;result.setText(temp.toString();if(Amal=已经选择乘号)temp=mulNumber*equalNumber;result.setText(temp.toString();if(Amal=已经选择除号)temp=divNumber/equalNumber;result.setText(temp.toString(););BaiFen.addActionListener( new ActionListener()public
26、void actionPerformed(ActionEvent e)if(ResultState=窗口不为空)temp=Double.valueOf(result.getText().doubleValue()/100;result.setText(temp.toString(););FenZhiYi.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)temp=1/(Double.valueOf(result.getText().doubleValue();result.setText(temp.toString(););