Love丶FFC's Blog

2016年12月-Java语言程序设计(课程设计)

2019-10-25 09:55:28
阅读:838   •   评论:8

首先看当时写的代码。

  1. package Java课程设计;
  2. import javax.swing.*;
  3.  
  4. import java.io.*;
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.beans.Expression;
  9.  
  10.  
  11. public class Jisuanqi {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. new Myframe();
  16. }
  17.  
  18. }
  19. class Myframe extends JFrame implements ActionListener
  20. {
  21. int i=0;
  22. String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
  23. double E=2.718281828459;
  24. double π=3.1415926;
  25. String[] s=new String[]{"","Backspace","CE","C"," MC ","7","8","9","/","√","MR","4","5","6","*",
  26. "%","MS","1","2","3","-","1/x","M+","0","+/-",".","+","=",
  27. "sin","cos","tan","x^2","x^3","x^y","n!","3√x","y√x","log",
  28. "10^x","ln","e^x","π","±","16(H)","10(D)","8(O)","2(B)","sinh","cosh","tanh","Int","sum"}; //字符串数组//
  29.  
  30. JTextField txt1=new JTextField("",18);
  31. JTextField txt2=new JTextField("",18);
  32. JLabel lab1=new JLabel("运算框",JLabel.CENTER);
  33. JLabel lab2=new JLabel("结果框",JLabel.CENTER);
  34. JPanel p1=new JPanel(new GridLayout(2,0)); //创建面板p1,布局为1行0列网格布局//
  35. JPanel p2=new JPanel(new GridLayout(1,4)); //创建面板p2,布局为1行4列网格布局//
  36. JPanel p3=new JPanel(new GridLayout(8,6)); //创建面板p3,布局为4行6列网格布局//
  37. String path = "D://Background.jpg"; //背景图片//
  38. ImageIcon Imagebackground = new ImageIcon(path); //把背景图片显示在一个标签里面//
  39. JLabel Imagelabel = new JLabel(Imagebackground); //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明//
  40. JPanel imagePanel = (JPanel) this.getContentPane();
  41. JButton[] but=new JButton[52]; //创建按钮数组//
  42. {
  43. for(i=0;i<52;i++) //将字符串数组s的内容一一作为按钮数组的名字//
  44. { //---------//
  45. but[i]=new JButton(s[i]); //---------// //数组下标从0开始//
  46. } //---------//
  47. } //---------//
  48. JMenuBar jmb=new JMenuBar();
  49. JMenu jm1=new JMenu("文件");
  50. JMenu jm2=new JMenu("查看");
  51. JMenu jm3=new JMenu("帮助");
  52. JMenuItem jmm1=new JMenuItem("退出");
  53. JMenuItem jmm2=new JMenuItem("关于计算器");
  54. JMenuItem jmm3=new JMenuItem("源代码");
  55. public Myframe()
  56. {
  57. try {
  58. UIManager.setLookAndFeel(lookAndFeel);
  59. } catch (ClassNotFoundException | InstantiationException
  60. | IllegalAccessException | UnsupportedLookAndFeelException e) {
  61. // TODO 自动生成的 catch 块
  62. e.printStackTrace();
  63. }
  64. this.setTitle("计算器"); //设置窗体标题为 计算器 //
  65. this.setSize(414,360); //设置窗体的大小(单位为像素)//
  66. Imagelabel.setBounds(0, 0, this.getWidth(), this.getHeight()); //把标签的大小位置设置为图片刚好填充整个面板 //
  67. imagePanel.setOpaque(false); //把背景图片添加到分层窗格的最底层作为背景 //
  68. this.getLayeredPane().add(Imagelabel, new Integer(Integer.MIN_VALUE));
  69. Add();
  70. Set();
  71. for(i=0;i<52;i++)
  72. {
  73. but[i].addActionListener(this); //为所有按钮添加事件响应//
  74. }
  75. txt2.setEditable(false);
  76. jmm1.addActionListener(this);
  77. jmm2.addActionListener(this);
  78. jmm3.addActionListener(this);
  79. this.setLayout(new FlowLayout(FlowLayout.LEFT,0,0)); //设置该窗体的布局//
  80. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置该窗体三按钮的作用//
  81. this.setLocationRelativeTo(null); //设置该窗体位于屏幕的位置为居中//
  82. this.setVisible(true); //设置窗体可见性为真//
  83. }
  84. @Override
  85. public void actionPerformed(ActionEvent e) { //事件响应//
  86. String save;
  87. if(e.getSource()==but[1]) //如果触发按钮but[1],计算器退格事件//
  88. {
  89. String s1=txt1.getText();
  90. String s2 =""; //定义一个“”字符串// //将s1的长度赋给sl//
  91. System.out.println(""+s1.length()); //检验字符串长度是否正确//
  92. for(i=0;i<s1.length()-1;i++) //总长度-1==退格//
  93. {
  94. s2=s2+s1.charAt(i); //依次获取s1-1的每一位// //依次加到s2上//
  95. }
  96. txt1.setText(s2);
  97. }
  98. else if(e.getSource()==but[2]) //如果触发按钮but[3],计算器全部清零事件//
  99. {
  100. String s1=txt1.getText();
  101. s1=null;
  102. txt1.setText(s1);
  103. }
  104. else if(e.getSource()==but[3]) //如果触发按钮but[3],计算器输入清零事件//
  105. {
  106. String s1=txt1.getText(); //将txt1的内容赋给s3//
  107. String s2=txt2.getText();
  108. s2=null;
  109. s1=null; //将null赋给s3,起到计算器清零的作用//
  110. txt1.setText(s1); //将s3的内容显示在txt1上//
  111. txt2.setText(s2);
  112. }
  113. else if(e.getSource()==but[4]) //存储器清空
  114. {
  115. save="";
  116. System.out.println("存储器内容:"+save);
  117. }
  118. else if(e.getSource()==but[5])
  119. {
  120. String s1=txt1.getText();
  121. s1=s1+"7"; //添加7
  122. txt1.setText(s1);
  123. }
  124. else if(e.getSource()==but[6])
  125. {
  126. String s1=txt1.getText();
  127. s1=s1+"8"; //添加8
  128. txt1.setText(s1);
  129. }
  130. else if(e.getSource()==but[7])
  131. {
  132. String s1=txt1.getText();
  133. s1=s1+"9"; //添加9
  134. txt1.setText(s1);
  135. }
  136. else if(e.getSource()==but[8])
  137. {
  138. String s1=txt1.getText();
  139. s1=s1+"/"; //添加/
  140. txt1.setText(s1);
  141. }
  142. else if(e.getSource()==but[9])
  143. {
  144. String s1=txt1.getText();
  145. s1="√"+s1; //添加√
  146. txt1.setText(s1);
  147. }
  148. else if(e.getSource()==but[10])
  149. {
  150. txt1.setText(txt2.getText());
  151. }
  152. else if(e.getSource()==but[11])
  153. {
  154. String s1=txt1.getText();
  155. s1=s1+"4"; //添加4
  156. txt1.setText(s1);
  157. }
  158. else if(e.getSource()==but[12])
  159. {
  160. String s1=txt1.getText();
  161. s1=s1+"5"; //添加5
  162. txt1.setText(s1);
  163. }
  164. else if(e.getSource()==but[13])
  165. {
  166. String s1=txt1.getText();
  167. s1=s1+"6"; //添加6
  168. txt1.setText(s1);
  169. }
  170. else if(e.getSource()==but[14])
  171. {
  172. String s1=txt1.getText();
  173. s1=s1+"*"; //添加*
  174. txt1.setText(s1);
  175. }
  176. else if(e.getSource()==but[15])
  177. {
  178. String s1=txt1.getText();
  179. s1=s1+"%"; //添加%
  180. txt1.setText(s1);
  181. }
  182. else if(e.getSource()==but[16])
  183. {
  184. save=txt2.getText();
  185. System.out.println("存储器内容:"+save); //完成MS
  186. }
  187. else if(e.getSource()==but[10])
  188. {
  189. } //完成MR
  190. else if(e.getSource()==but[17])
  191. {
  192. String s1=txt1.getText();
  193. s1=s1+"1"; //添加1
  194. txt1.setText(s1);
  195. }
  196. else if(e.getSource()==but[18])
  197. {
  198. String s1=txt1.getText();
  199. s1=s1+"2"; //添加2
  200. txt1.setText(s1);
  201. }
  202. else if(e.getSource()==but[19])
  203. {
  204. String s1=txt1.getText();
  205. s1=s1+"3"; //添加3
  206. txt1.setText(s1);
  207. }
  208. else if(e.getSource()==but[20])
  209. {
  210. String s1=txt1.getText();
  211. s1=s1+"-"; //添加-
  212. txt1.setText(s1);
  213. }
  214. else if(e.getSource()==but[21])
  215. {
  216. String s1=txt1.getText();
  217. double number1=Double.parseDouble(s1);
  218. number1=number1/(number1*number1); //倒数运算的部分
  219. txt1.setText("1/"+s1);
  220. txt2.setText(""+number1);
  221. }
  222. else if(e.getSource()==but[22]) //完成M+
  223. {
  224. save="";
  225. save=save+txt2.getText()+"+";
  226. txt1.setText(save);
  227. System.out.println("存储器内容:"+save);
  228. }
  229. else if(e.getSource()==but[23])
  230. {
  231. String s1=txt1.getText();
  232. s1=s1+"0"; //添加0
  233. txt1.setText(s1);
  234. }
  235. else if(e.getSource()==but[25])
  236. {
  237. String s1=txt1.getText();
  238. if(s1=="")
  239. {
  240. s1="0."+s1; //添加0.
  241. txt1.setText(s1);
  242. }
  243. else if(txt1.getText()!="")
  244. {
  245. s1=s1+"."; //添加.
  246. txt1.setText(s1);
  247. }
  248. }
  249. else if(e.getSource()==but[26])
  250. {
  251. String s1=txt1.getText();
  252. s1=s1+"+"; //添加+
  253. txt1.setText(s1);
  254. }
  255.  
  256.  
  257.  
  258. else if(e.getSource()==but[27])
  259. {
  260. String s1=txt1.getText();
  261. String s2="";
  262. String s3="";
  263. double result;
  264. int place;
  265. for(i=0;i<s1.length();i++)
  266. {
  267. place=i;
  268. if(i!=0 && s1.charAt(i)=='+') //正数加正数、正数加负数的计算部分
  269. {
  270. for(i=0;i<place;i++)
  271. {
  272. s2=s2+s1.charAt(i);
  273. }
  274. for(i=place+1;i<s1.length();i++)
  275. {
  276. s3=s3+s1.charAt(i);
  277. }
  278. double number1=Double.parseDouble(s2);
  279. double number2=Double.parseDouble(s3);
  280. result =number1+number2;
  281. txt2.setText(""+result);
  282. }
  283. else if(s1.charAt(0)!='-' && s1.charAt(i)=='-') //做正数减正数、正数减负数的计算部分
  284. {
  285. place=i;
  286. for(i=0;i<place;i++)
  287. {
  288. s2=s2+s1.charAt(i);
  289. }
  290. for(i=place+1;i<s1.length();i++)
  291. {
  292. s3=s3+s1.charAt(i);
  293. }
  294. double number1=Double.parseDouble(s2);
  295. double number2=Double.parseDouble(s3);
  296. result =number1-number2;
  297. txt2.setText(""+result);
  298. }
  299. else if(s1.charAt(0)=='-')
  300. {
  301. for(i=1;i<s1.length()-1;i++)
  302. {
  303. if(s1.charAt(i)=='+') //做负数加负数、负数加正数的计算部分
  304. {
  305. place=i;
  306. for(i=0;i<place;i++)
  307. {
  308. s2=s2+s1.charAt(i);
  309. }
  310. for(i=place+1;i<s1.length();i++)
  311. {
  312. s3=s3+s1.charAt(i);
  313. }
  314. double number1=Double.parseDouble(s2);
  315. double number2=Double.parseDouble(s3);
  316. result =number1+number2;
  317. txt2.setText(""+result);
  318. }
  319. else if(s1.charAt(i)=='-') //做负数减负数,负数减正数的计算部分
  320. {
  321. place=i;
  322. for(i=0;i<place;i++)
  323. {
  324. s2=s2+s1.charAt(i);
  325. }
  326. for(i=place+1;i<s1.length();i++)
  327. {
  328. s3=s3+s1.charAt(i);
  329. }
  330. double number1=Double.parseDouble(s2);
  331. double number2=Double.parseDouble(s3);
  332. result =number1-number2;
  333. txt2.setText(""+result);
  334. }
  335. else if(s1.charAt(i)=='*') //做负数乘正数、负数乘负数的计算部分
  336. {
  337. place=i;
  338. for(i=0;i<place;i++)
  339. {
  340. s2=s2+s1.charAt(i);
  341. }
  342. for(i=place+1;i<s1.length();i++)
  343. {
  344. s3=s3+s1.charAt(i);
  345. }
  346. double number1=Double.parseDouble(s2);
  347. double number2=Double.parseDouble(s3);
  348. result =number1*number2;
  349. txt2.setText(""+result);
  350. }
  351. else if(s1.charAt(i)=='/') //做负数除正数、负数除负数的计算部分
  352. {
  353. place=i;
  354. for(i=0;i<place;i++)
  355. {
  356. s2=s2+s1.charAt(i);
  357. }
  358. for(i=place+1;i<s1.length();i++)
  359. {
  360. s3=s3+s1.charAt(i);
  361. }
  362. double number1=Double.parseDouble(s2);
  363. double number2=Double.parseDouble(s3);
  364. result =number1/number2;
  365. txt2.setText(""+result);
  366. }
  367. else if(s1.charAt(i)=='^') //做负数开正方、负数开负方的计算部分
  368. {
  369. place=i;
  370. for(i=0;i<place;i++)
  371. {
  372. s2=s2+s1.charAt(i);
  373. }
  374. for(i=place+1;i<s1.length();i++)
  375. {
  376. s3=s3+s1.charAt(i);
  377. }
  378. double number1=Double.parseDouble(s2);
  379. double number2=Double.parseDouble(s3);
  380. result =Math.pow(number1, number2);
  381. if(number2>=0)
  382. txt2.setText(""+result);
  383. else if(number2<0)
  384. {
  385. txt2.setText("-"+result);
  386. }
  387. }
  388. }
  389.  
  390. }
  391. else if(s1.charAt(i)=='*') //做正数乘正数、正数乘负数的计算部分
  392. {
  393. place=i;
  394. for(i=0;i<place;i++)
  395. {
  396. s2=s2+s1.charAt(i);
  397. }
  398. for(i=place+1;i<s1.length();i++)
  399. {
  400. s3=s3+s1.charAt(i);
  401. }
  402. double number1=Double.parseDouble(s2);
  403. double number2=Double.parseDouble(s3);
  404. result =number1*number2;
  405. txt2.setText(""+result);
  406. }
  407.  
  408. else if(s1.charAt(i)=='/') //做正数除正数、正数除负数的计算部分
  409. {
  410. place=i;
  411. for(i=0;i<place;i++)
  412. {
  413. s2=s2+s1.charAt(i);
  414. }
  415. for(i=place+1;i<s1.length();i++)
  416. {
  417. s3=s3+s1.charAt(i);
  418. }
  419. double number1=Double.parseDouble(s2);
  420. double number2=Double.parseDouble(s3);
  421. result =number1/number2;
  422. txt2.setText(""+result);
  423. }
  424.  
  425. else if(s1.charAt(i)=='√') //做开方运算的部分
  426. {
  427. place=i;
  428. if(s1.charAt(0)!='√' && s1.charAt(i)!='-')
  429. {
  430. for(i=0;i<place;i++)
  431. {
  432. s2=s2+s1.charAt(i);
  433. }
  434. for(i=place+1;i<s1.length();i++)
  435. {
  436. s3=s3+s1.charAt(i);
  437. }
  438. double number1=Double.parseDouble(s2);
  439. double number2=Double.parseDouble(s3);
  440. result =Math.pow(number2,(1.0/number1)); //开方运算的库函数Math.pow(double x);
  441. txt2.setText(""+result);
  442. }
  443. else if(s1.charAt(0)=='√')
  444. {
  445. for(i=place+1;i<s1.length();i++)
  446. {
  447. s3=s3+s1.charAt(i);
  448. }
  449. double number2=Double.parseDouble(s3);
  450. result =Math.sqrt(number2); //开方运算的库函数Math.sqrt(double x);
  451. txt2.setText(""+result);
  452. }
  453. else if(s1.charAt(i)=='-')
  454. {
  455. txt2.setText("无效输入");
  456. }
  457. }
  458. else if(s1.charAt(i)=='√' && i!=0)
  459. {
  460. place=i;
  461. for(i=0;i<place;i++)
  462. {
  463. s2=s2+s1.charAt(i);
  464. }
  465. for(i=place+1;i<s1.length();i++)
  466. {
  467. s3=s3+s1.charAt(i);
  468. }
  469. double number1=Double.parseDouble(s2);
  470. double number2=Double.parseDouble(s3);
  471. System.out.println(""+number1+number2);
  472. result =Math.pow(number2,(1.0/number1)); //开方运算的库函数Math.pow(double x);
  473. txt2.setText(""+result);
  474. }
  475. else if(s1.charAt(i)=='n') //找到sin位置
  476. {
  477. place=i;
  478. for(i=place+1;i<s1.length();i++)
  479. {
  480. s2=s2+s1.charAt(i); //将n后的数字相加赋给s1
  481. }
  482. txt1.setText(s1+"°");
  483. int a=Integer.parseInt(s2); //将字符串强制转换成整形
  484. txt2.setText(""+Math.sin(Math.toRadians(a))); //sin运算的库函数Math.sin(int x);
  485. //转换角度的库函数Math.toRadians(int x);
  486. }
  487. else if(s1.charAt(i)=='o') //找到cos位置
  488. {
  489. place=i;
  490. for(i=place+2;i<s1.length();i++)
  491. {
  492. s2=s2+s1.charAt(i);
  493. }
  494. txt1.setText(s1+"°");
  495. int b=Integer.parseInt(s2);
  496. txt2.setText(""+Math.cos(Math.toRadians(b))); //cos运算的库函数Math.cos(int x);
  497. }
  498. else if(s1.charAt(i)=='a') //找到tan位置
  499. {
  500. place=i;
  501. for(i=place+2;i<s1.length();i++)
  502. {
  503. s2=s2+s1.charAt(i);
  504. }
  505. txt1.setText(s1+"°");
  506. double c=Double.parseDouble(s2);
  507. txt2.setText(""+Math.tan(Math.toRadians(c))); //tan运算的库函数Math.tan(int x);
  508. }
  509. else if(s1.charAt(0)=='^') //判断是否第一个字符是^
  510. {
  511. txt2.setText("无效输入");
  512. }
  513. else if(i!=0 && s1.charAt(i)=='^') //判断是否有^
  514. {
  515. place=i;
  516. for(i=place-1;i<place;i++)
  517. {
  518. s2=s2+s1.charAt(i);
  519. }
  520. for(i=place+1;i<s1.length();i++)
  521. {
  522. s3=s3+s1.charAt(i);
  523. }
  524. double number1=Double.parseDouble(s2);
  525. double number2=Double.parseDouble(s3);
  526. txt2.setText(""+Math.pow(number1, number2)); //做x的y次方的库函数Math.pow(double a,double b);
  527. }
  528. else if(s1.charAt(0)=='.')
  529. {
  530. txt2.setText("无效输入");
  531. }
  532. else if(s1.charAt(0)=='%')
  533. {
  534. txt2.setText("无效输入");
  535. }
  536. else if(i!=0 && s1.charAt(i)=='%') //判断是否有%
  537. {
  538. place=i;
  539. for(i=0;i<place;i++)
  540. {
  541. s2=s2+s1.charAt(i);
  542. }
  543. for(i=place+1;i<s1.length();i++)
  544. {
  545. s3=s3+s1.charAt(i);
  546. }
  547. double number1=Double.parseDouble(s2);
  548. double number2=Double.parseDouble(s3);
  549. txt2.setText(""+(number1%number2)); //求余运算
  550. }
  551.  
  552. }
  553.  
  554. }
  555. else if(e.getSource()==but[28])
  556. {
  557. String s1=txt1.getText();
  558. s1="sin"+s1; //添加sin
  559. txt1.setText(s1);
  560. }
  561. else if(e.getSource()==but[29])
  562. {
  563. String s1=txt1.getText();
  564. s1="cos"+s1; //添加cos
  565. txt1.setText(s1);
  566. }
  567. else if(e.getSource()==but[30])
  568. {
  569. String s1=txt1.getText();
  570. s1="tan"+s1; //添加tan
  571. txt1.setText(s1);
  572. }
  573. else if(e.getSource()==but[31])
  574. {
  575. String s1=txt1.getText();
  576. double number1=Double.parseDouble(s1);
  577. s1=s1+"^2";
  578. txt1.setText(s1);
  579. number1=number1*number1;
  580. txt2.setText(""+number1); //做平方运算的部分
  581. }
  582. else if(e.getSource()==but[32])
  583. {
  584. String s1=txt1.getText();
  585. double number2=Double.parseDouble(s1);
  586. s1=s1+"^3";
  587. txt2.setText(""+Math.pow(number2,3)); //做三次方运算的部分
  588. }
  589. else if(e.getSource()==but[33])
  590. {
  591. String s1=txt1.getText(); //添加^
  592. s1=s1+'^';
  593. txt1.setText(s1);
  594.  
  595. }
  596. else if(e.getSource()==but[34])
  597. {
  598. String s1=txt1.getText();
  599. String s2="";
  600. int number1=Integer.parseInt(s1);
  601. for(i=number1;i>0;i--)
  602. {
  603. s2=s2+"*"+i;
  604. }
  605. for(i=number1-1;i>0;i--) //做阶乘运算的部分
  606. {
  607. number1*=i;
  608. }
  609. txt1.setText(s2);
  610. txt2.setText(""+number1);
  611. }
  612. else if(e.getSource()==but[35])
  613. {
  614. String s1=txt1.getText();
  615. double number1=Double.parseDouble(s1);
  616. txt2.setText(""+Math.pow(number1, 1.0/3)); //做立方根运算的部分
  617. }
  618. else if(e.getSource()==but[36])
  619. {
  620. String s1=txt1.getText();
  621. s1=s1+'√';
  622. txt1.setText(s1);
  623. }
  624.  
  625.  
  626. else if(e.getSource()==but[37])
  627. {
  628. String s1=txt1.getText();
  629. double number1=Double.parseDouble(s1);
  630. s1="log"+s1;
  631. txt1.setText(s1);
  632. txt2.setText(""+Math.log(number1)); //做log运算的部分
  633. }
  634. else if(e.getSource()==but[38])
  635. {
  636. String s1=txt1.getText();
  637. double number1=Double.parseDouble(s1);
  638. s1="10^"+s1;
  639. txt1.setText(s1);
  640. txt2.setText(""+Math.pow(10, number1)); //做10的X次方运算的部分
  641. }
  642. else if(e.getSource()==but[39])
  643. {
  644. String s1=txt1.getText();
  645. double number1=Double.parseDouble(s1);
  646. s1="ln"+s1;
  647. txt1.setText(s1);
  648. txt2.setText(""+Math.abs(Math.log(1.0/number1))); //做ln运算的部分
  649. }
  650. else if(e.getSource()==but[40])
  651. {
  652. String s1=txt1.getText();
  653. double number1=Double.parseDouble(s1);
  654. s1="e^"+s1;
  655. txt1.setText(s1);
  656. txt2.setText(""+Math.pow(E, number1)); //做e的x次方运算的部分
  657. }
  658. else if(e.getSource()==but[41])
  659. {
  660. String s1="";
  661. s1=s1+"π";
  662. txt1.setText(s1);
  663. txt2.setText(""+π); //显示π的值
  664. }
  665. else if(e.getSource()==but[42])
  666. {
  667.  
  668. String s1=txt1.getText();
  669. double number1=Double.parseDouble(s1);
  670. number1=number1*(-1);
  671. s1=s1+"*-1";
  672. txt1.setText(s1);
  673. txt2.setText(""+number1); //做相反数运算的部分
  674. }
  675. else if(e.getSource()==but[43])
  676. {
  677.  
  678. String s1=txt1.getText();
  679. int number1=Integer.parseInt(s1);
  680. txt2.setText(""+Integer.toHexString(number1)); //做转换为16进制数的库函数Integer.toHexString(int x);
  681. }
  682. else if(e.getSource()==but[44])
  683. {
  684.  
  685. String s1=txt1.getText();
  686. int number1=Integer.parseInt(s1);
  687. txt2.setText(""+Integer.valueOf(number1)); //做转换为10进制数的库函数Integer.valueOf(int x);
  688. }
  689. else if(e.getSource()==but[45])
  690. {
  691.  
  692. String s1=txt1.getText();
  693. int number1=Integer.parseInt(s1);
  694. txt2.setText(""+Integer.toOctalString(number1)); //做转换为8进制数的库函数Integer.toOctalString(int x);
  695. }
  696. else if(e.getSource()==but[46])
  697. {
  698.  
  699. String s1=txt1.getText();
  700. int number1=Integer.parseInt(s1);
  701. txt2.setText(""+Integer.toBinaryString(number1)); //做转换为2进制数的库函数Integer.toBinaryString(int x);
  702. }
  703. else if(e.getSource()==but[47])
  704. {
  705. String s1=txt1.getText();
  706. double number1=Integer.parseInt(s1);
  707. s1="(e*"+s1+"-e*(-1*"+s1+"))/2";
  708. txt1.setText(s1);
  709. txt2.setText(""+ (Math.pow(E, number1)-Math.pow(E, -1*number1))/2); //做双曲正弦运算的部分
  710. }
  711. else if(e.getSource()==but[48])
  712. {
  713. String s1=txt1.getText();
  714. double number1=Integer.parseInt(s1);
  715. s1="(e*"+s1+"+e*(-1*"+s1+"))/2";
  716. txt1.setText(s1);
  717. txt2.setText(""+ (Math.pow(E, number1)+Math.pow(E, -1*number1))/2); //做双曲余弦运算的部分
  718. }
  719. else if(e.getSource()==but[49])
  720. {
  721. String s1=txt1.getText();
  722. double number1=Integer.parseInt(s1);
  723. s1="((e*"+s1+ "-e*(-1*" +s1+ "))/2)" +"/"+"((e*"+s1+"+e*(-1*"+s1+"))/2)";
  724. txt1.setText(s1);
  725. txt2.setText(""+ ((Math.pow(E, number1)-Math.pow(E, -1*number1))/2)
  726. /((Math.pow(E, number1)+Math.pow(E, -1*number1))/2)); //做双曲正切运算的部分
  727. }
  728. else if(e.getSource()==but[50]) //做求整运算的部分
  729. {
  730. int place;
  731. String s1=txt1.getText();
  732. String s2="";
  733. for(i=0;i<s1.length()-1;i++)
  734. {
  735. if(s1.charAt(i)=='.') //判断小数点的位置
  736. {
  737. place=i;
  738. for(i=0;i<place;i++)
  739. {
  740. s2=s2+s1.charAt(i); //取小数点之前的内容
  741. }
  742. int number1=Integer.parseInt(s2);
  743. txt2.setText(""+number1);
  744. }
  745. }
  746. }
  747. else if(e.getSource()==but[51]) //做累加运算的部分
  748. {
  749. String s1=txt1.getText();
  750. String s2="";
  751. int number1=Integer.parseInt(s1);
  752. int begin=number1; //做累加运算的起始值
  753. for(i=number1;i>0;i--)
  754. {
  755. s2=s2+"+"+i;
  756. }
  757. for(i=number1-1;i>0;i--)
  758. {
  759. number1+=i; //累加运算
  760. }
  761. txt1.setText(s2);
  762. txt2.setText(""+number1);
  763. }
  764. else if(e.getSource()==jmm1)
  765. {
  766. System.exit(0);
  767. }
  768. else if(e.getSource()==jmm2)
  769. {
  770. JFrame help=new JFrame("关于计算器");
  771. help.setSize(660,280);
  772. help.setLayout(new FlowLayout(FlowLayout.LEADING,0,0));
  773. String path = "D://Background.jpg"; //背景图片//
  774. ImageIcon Imagebackground = new ImageIcon(path); //把背景图片显示在一个标签里面//
  775. JLabel Imagelabel = new JLabel(Imagebackground); //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明//
  776. JPanel imagePanel = (JPanel) help.getContentPane();
  777. Imagelabel.setBounds(0, 0, help.getWidth(), help.getHeight()); //把标签的大小位置设置为图片刚好填充整个面板 //
  778. imagePanel.setOpaque(false); //把背景图片添加到分层窗格的最底层作为背景 //
  779. help.getLayeredPane().add(Imagelabel, new Integer(Integer.MIN_VALUE));
  780. JLabel h1=new JLabel("该计算机由王基书和程仿仿编写,是一个多功能的科学计算器,功能齐全,可以进行两个数之间的加减乘除和各类运算");
  781. JLabel h2=new JLabel("编写时间:2016年12月27日");
  782. JLabel h3=new JLabel("编写地点:昆明学院惟实楼3402-2");
  783. help.add(h1);
  784. help.add(h2);
  785. help.add(h3);
  786. help.setLocationRelativeTo(null);
  787. help.setVisible(true);
  788. }
  789. else if(e.getSource()==jmm3)
  790. {
  791. if (!Desktop.isDesktopSupported())
  792. {
  793. System.err.println("Desktop not supported!");
  794. System.exit(-1);
  795. }
  796. else
  797. {
  798. try{
  799. Desktop desktop = Desktop.getDesktop();
  800. File f=new File("Jisuanqi.txt");
  801. desktop.open(f);
  802. }
  803. catch(Exception e1){System.out.println(e1.getMessage());}
  804. }
  805. }
  806. }
  807.  
  808. private char charat(int j) {
  809. // TODO 自动生成的方法存根
  810. return 0;
  811. }
  812. public void Set()
  813. {
  814. for(i=1;i<4;i++)
  815. {
  816. but[i].setForeground(Color.red); //修改字体颜色
  817. but[i].setFont(new Font("黑体", ABORT, 15)); //修改字体大小和类型
  818. }
  819. for(i=4;i<10;i+=5)
  820. {
  821. but[i].setForeground(Color.red);
  822. }
  823. for(i=10;i<16;i+=5)
  824. {
  825. but[i].setForeground(Color.red);
  826. }
  827. for(i=16;i<22;i+=5)
  828. {
  829. but[i].setForeground(Color.red);
  830. }
  831. for(i=22;i<28;i+=5)
  832. {
  833. but[i].setForeground(Color.red);
  834. }
  835. for(i=8;i<27;i+=6) //使用for循环修改运算符的属性
  836. {
  837. but[i].setForeground(Color.red);
  838. but[i].setFont(new Font("黑体", ABORT, 15));
  839. }
  840. for(i=5;i<8;i++)
  841. {
  842. but[i].setForeground(Color.BLUE);
  843. }
  844. for(i=11;i<14;i++)
  845. {
  846. but[i].setForeground(Color.BLUE);
  847. }
  848. for(i=17;i<20;i++)
  849. {
  850. but[i].setForeground(Color.BLUE);
  851. }
  852. for(i=23;i<26;i++)
  853. {
  854. but[i].setForeground(Color.BLUE);
  855. }
  856. for(i=28;i<52;i++)
  857. {
  858. but[i].setForeground(Color.BLACK);
  859. }
  860. }
  861. public void Add()
  862. {
  863. this.add(jmb); //在 窗体中添加菜单行//
  864. jmb.add(jm1); //在菜单行中添加菜单栏//
  865. jmb.add(jm2); //-------------//
  866. jmb.add(jm3); //-------------//
  867. jm1.add(jmm1); //在菜单栏中添加菜单项//
  868. jm3.add(jmm2);
  869. jm2.add(jmm3);
  870. this.add(p1);
  871. p1.add(lab1);
  872. p1.add(txt1);
  873. p1.add(lab2);
  874. p1.add(txt2);
  875. //txt2.setEditable(false); //将txt2设置为不可以编辑文本//
  876. this.add(p2);
  877. p2.add(but[0]); //面板p2添加按钮//
  878. p2.add(but[1]); //----------//
  879. p2.add(but[2]); //----------//
  880. p2.add(but[3]); //----------//
  881. this.add(p3);
  882. for(i=4;i<52;i++) //使用for循环在面板p3中添加按钮数组but[i]//
  883. { //----------------------------//
  884. p3.add(but[i]); //----------------------------//
  885. } //----------------------------//
  886. }
  887. }
  888.  
  889.  

