fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6.  
  7. public class calculate extends WindowAdapter
  8. {
  9. private JTextField Jtext=new JTextField("0.");
  10. private JFrame f=new JFrame("简单计算器");
  11. private String x="";
  12. private String y="";
  13. private String cal="";
  14. private boolean flag1=true;
  15. private boolean flag2=false;
  16.  
  17. public void init() //初始化
  18. {
  19. String[] buttonValue=new String[]{"1","2","3","+","C","4","5","6","-","退格","7","8","9","*",
  20. "1/x","0","+/-",".","/","="};
  21. Container contain=f.getContentPane();
  22. JPanel JPan=new JPanel();
  23. JButton[] Jb=new JButton[20];
  24. contain.setLayout(new BorderLayout()); //采用4行5列的网格布局
  25. JPan.setLayout(new GridLayout(4,5));
  26. Jtext.setHorizontalAlignment(JTextField.RIGHT);
  27. contain.add(Jtext,"North");
  28. contain.add(JPan);
  29. A num=new A(); //数据
  30. Operation op=new Operation(); //运算符
  31. Clear cl=new Clear(); //清零
  32. CountDown count_d=new CountDown(); //倒数
  33. Strains stra=new Strains(); //相反数
  34. Result re=new Result(); //结果
  35. BackSpace back=new BackSpace(); //退格
  36.  
  37. for(int i=0;i<Jb.length;i++) //利用for循环添加按钮
  38. {
  39. Jb[i]=new JButton(buttonValue[i]);
  40. JPan.add(Jb[i]);
  41. if(i==3||i==8||i==13||i==18)
  42. Jb[i].addActionListener(op);
  43. if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15||i==17)
  44. Jb[i].addActionListener(num);
  45. if(i==3||i==4||i==8||i==9||((i>12)&&(i<=19)&&(i!=15)))
  46. Jb[i].setForeground(new Color(255,0,0));
  47. else
  48. Jb[i].setForeground(new Color(0,0,255)); //设置按钮颜色
  49. }
  50. Jb[4].addActionListener(cl);
  51. Jb[9].addActionListener(back);
  52. Jb[14].addActionListener(count_d);
  53. Jb[16].addActionListener(stra);
  54. Jb[19].addActionListener(re);
  55. f.setSize(320,240);
  56. f.setVisible(true);
  57. f.addWindowListener( //采用匿名类实现窗口的正常关闭
  58. {
  59. public void windowClosing(WindowEvent e)
  60. {System.exit(0);}
  61. });
  62. }
  63.  
  64. class A implements ActionListener //输入数据
  65. {
  66. public void actionPerformed(ActionEvent e)
  67. {
  68. String a=Jtext.getText();
  69. String s=e.getActionCommand();
  70. if(a.equals("0.")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/"))
  71. Jtext.setText(s);
  72. else{
  73. if(flag2)
  74. {Jtext.setText(s);
  75. flag2=false;}
  76. else
  77. Jtext.setText(a+s);
  78. }
  79. }
  80. }
  81.  
  82. class Operation implements ActionListener
  83. {
  84. public void actionPerformed(ActionEvent e)
  85. {
  86. cal=e.getActionCommand();
  87. if(flag1==true)
  88. x=Jtext.getText();
  89. Jtext.setText(cal);
  90. flag1=false;
  91. }
  92. }
  93.  
  94. class Clear implements ActionListener //清零功能
  95. {
  96. public void actionPerformed(ActionEvent e)
  97. {
  98. Jtext.setText("0.");
  99. }
  100. }
  101.  
  102. class CountDown implements ActionListener //求倒数类
  103. {
  104. public void actionPerformed(ActionEvent e) 
  105. {
  106. String s=e.getActionCommand();
  107. String s1 = Jtext.getText();
  108. if(s.equals("1/x"))
  109. s1=new String(""+1/Double.parseDouble(s1));
  110. Jtext.setText(s1);
  111. }
  112. }
  113. class Strain implements ActionListener //求相反数类
  114. {
  115. public void actionPerformed(ActionEvent e)
  116. {
  117. String s=e.getActionCommand();
  118. String s1=Jtext.getText();
  119. if(s.equals("+/-"))
  120. s1=new String(""+(0-Double.parseDouble(s1)));
  121. Jtext.setText(s1);
  122. }
  123. }
  124.  
  125. class BackSpace implements ActionListener //退格功能
  126. {
  127. public void actionPerformed(ActionEvent e)
  128. {
  129. String s=e.getActionCommand();
  130. String s1=Jtext.getText();
  131. if(s.equals("退格"))
  132. s1=new String(s1.substring(0,s1.length()-1));
  133. Jtext.setText(s1);
  134. }
  135. }
  136.  
  137. class Result implements ActionListener //计算并显示结果
  138. {
  139. public void actionPerformed(ActionEvent e)
  140. {
  141. double num1;
  142. num1=Double.parseDouble(x);
  143. y=Jtext.getText();
  144. double num2;
  145. num2=Double.parseDouble(y);
  146. double result=0;
  147. if(num2!=0)
  148. {
  149. if(cal.equals("+"))
  150. result=num1+num2;
  151. if(cal.equals("-"))
  152. result=num1-num2;
  153. if(cal.equals("*"))
  154. result=num1*num2;
  155. String s1=Double.toString(result);
  156. Jtext.setText(s1);
  157. }
  158. if(cal.equals("/"))
  159. {
  160. if(num2==0)
  161. Jtext.setText("除数不能为0");
  162. else
  163. {
  164. result=num1/num2;
  165. String s1=Double.toString(result);
  166. Jtext.setText(s1);
  167. }
  168. }
  169. flag1=true;
  170. flag2=true;
  171. }
  172. }
  173.  
  174. public static void main(String[] args) //main方法
  175. {
  176. calculate count=new calculate();
  177. count.init();
  178. }
  179.  
  180. }
  181.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:105: error: illegal character: '\u00a0'
		public void actionPerformed(ActionEvent?e)?
		                                       ^
Main.java:105: error: ';' expected
		public void actionPerformed(ActionEvent?e)?
		                                        ^
Main.java:105: error: illegal start of type
		public void actionPerformed(ActionEvent?e)?
		                                         ^
Main.java:105: error: illegal character: '\u00a0'
		public void actionPerformed(ActionEvent?e)?
		                                          ^
Main.java:105: error: ';' expected
		public void actionPerformed(ActionEvent?e)?
		                                           ^
Main.java:109: error: illegal start of type
			if(s.equals("1/x"))
			^
Main.java:109: error: <identifier> expected
			if(s.equals("1/x"))
			           ^
Main.java:109: error: ';' expected
			if(s.equals("1/x"))
			            ^
Main.java:109: error: illegal start of type
			if(s.equals("1/x"))
			                 ^
Main.java:109: error: <identifier> expected
			if(s.equals("1/x"))
			                  ^
Main.java:109: error: ';' expected
			if(s.equals("1/x"))
			                   ^
Main.java:110: error: illegal start of type
				s1=new String(""+1/Double.parseDouble(s1));
				  ^
Main.java:110: error: <identifier> expected
				s1=new String(""+1/Double.parseDouble(s1));
				   ^
Main.java:110: error: ';' expected
				s1=new String(""+1/Double.parseDouble(s1));
				      ^
Main.java:110: error: illegal start of type
				s1=new String(""+1/Double.parseDouble(s1));
				             ^
Main.java:110: error: <identifier> expected
				s1=new String(""+1/Double.parseDouble(s1));
				              ^
Main.java:110: error: ';' expected
				s1=new String(""+1/Double.parseDouble(s1));
				                ^
Main.java:110: error: illegal start of type
				s1=new String(""+1/Double.parseDouble(s1));
				                 ^
Main.java:110: error: <identifier> expected
				s1=new String(""+1/Double.parseDouble(s1));
				                  ^
Main.java:110: error: ';' expected
				s1=new String(""+1/Double.parseDouble(s1));
				                   ^
Main.java:110: error: illegal start of type
				s1=new String(""+1/Double.parseDouble(s1));
				                         ^
Main.java:110: error: <identifier> expected
				s1=new String(""+1/Double.parseDouble(s1));
				                                        ^
Main.java:110: error: ';' expected
				s1=new String(""+1/Double.parseDouble(s1));
				                                         ^
Main.java:111: error: <identifier> expected
			Jtext.setText(s1);
			             ^
Main.java:111: error: <identifier> expected
			Jtext.setText(s1);
			                ^
Main.java:175: error: class, interface, or enum expected
    public static void main(String[] args) //main??
                  ^
Main.java:178: error: class, interface, or enum expected
    	count.init();
    	^
Main.java:179: error: class, interface, or enum expected
    }
    ^
28 errors
stdout
Standard output is empty