现在回头看,这个程序的代码还可以再优化100-200行,许多重复的地方可以编写方法来实现,而且计算器的功能也不够,只能实现两个数之间的四则运算和一个数的各种运算。

主要实现的功能:

1.可以点击鼠标或者键盘进行操作。

2.优化了界面,使布局尽量清晰。

效果如下:

界面效果

评论板

共有 8 条评论

  1. Abupguele

    He made double bogey on the final hole and still had a 65 that matched the best score of the tournament, and the low round of the day by two shots buying cheap cialis online Tamoxifen blood levels following initial and chronic dosing in patients with breast cancer Abstract

  2. Outlils

    tadalafil generic vs cialis Dernovsek, Neven Henigsberg, Daniel Souery, Annamaria Cattaneo, Joanna Hauser, Ole Mors, Marcella Rietschel, Gerald Pfeffer, Chad Bousman, Katherine J

  3. Gealpaste

    On the other hand, when these mice were subjected to the acetic acid induced writhing test, they showed markedly decreased responses compared with control wild type mice, and their responses were as low as those observed in control mice treated with indomethacin is viagra bad for Although the consequences of these compensatory mechanisms are still subject to debate, it appears that the overall genetic background may determine whether a CREBО±Оґ mutant mouse has memory deficits

  4. invoimi

    Zoe BoZQfqeRWIs 5 20 2022 cialis for sale in usa Hickey M et al 2009 Breast cancer in young women and its impact on reproductive function Hum Reprod Update 15 3 323 39 DOI 10

  5. invoimi

    Be careful and be sure to specify the information on the section Dosage Posology and method of administration in the instructions to the drug Tamoxifen Ebewe directly from the package or from the pharmacist at the pharmacy what is priligy dapoxetine It is not clear from our data whether subsequent cancers that were diagnosed were missed by initial investigation even in those with a biopsy or new pathology

  6. intuido

    cialis valtrex et prise de poids Binz said he was fully supportive of developing thenation s gas reserves, added that FERC was not responsible forclimate policy and that he had not spoken with the White Houseabout Obama s climate change plan, which was laid out in June best place to buy generic cialis online viagra avapro 150 mg tab Hasan admitted to the shooting, claiming he carried it out to defend members of the Taliban from an illegal war

  7. HcRUoX

    canadian pharmacy cialis It has not been determined whether the rates of fracture and osteoporosis seen in ATAC in patients on Amenur treatment reflect a protective effect of tamoxifen, a specific effect of Amenur, or both

  8. syaUnrB

    Because Magnesium silicate is primarily eliminated by the kidney, there is significant risk of hypermagnesemia in patients with renal dysfunction finasteride tablets 5mg where to buy A variety of procedures, including maxillary antrostomy, uncinate takedown, ethmoidectomy, Draf I III, and or saline washout, can be performed depending on the location and severity of the individual patient s pathology

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:449696