当前位置:   article > 正文

Java界面编程之复杂计算器_java实现复杂计算器界面及功能

java实现复杂计算器界面及功能

这是上学期Java的期末设计啦,给大家互相借鉴下,加上搜集资料和自己的理解整合处来的一个多功能计算器,

下面看图片及代码,还有有什么问题可以留言哦,

嗯,这里一共分为五个类来写,第一个来类是JSQ是主界面,然后剩下的类都是功能类

  1. package qm;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. public class JSQ extends Frame implements ActionListener, WindowListener
  6. {
  7. private Container container;
  8. private GridBagLayout layout;
  9. private GridBagConstraints constraints;
  10. private JTextField displayField; //计算结果显示区
  11. private String lastCommand; //保存+,-,*,/,=命令0
  12. private double result; //保存计算结果
  13. private boolean start; //判断是否为数字的开始
  14. private JMenuBar menubar;
  15. private JMenuItem m_exit;
  16. private JMenuItem m2_ejz;
  17. private JMenuItem m2_bjz;
  18. private Dialog dialog;
  19. private Label label_dialog;
  20. private JButton button_sqrt;
  21. private JButton button_plusminus;
  22. private JButton button_CE;
  23. private JButton button_cancel;
  24. private JButton button_1;
  25. private JButton button_2;
  26. private JButton button_3;
  27. private JButton button_4;
  28. private JButton button_5;
  29. private JButton button_6;
  30. private JButton button_7;
  31. private JButton button_8;
  32. private JButton button_9;
  33. private JButton button_0;
  34. private JButton button_plus;
  35. private JButton button_minus;
  36. private JButton button_multiply;
  37. private JButton button_divide;
  38. private JButton button_point;
  39. private JButton button_equal;
  40. private JButton button_log;
  41. private JButton button_tan;
  42. private JButton button_cos;
  43. private JButton button_sin;
  44. private JButton button_exp;
  45. public JSQ() //构造方法设置布局、为按钮注册事件监听器
  46. {
  47. super( "科学计算器" );
  48. this.setLocation( 300,200 );
  49. this.setSize( 328,424);
  50. this.setResizable( true );
  51. this.setLayout( new GridLayout( 7,1 ) );//网格布局
  52. this.addmyMenu(); //调用成员方法添加菜单
  53. displayField = new JTextField( 30 );
  54. displayField.setBackground(new Color(233,240,247));
  55. displayField.setForeground(new Color(30,57,91));
  56. // 设置outputField内的默认内容,应该显示为“0
  57. displayField.setText("0");
  58. displayField.setFont(new Font("宋体",Font.BOLD,25));
  59. // 禁止从界面(键盘)向outputField输入信息,其内容只能通过程序内部改变
  60. displayField.setEditable(false);
  61. this.setResizable(false);
  62. // displayField.setBackground(Color.blue);
  63. this.add( displayField );
  64. //displayField.setEditable( true );
  65. start = true;
  66. result = 0;
  67. lastCommand = "=";
  68. JPanel panel0 = new JPanel();
  69. panel0.setLayout( new GridLayout( 1,4,4,4 ) );
  70. JPanel panel1 = new JPanel();
  71. panel1.setLayout( new GridLayout( 1,5,4,4 ) );
  72. this.add( panel1 );
  73. button_sqrt = new JButton( "sqrt" );//根号
  74. button_sqrt.setBackground(new Color(233,240,247));
  75. //button_sqrt.setForeground(new Color(30,57,91));
  76. // button_sqrt.setBackground(Color.blue);
  77. button_plusminus = new JButton( "+/-" );
  78. button_plusminus.setBackground(new Color(233,240,247));
  79. //button_plusminus.setForeground(new Color(30,57,91));
  80. //button_plusminus.setBackground(Color.blue);
  81. button_exp = new JButton( "exp" );//底数e的n次幂
  82. button_exp.setBackground(new Color(233,240,247));
  83. //button_exp.setForeground(new Color(30,57,91));
  84. // button_exp.setBackground(Color.blue);
  85. button_CE = new JButton( "退位");
  86. button_CE.setBackground(new Color(233,240,247));
  87. button_CE.setForeground(new Color(30,57,91));
  88. // button_CE.setBackground(Color.blue);
  89. button_cancel = new JButton( "CE" );//清除
  90. button_cancel.setBackground(new Color(233,240,247));
  91. button_cancel.setForeground(new Color(30,57,91));
  92. // button_cancel.setBackground(Color.blue);
  93. JPanel panel2 = new JPanel();
  94. panel2.setLayout( new GridLayout( 1,5,4,4 ) );
  95. this.add( panel2 );
  96. button_7 = new JButton( "7" );
  97. button_7.setBackground(new Color(233,240,247));
  98. button_7.setForeground(new Color(30,57,91));
  99. // button_7.setBackground(Color.blue);
  100. button_8 = new JButton( "8" );
  101. button_8.setBackground(new Color(233,240,247));
  102. button_8.setForeground(new Color(30,57,91));
  103. // button_8.setBackground(Color.blue);
  104. button_9 = new JButton( "9" );
  105. button_9.setBackground(new Color(233,240,247));
  106. button_9.setForeground(new Color(30,57,91));
  107. // button_9.setBackground(Color.blue);
  108. button_log = new JButton( "log" );//对数
  109. button_log.setBackground(new Color(233,240,247));
  110. button_log.setForeground(new Color(30,57,91));
  111. // button_log.setBackground(Color.blue);
  112. button_divide = new JButton( "/" );//
  113. button_divide.setBackground(new Color(233,240,247));
  114. button_divide.setForeground(new Color(30,57,91));
  115. // button_divide.setBackground(Color.blue);
  116. JPanel panel3 = new JPanel();
  117. panel3.setLayout( new GridLayout(1,5,4,4) );
  118. this.add( panel3 );
  119. button_4 = new JButton( "4" );
  120. button_4.setBackground(new Color(233,240,247));
  121. button_4.setForeground(new Color(30,57,91));
  122. // button_4.setBackground(Color.blue);
  123. button_5 = new JButton( "5" );
  124. button_5.setBackground(new Color(233,240,247));
  125. button_5.setForeground(new Color(30,57,91));
  126. // button_5.setBackground(Color.blue);
  127. button_6 = new JButton( "6" );
  128. button_6.setBackground(new Color(233,240,247));
  129. button_6.setForeground(new Color(30,57,91));
  130. // button_6.setBackground(Color.blue);
  131. button_tan = new JButton( "tan" );//正切
  132. button_tan.setBackground(new Color(233,240,247));
  133. button_tan.setForeground(new Color(30,57,91));
  134. //.setBackground(Color.blue);
  135. button_multiply = new JButton( "*" );//乘法
  136. button_multiply.setBackground(new Color(233,240,247));
  137. button_multiply.setForeground(new Color(30,57,91));
  138. // button_multiply.setBackground(Color.blue);
  139. JPanel panel4=new JPanel();
  140. panel4.setLayout( new GridLayout( 1,5,4,4 ) );
  141. this.add(panel4);
  142. button_1 = new JButton( "1" );
  143. button_1.setBackground(new Color(233,240,247));
  144. button_1.setForeground(new Color(30,57,91));
  145. // button_1.setBackground(Color.blue);
  146. button_2 = new JButton( "2" );
  147. button_2.setBackground(new Color(233,240,247));
  148. button_2.setForeground(new Color(30,57,91));
  149. // button_2.setBackground(Color.blue);
  150. button_3 = new JButton( "3" );
  151. button_3.setBackground(new Color(233,240,247));
  152. button_3.setForeground(new Color(30,57,91));
  153. // button_3.setBackground(Color.blue);
  154. button_cos = new JButton( "cos");//余弦
  155. button_cos.setBackground(new Color(233,240,247));
  156. button_cos.setForeground(new Color(30,57,91));
  157. // button_cos.setBackground(Color.blue);
  158. button_minus = new JButton( "-" );
  159. button_minus.setBackground(new Color(233,240,247));
  160. button_minus.setForeground(new Color(30,57,91));
  161. // button_minus.setBackground(Color.blue);
  162. JPanel panel5 = new JPanel();
  163. panel5.setLayout( new GridLayout( 1,5,4,4 ) );
  164. this.add( panel5 );
  165. button_point=new JButton( "." );
  166. button_point.setBackground(new Color(233,240,247));
  167. button_point.setForeground(new Color(30,57,91));
  168. button_0 = new JButton( "0" );
  169. button_0.setBackground(new Color(233,240,247));
  170. button_0.setForeground(new Color(30,57,91));
  171. // button_0.setBackground(Color.blue);
  172. // button_point=new JButton( "." );
  173. // button_point.setBackground(new Color(233,240,247));
  174. // button_point.setForeground(new Color(30,57,91));
  175. //button_point.setBackground(Color.blue);
  176. button_equal = new JButton( "=" );
  177. button_equal.setBackground(new Color(233,240,247));
  178. button_equal.setForeground(new Color(30,57,91));
  179. // button_equal.setBackground(Color.blue);
  180. button_sin = new JButton( "sin" );//正弦
  181. button_sin.setBackground(new Color(233,240,247));
  182. button_sin.setForeground(new Color(30,57,91));
  183. // button_sin.setBackground(Color.blue);
  184. button_plus = new JButton( "+" );
  185. button_plus.setBackground(new Color(233,240,247));
  186. button_plus.setForeground(new Color(30,57,91));
  187. // button_plus.setBackground(Color.blue);
  188. panel1.add( button_sqrt );
  189. panel1.add( button_plusminus );
  190. panel1.add( button_exp );
  191. panel1.add( button_CE );
  192. panel1.add( button_cancel );
  193. panel2.add( button_7 );
  194. panel2.add( button_8 );
  195. panel2.add( button_9 );
  196. panel2.add( button_log );
  197. panel2.add( button_divide );
  198. panel3.add( button_4 );
  199. panel3.add( button_5 );
  200. panel3.add( button_6 );
  201. panel3.add( button_tan );
  202. panel3.add( button_multiply );
  203. panel4.add( button_1 );
  204. panel4.add( button_2 );
  205. panel4.add( button_3 );
  206. panel4.add( button_cos );
  207. panel4.add( button_minus );
  208. panel5.add( button_point );
  209. panel5.add( button_0 );
  210. // panel5.add( button_point );
  211. panel5.add( button_equal );
  212. panel5.add( button_sin );
  213. panel5.add( button_plus) ;
  214. button_sqrt.addActionListener( this );
  215. button_plusminus.addActionListener( this );
  216. button_exp.addActionListener( this );
  217. button_CE.addActionListener( this );
  218. button_cancel.addActionListener( this );
  219. button_7.addActionListener( this );
  220. button_8.addActionListener( this );
  221. button_9.addActionListener( this );
  222. button_log.addActionListener( this );
  223. button_divide.addActionListener( this );
  224. button_4.addActionListener( this );
  225. button_5.addActionListener( this );
  226. button_6.addActionListener( this );
  227. button_tan.addActionListener( this );
  228. button_multiply.addActionListener( this );
  229. button_1.addActionListener( this );
  230. button_2.addActionListener( this );
  231. button_3.addActionListener( this );
  232. button_cos.addActionListener( this );
  233. button_minus.addActionListener( this );
  234. button_0.addActionListener( this );
  235. button_point.addActionListener( this );
  236. button_equal.addActionListener( this );
  237. button_sin.addActionListener( this );
  238. button_plus.addActionListener( this );
  239. this.addWindowListener( new WinClose() ); //注册窗口监听器
  240. this.setVisible( true );
  241. }
  242. private void addmyMenu() //菜单的添加
  243. {
  244. JMenuBar menubar = new JMenuBar();
  245. this.add( menubar );
  246. JMenu m1 = new JMenu( "查看" );
  247. JMenu m2 = new JMenu( "进制转换" );
  248. JMenuItem m1_standardcalculator=new JMenuItem("标准型计算器");
  249. m1_standardcalculator.addActionListener(this);
  250. JMenuItem m1_exit = new JMenuItem( "退出" );
  251. m1_exit.addActionListener( this );
  252. JMenuItem m2_ejz = new JMenuItem( "二进制" );
  253. m2_ejz.addActionListener( this );
  254. JMenuItem m2_ternary=new JMenuItem("三进制");
  255. m2_ternary.addActionListener(this);
  256. JMenuItem m2_bjz = new JMenuItem("八进制");
  257. m2_bjz.addActionListener( this );
  258. JMenuItem m2_sljz = new JMenuItem("十六进制");
  259. m2_sljz.addActionListener( this );
  260. JMenu m3 = new JMenu( "帮助" );
  261. JMenuItem m3_Help = new JMenuItem( "用法" );
  262. m3_Help.addActionListener( this );
  263. // dialog = new Dialog( this, "提示" , true ); //模式窗口
  264. // dialog.setSize( 240,80 );
  265. //label_dialog = new Label("", Label.CENTER ); //标签的字符串为空,居中对齐
  266. // dialog.add( label_dialog );
  267. //dialog.addWindowListener( this ); //为对话框注册窗口事件监听器
  268. JMenu m4 =new JMenu("关于");
  269. JMenuItem m4_developer=new JMenuItem("开发者");
  270. m4_developer.addActionListener(this);
  271. // dialog =new Dialog(this,"",true);
  272. // dialog.setSize(380,100);
  273. // label_dialog =new Label("",Label.CENTER);
  274. // dialog.add(label_dialog);
  275. //dialog.addWindowListener(this);
  276. JMenuItem m4_developmentTime=new JMenuItem("开发时间");
  277. m4_developmentTime.addActionListener(this);
  278. dialog =new Dialog(this,"",true); //模式窗口
  279. dialog.setSize(380,100);
  280. label_dialog =new Label("",Label.CENTER); //标签的字符串为空,居中对齐
  281. dialog.add(label_dialog);
  282. dialog.addWindowListener(this);//为对话框注册窗口事件监听器
  283. JMenuItem m5_unitconversion = new JMenuItem( "单位转换器" );
  284. m5_unitconversion .addActionListener( this );
  285. JMenuItem m6_Temperature = new JMenuItem( "温度转换器" );
  286. m6_Temperature.addActionListener( this );
  287. m6_Temperature.setBackground(new Color(233,240,247));
  288. m6_Temperature.setForeground(new Color(30,57,91));
  289. m6_Temperature.setBackground(new Color(233,240,247));
  290. m6_Temperature.setForeground(new Color(30,57,91));
  291. m6_Temperature.setBackground(new Color(233,240,247));
  292. m6_Temperature.setForeground(new Color(30,57,91));
  293. m1.add(m1_standardcalculator);
  294. m1.add(m5_unitconversion);
  295. m1.add(m6_Temperature);
  296. m1.add( m1_exit );
  297. menubar.add( m1 );
  298. m2.add( m2_ejz );
  299. m2.add(m2_ternary);
  300. m2.add( m2_bjz );
  301. m2.add( m2_sljz );
  302. menubar.add( m2 );
  303. m3.add( m3_Help );
  304. menubar.add( m3 );
  305. m4.add(m4_developer);
  306. m4.add(m4_developmentTime);
  307. menubar.add(m4);
  308. m6_Temperature.setBackground(new Color(233,240,247));
  309. m6_Temperature.setForeground(new Color(30,57,91));
  310. }
  311. public void actionPerformed(ActionEvent e) //按钮的单击事件处理方法
  312. {
  313. if(
  314. e.getSource().equals( button_1 )||e.getSource().equals( button_2 )||
  315. e.getSource().equals( button_3 )||e.getSource().equals( button_4 )||
  316. e.getSource().equals( button_5 )|| e.getSource().equals( button_6 )||
  317. e.getSource().equals( button_7 )|| e.getSource().equals( button_8 )||
  318. e.getSource().equals( button_9 ) ||e.getSource().equals( button_0 )||
  319. e.getSource().equals( button_point )||e.getSource().equals( button_plusminus )||
  320. e.getSource().equals( button_cancel )||e.getSource().equals( button_CE )
  321. )
  322. { //非运算符的处理方法
  323. String input = e.getActionCommand();
  324. if ( start )
  325. {
  326. displayField.setText("");
  327. start = false;
  328. if( input.equals( "+/-" ) )
  329. displayField.setText( displayField.getText()+ "-" );
  330. }
  331. if( !input.equals( "+/-" ) )
  332. {
  333. String str = displayField.getText();
  334. if( input.equals( "退位") ) //退格键的实现方法
  335. {
  336. if( str.length() > 0 )
  337. displayField.setText( str.substring( 0,str.length() - 1 ) );
  338. }
  339. else if( input.equals( "CE" ) ) //清零键的实现方法
  340. {
  341. displayField.setText( "0");
  342. start = true;
  343. }
  344. else
  345. displayField.setText( displayField.getText() + input );
  346. }
  347. }
  348. else if ( e.getActionCommand()=="二进制") //二进制的转换
  349. {
  350. int n = Integer.parseInt( displayField.getText() );
  351. displayField.setText(Integer.toBinaryString(n));
  352. //displayField.setText( Integer.toBinaryString( n ) );
  353. //SysConvert2 a=new SysConvert2();
  354. //a.main(null);
  355. //SysConvert2 convert = new SysConvert2();
  356. //convert.setVisible(true);
  357. //this.dispose();
  358. }
  359. else if(e.getActionCommand()=="三进制")//三进制的转换
  360. {
  361. int n=Integer.parseInt(displayField.getText());
  362. displayField.setText(Integer.toString(n,3));
  363. }
  364. else if ( e.getActionCommand() == "八进制" ) //八进制的转换
  365. {
  366. int n = Integer.parseInt( displayField.getText() );
  367. displayField.setText( Integer.toOctalString( n ) );
  368. //displayField.setText(Integer.toBinaryString(n));
  369. }
  370. else if ( e.getActionCommand() == "十六进制" ) //十六进制的转换
  371. {
  372. int n = Integer.parseInt( displayField.getText() );
  373. displayField.setText( Integer.toHexString( n ) );
  374. }
  375. else if(e.getActionCommand()=="标准型计算器")
  376. {
  377. this.dispose();
  378. StandardCalculator sc=new StandardCalculator();
  379. sc.main(null);
  380. }
  381. else if(e.getActionCommand()=="单位转换器")
  382. {
  383. this.dispose();
  384. UnitTransfer un=new UnitTransfer();
  385. un.main(null);
  386. }
  387. else if(e.getActionCommand()=="温度转换器")
  388. {
  389. WenDuChang wd=new WenDuChang();
  390. this.dispose();
  391. }
  392. else if ( e.getActionCommand() == "退出" ) //选项中退出的处理方法
  393. {
  394. System.exit( 0 );
  395. }
  396. else if ( e.getActionCommand() == "用法" ) //按下'帮助'菜单栏中用法的处理方法
  397. {
  398. label_dialog.setText( "sqrt,exp等键是先输运算符再输数字,先输入数字再点进制转换\n" );
  399. dialog.setLocation( 400,250 );
  400. dialog.setVisible( true );
  401. }
  402. else if(e.getActionCommand()=="开发者")
  403. {
  404. label_dialog.setText( "182017331,陈锦贤 182017505, 吴洪缤 182017190,王浩铸" );
  405. dialog.setLocation( 400,250 );
  406. dialog.setVisible( true );
  407. }
  408. else if(e.getActionCommand()=="开发时间")
  409. {
  410. label_dialog.setText( "开发于2019年6月17号" );
  411. dialog.setLocation( 400,250 );
  412. dialog.setVisible( true );
  413. }
  414. else //各运算符的识别
  415. {
  416. String command = e.getActionCommand();
  417. if( start )
  418. {
  419. lastCommand = command;
  420. }
  421. else
  422. {
  423. calculate( Double.parseDouble( displayField.getText() ) );
  424. lastCommand = command;
  425. start = true;
  426. }
  427. }
  428. }
  429. public void calculate( double x ) //各运算符的具体运算方法
  430. {
  431. double d = 0;
  432. if ( lastCommand.equals( "+" ) )
  433. result += x;
  434. else if (lastCommand.equals( "-" ) )
  435. result -= x;
  436. else if ( lastCommand.equals( "*" ) )
  437. result *= x;
  438. else if ( lastCommand.equals( "/" ) )
  439. result /= x;
  440. else if ( lastCommand.equals( "=" ) )
  441. result = x;
  442. else if ( lastCommand.equals( "sqrt" ) )
  443. {
  444. d = Math.sqrt( x );
  445. result = d;
  446. }
  447. else if ( lastCommand.equals( "exp" ) )
  448. {
  449. d = Math.exp( x );
  450. result = d;
  451. }
  452. else if ( lastCommand.equals( "log" ) )
  453. {
  454. d = Math.log( x );
  455. result = d;
  456. }
  457. else if ( lastCommand.equals( "tan" ) )
  458. {
  459. d = Math.tan(x);
  460. result = d;
  461. }
  462. else if ( lastCommand.equals( "cos" ) )
  463. {
  464. d = Math.cos( x );
  465. result = d;
  466. }
  467. else if ( lastCommand.equals( "sin" ) )
  468. {
  469. d = Math.sin( x );
  470. result = d;
  471. }
  472. displayField.setText( ""+ result );
  473. }
  474. public void windowClosing( WindowEvent e )
  475. {
  476. if( e.getSource() == dialog )
  477. dialog.setVisible( false ); //隐藏对话框
  478. else
  479. System.exit( 0 );
  480. }
  481. public void windowOpened( WindowEvent e ) { }
  482. public void windowActivated( WindowEvent e ) { }
  483. public void windowDeactivated( WindowEvent e ) { }
  484. public void windowClosed( WindowEvent e ) { }
  485. public void windowIconified( WindowEvent e ) { }
  486. public void windowDeiconified( WindowEvent e ) { }
  487. public static void main( String args[] )
  488. {
  489. JSQ calculator = new JSQ();
  490. }
  491. }
  492. class WinClose implements WindowListener
  493. {
  494. public void windowClosing( WindowEvent e ) //单击窗口关闭按钮时触发并执行实现窗口监听器接口中的方法
  495. {
  496. System.exit( 0 ); //结束程序运行
  497. }
  498. public void windowOpened( WindowEvent e ){ }
  499. public void windowActivated( WindowEvent e ){}
  500. public void windowDeactivated( WindowEvent e){ }
  501. public void windowClosed( WindowEvent e ){ }
  502. public void windowIconified( WindowEvent e ){ }
  503. public void windowDeiconified( WindowEvent e ){ }
  504. }
  505. //第二个类
  506. package qm;
  507. import java.awt.*;
  508. import java.awt.event.ActionEvent;
  509. import java.awt.event.ActionListener;
  510. import java.awt.event.KeyAdapter;
  511. import java.awt.event.KeyEvent;
  512. import java.awt.event.MouseAdapter;
  513. import java.awt.event.MouseEvent;
  514. import java.math.BigDecimal;
  515. import javax.swing.*;
  516. import javax.swing.border.Border;
  517. import javax.swing.border.LineBorder;
  518. public class StandardCalculator extends JFrame{
  519. private JTextField display1,display2,display3;
  520. private JButton num[],operate[];
  521. private String name[]={"MC","MR","MS","M+","M-","←","CE","C","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+"};
  522. private operateNum op1=new operateNum(),op2=new operateNum();//操作数1,操作数2
  523. private storageField storage=new storageField();//存储区
  524. private String action="op1";//表示要操作的对象 "op1"操作第一个操作数,"op2"操作第二个操作数
  525. private String sign="";//运算符,默认为空
  526. private String screen1,screen2;
  527. private boolean Disable=false;
  528. public StandardCalculator()
  529. {
  530. super("标准计算器");
  531. this.setLayout(null);
  532. this.setSize(328, 424);
  533. this.setResizable(false);
  534. this.setLocation(300,200);
  535. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  536. display1=new JTextField(30);
  537. // this.add(display1);
  538. // display1.setEditable(true);
  539. final StandardCalculator a = this;
  540. JPanel jp=new JPanel(null);
  541. //jp.setBounds(10,15,getWidth()-24,getHeight());
  542. jp.setBounds(10,15,getWidth()-24,getHeight());
  543. jp.setBackground(new Color(217,228,241));
  544. this.getContentPane().setBackground(new Color(217,228,241));
  545. /**显示屏**/
  546. display1=new JTextField("");
  547. display2=new JTextField("0");
  548. display3=new JTextField("");
  549. jp.add(display1);
  550. jp.add(display3);
  551. jp.add(display2);
  552. this.add(jp);
  553. display1.setEnabled(false);
  554. display2.setEnabled(false);
  555. display3.setEnabled(false);
  556. display1.setBounds(0, 0, 200, 34);
  557. display2.setBounds(20, 20, 180, 34);
  558. display3.setBounds(0, 20, 30, 34);
  559. display1.setHorizontalAlignment(JLabel.RIGHT);
  560. display2.setHorizontalAlignment(JLabel.RIGHT);
  561. display3.setHorizontalAlignment(JLabel.CENTER);
  562. display1.setFont(new Font("宋体",Font.PLAIN,12));
  563. display2.setFont(new Font("宋体",Font.BOLD,20));
  564. display3.setFont(new Font("宋体",Font.PLAIN,20));
  565. display1.setDisabledTextColor(Color.BLACK);
  566. display2.setDisabledTextColor(Color.BLACK);
  567. display3.setDisabledTextColor(Color.BLACK);
  568. display1.setBorder(new LineBorder(new Color(242,247,252)));
  569. display2.setBorder(new LineBorder(new Color(242,247,252)));
  570. display3.setBorder(new LineBorder(new Color(242,247,252)));
  571. display1.setBackground(new Color(242,247,252));
  572. display2.setBackground(new Color(242,247,252));
  573. display3.setBackground(new Color(242,247,252));
  574. /**按钮 35px*29px 28***/
  575. int i;
  576. operate=new JButton[28];
  577. /**24**/
  578. for(i=0;i<24;i++)
  579. {
  580. operate[i]=new JButton(name[i]);
  581. operate[i].setMargin(new java.awt.Insets(0,0,0,0));
  582. operate[i].setBounds(i%5*(35+7+15), 60+i/5*(29+5+15)+5, 35, 29);
  583. jp.add(operate[i]);
  584. }
  585. JButton jb1= new JButton("返回"); //返回复杂计算器
  586. jb1.setBackground(new Color(233,240,247));
  587. jb1.setForeground(new Color(30,57,91));
  588. jb1.setVisible(true);
  589. jb1.setBounds(210, 0, 110, 40);
  590. jb1.addActionListener(new ActionListener()
  591. {
  592. @Override
  593. public void actionPerformed(ActionEvent e) {
  594. a.setVisible(false);
  595. JSQ calculator = new JSQ();
  596. }
  597. });
  598. jp.add(jb1);
  599. //添加按钮
  600. /***=***/
  601. operate[i]=new JButton(name[i]);
  602. operate[i].setMargin(new java.awt.Insets(0,0,0,0));
  603. operate[i].setBounds(i%5*(35+7+15), 60+i/5*(29+5+15)+5, 35, 29*2+5);
  604. jp.add(operate[i]);
  605. i++;
  606. /***0***/
  607. operate[i]=new JButton(name[i]);
  608. operate[i].setMargin(new java.awt.Insets(0,0,0,0));
  609. operate[i].setBounds(i%5*(35+7+15), 60+i/5*(29+5+15)+5, 35*2+7, 29);
  610. jp.add(operate[i]);
  611. /**24**/
  612. for(i=i+1;i<name.length;i++)
  613. {
  614. operate[i]=new JButton(name[i]);
  615. operate[i].setMargin(new java.awt.Insets(0,0,0,0));
  616. operate[i].setBounds((i+1)%5*(35+7+15), 60+i/5*(29+5+15)+5, 35, 29);
  617. jp.add(operate[i]);
  618. }
  619. /**
  620. * 给按钮注册鼠标监听器,键盘监听器和背景
  621. */
  622. mouseAdapter ml=new mouseAdapter();
  623. keyAdapter kl=new keyAdapter();
  624. //算法的介绍
  625. for(i=0;i<name.length;i++)
  626. {
  627. operate[i].addMouseListener(ml);
  628. operate[i].addKeyListener(kl);
  629. operate[i].setBackground(new Color(233,240,247));
  630. operate[i].setForeground(new Color(30,57,91));
  631. }
  632. jp.add(display1);
  633. jp.add(display2);
  634. jp.add(display3);
  635. jp.addKeyListener(kl);
  636. this.add(jp);
  637. this.setVisible(true);
  638. }
  639. class mouseAdapter extends MouseAdapter{
  640. public void mouseClicked(MouseEvent e)
  641. {
  642. JButton operate=(JButton)e.getSource();
  643. if(Disable)//禁用按钮,点击C 恢复计算器
  644. {
  645. if(operate.getText()=="C")
  646. { clear();
  647. Disable=false;
  648. }
  649. else
  650. return;
  651. }
  652. if(operate.getText().equals("MC")){
  653. mc();
  654. //return;
  655. }
  656. if(operate.getText().equals("MR")){
  657. mr();
  658. //return;
  659. }
  660. if(operate.getText().equals("MS")){
  661. ms();
  662. //return;
  663. }
  664. if(operate.getText().equals("M+")){
  665. mAdd();
  666. //return;
  667. }
  668. if(operate.getText().equals("M-")){
  669. mCut();
  670. //return;
  671. }
  672. if(operate.getText().equals("←")){
  673. cutEnd();
  674. // return;
  675. }
  676. if(operate.getText().equals("CE")){
  677. cutNum();
  678. //return;
  679. }
  680. if(operate.getText().equals("C")){
  681. clear();
  682. //return;
  683. }
  684. if(operate.getText().equals("±")){
  685. revolt();
  686. //return;
  687. }
  688. if(operate.getText().equals("√")){
  689. sqrt();
  690. //return;
  691. }
  692. if(operate.getText().equals("7")||operate.getText().equals("8")||operate.getText().equals("9")||operate.getText().equals("4")||operate.getText().equals("5")||operate.getText().equals("6")||operate.getText().equals("3")||operate.getText().equals("2")||operate.getText().equals("1")||operate.getText().equals("0")){
  693. read(Integer.parseInt(operate.getText()));
  694. }
  695. if(operate.getText().equals("/")){
  696. divide();
  697. }
  698. if(operate.getText().equals("%")){
  699. mo();
  700. }
  701. if(operate.getText().equals("*")){
  702. mul();
  703. }
  704. if(operate.getText().equals("1/x")){
  705. inverted();
  706. }
  707. if(operate.getText().equals("-")){
  708. cut();
  709. }
  710. if(operate.getText().equals("+")){
  711. add();
  712. }
  713. if(operate.getText().equals("=")){
  714. sum();
  715. }
  716. if(operate.getText().equals(".")){
  717. dot();
  718. }
  719. // switch(operate.getText())
  720. // {
  721. // case "MC":mc();break;
  722. // case "MR":mr();break;
  723. // case "MS":ms();break;
  724. // case "M+":mAdd();break;
  725. // case "M-":mCut();break;
  726. // case "←":cutEnd();break;
  727. // case "CE":cutNum();break;
  728. // case "C":clear();break;
  729. // case "±":revolt();break;
  730. // case "√":sqrt();break;
  731. // case "7":
  732. // case "8":
  733. // case "9":
  734. // case "4":
  735. // case "5":
  736. // case "6":
  737. // case "3":
  738. // case "2":
  739. // case "1":
  740. // case "0":read(Integer.parseInt(operate.getText()));break;//将按键上的文本转化为Int型
  741. // case "/":divide();break;
  742. // case "%":mo();break;
  743. // case "*":mul();break;
  744. // case "1/x":inverted();break;
  745. // case "-":cut();break;
  746. // case "+":add();break;
  747. // case "=":sum();break;
  748. // case ".":dot();break;
  749. // }
  750. }
  751. public void mouseEntered(MouseEvent e)
  752. {
  753. ((JButton)e.getSource()).setBackground(new Color(255,211,113));
  754. }
  755. public void mouseExited(MouseEvent e)
  756. {
  757. ((JButton)e.getSource()).setBackground(new Color(233,240,247));
  758. }
  759. }
  760. /*
  761. * MC 取消存储区,清空存储区数据
  762. */
  763. public void mc()
  764. {
  765. storage.storageNum=0;
  766. storage.storageMode=false;
  767. display3.setText(null);
  768. }
  769. /*
  770. * MR 读取存储器存储的数据
  771. */
  772. public void mr()
  773. {
  774. op1.value=storage.storageNum;
  775. screen2=""+op1.value;
  776. if(op1.value==Math.floor(op1.value))
  777. screen2=""+(int)op1.value;
  778. display2.setText(screen2);
  779. op1.clear=true;
  780. }
  781. /*
  782. * MS 保存数据到存储器
  783. */
  784. public void ms()
  785. {
  786. storage.storageNum=op1.value;
  787. display3.setText("M");//屏幕左下角显示M标志
  788. }
  789. /*
  790. * M+ 已经储存的数加上当前计算结果并将和存入存储器
  791. */
  792. public void mAdd()
  793. {
  794. storage.storageNum=storage.storageNum+op1.value;
  795. }
  796. /*
  797. * M- 已经储存的数减去当前计算结果并将差存入存储器
  798. */
  799. public void mCut()
  800. {
  801. storage.storageNum=storage.storageNum-op1.value;
  802. }
  803. /*
  804. * ← 输入的数去掉尾数
  805. *
  806. */
  807. public void cutEnd()
  808. {
  809. if(action=="op1"&&op1.value!=0)//表示对op1进行操作
  810. {
  811. if(op1.isFloat==false)//如果op1为整数
  812. {
  813. op1.value=(int)op1.value/10;
  814. screen2=""+(int)op1.value;
  815. }
  816. else{//如果op1为小数
  817. BigDecimal bd=new BigDecimal(op1.value);
  818. op1.value=bd.setScale(--op1.dotWei,BigDecimal.ROUND_DOWN).doubleValue();
  819. screen2=""+op1.value;
  820. if(op1.dotWei==0)//小数点后数位都去除掉后,变位整数,更新isFloat标记
  821. op1.isFloat=false;
  822. }
  823. }
  824. else if(action=="op2"&&op2.value!=0)//表示对op2进行操作
  825. {
  826. if(op2.isFloat==false)//如果op2位整数
  827. {
  828. op2.value=(int)op2.value/10;
  829. screen2=""+(int)op2.value;
  830. }
  831. else{//如果op2为小数
  832. BigDecimal bd=new BigDecimal(op2.value);
  833. op2.value=bd.setScale(--op2.dotWei,BigDecimal.ROUND_DOWN ).doubleValue();
  834. screen2=""+op2.value;
  835. if(op2.dotWei==0)//小数点后数位都去除掉后,变位整数,更新isFloat标记
  836. op2.isFloat=false;
  837. }
  838. }
  839. display2.setText(screen2);//输出修改后的操作数
  840. }
  841. /*
  842. * CE 清空当前操作数操作数
  843. */
  844. public void cutNum()
  845. {
  846. if(action=="op1")
  847. op1.reset();
  848. else if(action=="op2")
  849. op2.reset();
  850. display2.setText("0");//初始化显示屏2
  851. }
  852. /*
  853. * C 归零 重置计算器
  854. */
  855. public void clear()
  856. {
  857. op1.reset();
  858. op2.reset();
  859. //初始化数据成员
  860. action="op1";
  861. sign="";
  862. //初始化显示屏
  863. display1.setText("");
  864. display2.setText("0");
  865. }
  866. /*
  867. * ± 正负号
  868. */
  869. public void revolt()
  870. {
  871. if(action=="op1")
  872. {
  873. op1.value=-op1.value;
  874. screen2=""+op1.value;
  875. }
  876. else if(action=="op2")
  877. {
  878. op2.value=-op2.value;
  879. screen2=""+op2.value;
  880. }
  881. display2.setText(screen2);
  882. }
  883. /*
  884. * √ 根号
  885. */
  886. public void sqrt()
  887. {
  888. double x;//临时变量
  889. if(action=="op1")
  890. {
  891. op1.sqrtedString="sqrt("+op1.value+")";
  892. op1.value=Math.sqrt(op1.value);
  893. op1.isSqrted=true;
  894. x=op1.value;
  895. }
  896. else
  897. {
  898. op2.sqrtedString="sqrt("+op2.value+")";
  899. op2.value=Math.sqrt(op2.value);
  900. op2.isSqrted=true;
  901. x=op2.value;
  902. }
  903. screen2=x+"";
  904. if(x==Math.floor(x))//如果x为整数
  905. {
  906. screen2=(int)x+"";//则将浮点数x先转化为int再转化成字符串
  907. }
  908. display2.setText(screen2);
  909. }
  910. /*
  911. * 按下数字键
  912. */
  913. public void read(int value)
  914. {
  915. //display2.setFont(new Font("宋体",Font.BOLD,20));//默认字体大小
  916. display2.setText(null);//清屏
  917. if(op1.clear==true)
  918. op1.reset();
  919. if(op2.clear==true)
  920. op2.reset();
  921. if(action=="op1")//表示输数据给op1
  922. {
  923. if(op1.isFloat==true)//若op1为浮点数
  924. {
  925. int i=1;
  926. double num=value;
  927. ++op1.dotWei;
  928. while(i<=op1.dotWei)
  929. {
  930. num*=0.1;
  931. i++;
  932. }
  933. op1.value=op1.value+num;//将新的小数点位添加到操作数op1
  934. //因为双精度浮点数 其精度比较高,而我们只需取它的op1.dotWei保存
  935. op1.value=Double.parseDouble(String.format("%."+op1.dotWei+"f",op1.value));
  936. //因为双精度浮点数 其精度比较高,而我们只需取它的op1.dotWei显示在屏幕上
  937. display2.setText(String.format("%."+op1.dotWei+"f", op1.value));
  938. }
  939. else//op1为整数
  940. {
  941. op1.value=op1.value*10+value;//将新的整数位加倒op1
  942. display2.setText((int)op1.value+"");//屏幕输出op1的值
  943. }
  944. }
  945. else if(action=="op2")//表示输数据给op2
  946. {
  947. if(op2.isFloat==true)//若op2为浮点数
  948. {
  949. int i=1;
  950. double num=value;
  951. ++op2.dotWei;
  952. while(i<=op2.dotWei)
  953. {
  954. num*=0.1;
  955. i++;
  956. }
  957. op2.value=op2.value+num;//将新的小数点位添加到操作数op2
  958. //因为双精度浮点数 其精度比较高,而我们只需取它的op2.dotWei保存
  959. op2.value=Double.parseDouble(String.format("%."+op2.dotWei+"f",op2.value));
  960. //因为双精度浮点数 其精度比较高,而我们只需取它的op2.dotWei显示在屏幕上
  961. display2.setText(String.format("%."+op2.dotWei+"f", op2.value));
  962. }
  963. else//op2为整数
  964. {
  965. op2.value=op2.value*10+value;
  966. display2.setText((int)op2.value+"");
  967. }
  968. }
  969. }
  970. public void divide()
  971. {
  972. run("/");
  973. }
  974. public void mo()
  975. {
  976. run("%");
  977. }
  978. public void mul()
  979. {
  980. run("*");
  981. }
  982. /*
  983. * 1/x
  984. */
  985. public void inverted()
  986. {
  987. double num;
  988. String str;
  989. if(action=="op1")
  990. {
  991. op1.invertedString="1/"+op1.value;//1/x形式字符串
  992. op1.value=1/op1.value;
  993. op1.isInverted=true;
  994. num=op1.value;
  995. }
  996. else{
  997. op2.invertedString="1/"+op2.value;//1/x形式字符串
  998. op2.value=1/op2.value;
  999. op1.isInverted=true;
  1000. num=op2.value;
  1001. }
  1002. str=num+"";
  1003. if(str.length()>=16)//计算器屏幕所能显示数据的最大长度
  1004. {
  1005. display2.setFont(new Font("宋体",Font.BOLD,14));//缩小字体输出
  1006. display2.setText(str.substring(0, 16));
  1007. }
  1008. else
  1009. display2.setText(str);
  1010. }
  1011. public void cut()
  1012. {
  1013. run("-");
  1014. }
  1015. public void add()
  1016. {
  1017. run("+");
  1018. }
  1019. public void sum()
  1020. {
  1021. display2.setFont(new Font("宋体",Font.BOLD,20));
  1022. int d1=op1.dotWei,d2=op2.dotWei,i;
  1023. if(sign.equals("+")){
  1024. op1.value=op1.value+op2.value;
  1025. }
  1026. if(sign.equals("-")){
  1027. op1.value=op1.value-op2.value;
  1028. }
  1029. if(sign.equals("*")){
  1030. op1.value=op1.value*op2.value;
  1031. }
  1032. if(sign.equals("/")){
  1033. op1.value=op1.value/op2.value;
  1034. }
  1035. if(sign.equals("%")){
  1036. op1.value=op1.value%op2.value;
  1037. }
  1038. // switch(sign)
  1039. // { //运算后 结果保存到op1
  1040. // case "+":op1.value=op1.value+op2.value;break;
  1041. // case "-":op1.value=op1.value-op2.value;break;
  1042. // case "*":op1.value=op1.value*op2.value;break;
  1043. // case "/":op1.value=op1.value/op2.value;break;
  1044. // case "%":op1.value=op1.value%op2.value;break;
  1045. // }
  1046. if(op2.value==0&&sign=="/")//除数为0
  1047. {
  1048. Disable=true;
  1049. display2.setText(op1.value+"");
  1050. display1.setText(null);
  1051. action="op1";
  1052. return ;
  1053. }
  1054. if(op1.value==Math.floor(op1.value))//结果为整数
  1055. {
  1056. display2.setText((int)op1.value+"");
  1057. op1.dotWei=0;
  1058. op1.isFloat=false;
  1059. }
  1060. else{//结果为小数
  1061. String str=op1.value+"";
  1062. //准确控制算术运算结果的精度,加,减,取模运算,小数点后的有效数字最多为max(d1,d2)位
  1063. if(sign.equals("+")||sign.equals("-")||sign.equals("%"))
  1064. {
  1065. i=d1>d2?d1:d2;
  1066. str=op1.value+"";
  1067. str=str.substring(0, str.indexOf(".")+i+1);//取i位输出
  1068. }
  1069. //准确控制算术运算结果的精度,乘法运算,小数点后的有效数字最多为d1+d2
  1070. else if(sign.equals("*"))
  1071. {
  1072. i=d1+d2;
  1073. BigDecimal bd=new BigDecimal(op1.value);
  1074. op1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue();
  1075. str=op1.value+"";//更新修改后的str
  1076. }
  1077. //结果超过显示数据的最大长度
  1078. if(str.length()>=16)
  1079. {
  1080. display2.setFont(new Font("宋体",Font.BOLD,14));
  1081. str=str.substring(0, 16);
  1082. }
  1083. display2.setText(str);
  1084. op1.dotWei=str.length()-str.indexOf(".")-1;//更新op1w值
  1085. }
  1086. display1.setText(null);
  1087. action="op1";
  1088. op1.clear=true;//开始新的表达式运算时,op1要先重置
  1089. op2.clear=true;//开始新的表达式运算时,op2要先重置
  1090. sign="";
  1091. }
  1092. public void dot()
  1093. {
  1094. if(action=="op1")
  1095. op1.isFloat=true;
  1096. else
  1097. op2.isFloat=true;
  1098. display2.setText(display2.getText()+".");
  1099. }
  1100. public void run(String SIGN)
  1101. {
  1102. display2.setFont(new Font("宋体",Font.BOLD,20));
  1103. action="op2";
  1104. int d1=op1.dotWei,d2=op2.dotWei,i;
  1105. if(!sign.equals(""))//检测是否为以为表达式的第一运算
  1106. {
  1107. if(sign.equals("+")){
  1108. op1.value=op1.value+op2.value;
  1109. }
  1110. if(sign.equals("-")){
  1111. op1.value=op1.value-op2.value;
  1112. }
  1113. if(sign.equals("*")){
  1114. op1.value=op1.value*op2.value;
  1115. }
  1116. if(sign.equals("/")){
  1117. op1.value=op1.value/op2.value;
  1118. }
  1119. if(sign.equals("%")){
  1120. op1.value=op1.value%op2.value;
  1121. }
  1122. // switch(sign)
  1123. // {//运算后 结果保存到op1
  1124. // case "+":op1.value=op1.value+op2.value;break;
  1125. // case "-":op1.value=op1.value-op2.value;break;
  1126. // case "*":op1.value=op1.value*op2.value;break;
  1127. // case "/":op1.value=op1.value/op2.value;break;
  1128. // case "%":op1.value=op1.value%op2.value;break;
  1129. // }
  1130. }
  1131. String temp=isSpecileHandle();
  1132. if(temp==null)
  1133. temp=display2.getText();//先保存display2文本框里的数据
  1134. if(op2.value==0&&sign=="/")//除数为0
  1135. {
  1136. Disable=true;
  1137. display2.setText(op1.value+"");
  1138. display1.setText(display1.getText()+op1.value);
  1139. }
  1140. if(op1.value==Math.floor(op1.value))//结果为整数
  1141. {
  1142. display2.setText((int)op1.value+"");
  1143. op1.dotWei=0;
  1144. op1.isFloat=false;
  1145. }
  1146. else{
  1147. String str=op1.value+"";
  1148. //准确控制算术运算结果的精度,加,减,取模运算,小数点后的有效数字最多为max(d1,d2)位
  1149. if(sign.equals("+")||sign.equals("-")||sign.equals("%"))
  1150. {
  1151. i=d1>d2?d1:d2;
  1152. BigDecimal bd=new BigDecimal(op1.value);
  1153. op1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue();
  1154. str=op1.value+"";//更新修改后的str
  1155. }
  1156. //准确控制算术运算结果的精度,乘法运算,小数点后的有效数字最多为d1+d2
  1157. else if(sign.equals("*"))
  1158. {
  1159. i=d1+d2;
  1160. BigDecimal bd=new BigDecimal(op1.value);
  1161. op1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue();
  1162. str=op1.value+"";//更新修改后的str
  1163. }
  1164. //结果超过显示数据的最大长度
  1165. if(str.length()>=16)
  1166. {
  1167. display2.setFont(new Font("宋体",Font.BOLD,14));
  1168. str=str.substring(0, 16);
  1169. }
  1170. display2.setText(str);
  1171. op1.dotWei=str.length()-str.indexOf(".")-1;//每次加完后,如果结果op1的值为小数则更新op1w的值
  1172. }
  1173. sign=SIGN;
  1174. display1.setText(display1.getText()+temp+sign);
  1175. op2.value=op1.value;//运算后,操作数op2默认的值为op1的值
  1176. op2.clear=true;//下一次键入数据,op2要重置
  1177. op1.clear=false;//下一次键入数据,op1不要重置
  1178. }
  1179. /*
  1180. * isSpecileHandle()
  1181. * 操作数是否sqrt()或1/x过,
  1182. * 如果有,则返回"sqrt(x)""1/x"字符串
  1183. */
  1184. public String isSpecileHandle()
  1185. {
  1186. String temp=null;
  1187. if(op1.isSqrted)
  1188. {
  1189. temp=op1.sqrtedString;
  1190. op1.isSqrted=false;
  1191. }
  1192. else if(op2.isSqrted)
  1193. {
  1194. temp=op2.sqrtedString;
  1195. op2.isSqrted=false;
  1196. }
  1197. if(op1.isInverted)
  1198. {
  1199. temp=op1.invertedString;
  1200. op1.isInverted=false;
  1201. }
  1202. else if(op2.isInverted)
  1203. {
  1204. temp=op2.invertedString;
  1205. op2.isInverted=false;
  1206. }
  1207. return temp;
  1208. }
  1209. class keyAdapter extends KeyAdapter{
  1210. public void keyPressed(KeyEvent e)
  1211. {
  1212. int keycode=e.getKeyCode();
  1213. if((keycode>=96&&keycode<=105)||(keycode>=48&&keycode<=57))//数字键
  1214. {
  1215. if(keycode>=96)
  1216. keycode-=48;
  1217. switch(keycode)
  1218. {
  1219. case 48:read(0);break;
  1220. case 49:read(1);break;
  1221. case 50:read(2);break;
  1222. case 51:read(3);break;
  1223. case 52:read(4);break;
  1224. case 53:read(5);break;
  1225. case 54:read(6);break;
  1226. case 55:read(7);break;
  1227. case 56:read(8);break;
  1228. case 57:read(9);break;
  1229. }
  1230. }
  1231. //运算符键+ - * / =和. 以及退格键(* =号)
  1232. else if((keycode==110||keycode==46)||(keycode==111||keycode==47)||(keycode==45||keycode==109)||keycode==107||keycode==106||keycode==61||keycode==10||keycode==8)
  1233. {
  1234. if((keycode==110||keycode==46))
  1235. dot();
  1236. else if(keycode==111||keycode==47)
  1237. run("/");
  1238. else if(keycode==45||keycode==109)
  1239. run("-");
  1240. else if(keycode==107)
  1241. run("+");
  1242. else if(keycode==106)
  1243. run("*");
  1244. else if(keycode==61||keycode==10)//=
  1245. sum();
  1246. else if(keycode==8)
  1247. cutEnd();
  1248. }
  1249. }
  1250. }
  1251. public static void main(String[] args) {
  1252. // TODO Auto-generated method stub
  1253. //StandardCalculator sc=new StandardCalculator();
  1254. }
  1255. }
  1256. //第三个类
  1257. package qm;
  1258. public class StandardCalculatorText {
  1259. public static void main(String[] args) {
  1260. // TODO Auto-generated method stub
  1261. //StandardCalculator sc=new StandardCalculator();
  1262. }
  1263. }
  1264. class operateNum {//操作数类
  1265. public double value;//操作数的实际值
  1266. public int dotWei;//操作数如果是小数,记录小数点后的位数
  1267. public String invertedString;//1/x字符串
  1268. public String sqrtedString;//sqrt(x)字符串
  1269. public boolean isSqrted,isInverted;//做标记是否√,1/x过,用于后续判断
  1270. public boolean clear;//clear为真表示是否重置操作数
  1271. public boolean isFloat;//isFloat为真,表示操作数是小数
  1272. public operateNum()
  1273. {
  1274. value=0;
  1275. dotWei=0;
  1276. sqrtedString=null;
  1277. invertedString=null;
  1278. isSqrted=false;
  1279. isInverted=false;
  1280. clear=false;
  1281. isFloat=false;
  1282. }
  1283. public void reset()//重置操作数
  1284. {
  1285. value=0;
  1286. dotWei=0;
  1287. sqrtedString=null;
  1288. invertedString=null;
  1289. isSqrted=false;
  1290. isInverted=false;
  1291. clear=false;
  1292. isFloat=false;
  1293. }
  1294. }
  1295. class storageField {//存储区
  1296. public double storageNum;//存储区存储的数据
  1297. public boolean storageMode;//是否启动存储区
  1298. public storageField()
  1299. {
  1300. storageNum=0;
  1301. storageMode=false;
  1302. }
  1303. }
  1304. //第四个类
  1305. package qm;
  1306. import javax.swing.*;
  1307. import javax.swing.event.*;
  1308. import java.awt.*;
  1309. import java.awt.event.*;
  1310. public class UnitTransfer extends JFrame{
  1311. private final int WIDTH = 324, HEIGHT = 424;//窗口默认的宽度、高度
  1312. private JLabel lblInUnit=new JLabel("输入单位");
  1313. private JComboBox cboIn=new JComboBox(new String[]{"", ""});
  1314. private JLabel lblIn=new JLabel("输入数值");
  1315. private JTextField txtIn=new JTextField("10");
  1316. private JLabel lblOutUnit=new JLabel("输出单位");
  1317. private JLabel lblResult=new JLabel("显示结果");
  1318. private JLabel txtResult=new JLabel("结果");
  1319. private JComboBox cboOut=new JComboBox(new String[]{"", ""});
  1320. private JButton btnTrans = new JButton("转换");
  1321. private JButton btnClear = new JButton("清空");
  1322. private JButton btnreturn=new JButton("返回");
  1323. private JRadioButton rdLeng = new JRadioButton("长度");
  1324. private JRadioButton rdWeig = new JRadioButton("时间");
  1325. private String [] lengthUnit={"米", "分米", "厘米", "毫米"};
  1326. private String [] timeUnit={"天", "时", "分", "秒"};
  1327. public UnitTransfer(){
  1328. super("单位转换器 ");
  1329. this.setSize(WIDTH, HEIGHT);
  1330. this.setLocation( 300,200 );
  1331. this.setResizable(false);
  1332. this.setLayout(null);
  1333. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  1334. ButtonGroup group = new ButtonGroup();
  1335. group.add(rdLeng);
  1336. group.add(rdWeig);
  1337. this.getContentPane().add(rdLeng);
  1338. this.getContentPane().add(rdWeig);
  1339. this.getContentPane().add(btnTrans);
  1340. this.getContentPane().add(btnClear);
  1341. this.getContentPane().add(btnreturn);
  1342. this.getContentPane().add(lblIn);
  1343. this.getContentPane().add(txtIn);
  1344. this.getContentPane().add(lblInUnit);
  1345. this.getContentPane().add(cboIn);
  1346. this.getContentPane().add(lblResult);
  1347. this.getContentPane().add(txtResult);
  1348. this.getContentPane().add(lblOutUnit);
  1349. this.getContentPane().add(cboOut);
  1350. txtIn.setFont(new Font("宋体",Font.BOLD,20));
  1351. lblInUnit.setFont(new Font("宋体",Font.BOLD,15));
  1352. lblIn.setFont(new Font("宋体",Font.BOLD,15));
  1353. lblOutUnit.setFont(new Font("宋体",Font.BOLD,15));
  1354. lblResult.setFont(new Font("宋体",Font.BOLD,15));
  1355. btnTrans.setFont(new Font("宋体",Font.BOLD,15));
  1356. btnClear.setFont(new Font("宋体",Font.BOLD,15));
  1357. btnreturn.setBackground(new Color(233,240,247));
  1358. btnreturn.setForeground(new Color(30,57,91));
  1359. btnClear.setBackground(new Color(233,240,247));
  1360. btnClear.setForeground(new Color(30,57,91));
  1361. btnTrans.setBackground(new Color(233,240,247));
  1362. btnTrans.setForeground(new Color(30,57,91));
  1363. cboIn.setBackground(new Color(233,240,247));
  1364. cboIn.setForeground(new Color(30,57,91));
  1365. btnreturn.setBackground(new Color(233,240,247));
  1366. cboOut.setForeground(new Color(30,57,91));
  1367. this.setVisible(true);
  1368. this.doLayout();
  1369. btnTrans.addActionListener(new ActionListener(){
  1370. public void actionPerformed(ActionEvent e){
  1371. doConvert();
  1372. }
  1373. });
  1374. btnClear.addActionListener(new ActionListener(){
  1375. public void actionPerformed(ActionEvent e){
  1376. txtIn.setText("0");
  1377. txtResult.setText("0");
  1378. }
  1379. });
  1380. btnreturn.addActionListener(new ActionListener(){
  1381. @Override
  1382. public void actionPerformed(ActionEvent e){
  1383. JSQ calculator = new JSQ();
  1384. boolean flase = false;
  1385. setVisible(flase);
  1386. }
  1387. });
  1388. rdLeng.addActionListener(new ActionListener(){
  1389. public void actionPerformed(ActionEvent e){
  1390. cboIn.setModel(new DefaultComboBoxModel(lengthUnit));
  1391. cboOut.setModel(new DefaultComboBoxModel(lengthUnit));
  1392. }
  1393. });
  1394. rdWeig.addActionListener(new ActionListener(){
  1395. public void actionPerformed(ActionEvent e){
  1396. cboIn.setModel(new DefaultComboBoxModel(timeUnit));
  1397. cboOut.setModel(new DefaultComboBoxModel(timeUnit));
  1398. }
  1399. });
  1400. rdLeng.setSelected(true);
  1401. cboIn.setModel(new DefaultComboBoxModel(lengthUnit));
  1402. cboOut.setModel(new DefaultComboBoxModel(timeUnit));
  1403. }
  1404. final int offX=100;
  1405. public void doLayout(){
  1406. super.doLayout();
  1407. rdLeng.setBounds(offX, 15, 60, 20);
  1408. rdWeig.setBounds(rdLeng.getX()+rdLeng.getWidth()+5, 15, 60, 20);
  1409. lblInUnit.setBounds(offX, rdLeng.getY()+rdLeng.getHeight()+20, 80, 20);
  1410. cboIn.setBounds(lblInUnit.getX()+lblInUnit.getWidth()+5, lblInUnit.getY(), 80, 20);
  1411. lblIn.setBounds(offX, lblInUnit.getY()+lblInUnit.getHeight()+5, 80, 20);
  1412. txtIn.setBounds(lblIn.getX()+lblIn.getWidth()+5, lblIn.getY(), 80, 20);
  1413. lblOutUnit.setBounds(offX, lblIn.getY()+lblIn.getHeight()+30, 80, 20);
  1414. cboOut.setBounds(lblOutUnit.getX()+lblOutUnit.getWidth()+5, lblOutUnit.getY(), 80, 20);
  1415. lblResult.setBounds(offX, cboOut.getY()+cboOut.getHeight()+5, 80, 20);
  1416. txtResult.setBounds(lblResult.getX()+lblResult.getWidth()+5, lblResult.getY(), 100, 20);
  1417. int w=getWidth ();
  1418. int x=(w-70*2-5)/2;//水平居中
  1419. btnTrans.setBounds(x, lblResult.getY()+lblResult.getHeight()+30, 70, 25);
  1420. btnClear.setBounds(btnTrans.getX()+btnTrans.getWidth()+3, btnTrans.getY(), 70, 25);
  1421. //btnreturn.setBounds(x, lblResult.getY()+lblResult.getHeight()+80, 70, 65);
  1422. btnreturn.setBounds(15,12, 70, 25);
  1423. }
  1424. public void doConvert(){
  1425. double v=0;
  1426. try{
  1427. v= Double.parseDouble(txtIn.getText());
  1428. }catch(Exception ex){
  1429. txtIn.setText("0");
  1430. return;
  1431. }
  1432. //"米", "分米", "厘米", "毫米"
  1433. if(rdLeng.isSelected()){
  1434. switch(cboIn.getSelectedIndex()){
  1435. case 0:
  1436. break;
  1437. case 1:
  1438. v=v/10;
  1439. break;
  1440. case 2:
  1441. v=v/100;
  1442. break;
  1443. case 3:
  1444. v=v/1000;
  1445. break;
  1446. default:
  1447. return;
  1448. }
  1449. //v 现在是标准单位:米
  1450. switch(cboOut.getSelectedIndex()){
  1451. case 0:
  1452. break;
  1453. case 1:
  1454. v=v*10;
  1455. break;
  1456. case 2:
  1457. v=v*100;
  1458. break;
  1459. case 3:
  1460. v=v*1000;
  1461. break;
  1462. default:
  1463. return;
  1464. }
  1465. if(v<0.01){
  1466. txtResult.setText(String.format("%2.8f", v));
  1467. }else{
  1468. txtResult.setText(String.format("%2.2f", v));
  1469. }
  1470. }else{
  1471. //"天", "时", "分", "秒"
  1472. switch(cboIn.getSelectedIndex()){
  1473. case 0:
  1474. v=v*24;
  1475. break;
  1476. case 1:
  1477. break;
  1478. case 2:
  1479. v=v/60;
  1480. break;
  1481. case 3:
  1482. v=v/3600;
  1483. break;
  1484. default:
  1485. return;
  1486. }
  1487. //v 现在是标准单位:小时
  1488. switch(cboOut.getSelectedIndex()){
  1489. case 0:
  1490. v=v/24;
  1491. break;
  1492. case 1:
  1493. break;
  1494. case 2:
  1495. v=v*60;
  1496. break;
  1497. case 3:
  1498. v=v*3600;
  1499. break;
  1500. default:
  1501. return;
  1502. }
  1503. if(v<0.01){
  1504. txtResult.setText(String.format("%2.8f", v));
  1505. }else{
  1506. txtResult.setText(String.format("%2.8f", v));
  1507. }
  1508. }
  1509. }
  1510. public static void main(String[] args){
  1511. EventQueue.invokeLater(new Runnable() {
  1512. public void run() {
  1513. //UnitTransfer un= new UnitTransfer();
  1514. }
  1515. });
  1516. }
  1517. }
  1518. //最后一个类
  1519. package qm;
  1520. import java.awt.*;
  1521. import java.awt.event.*;
  1522. import javax.swing.*;
  1523. //import java.awt.Button;
  1524. import java.text.DecimalFormat;
  1525. public class WenDuChang extends JFrame implements ActionListener{
  1526. private JTextField textField;
  1527. private JTextField textField_1;
  1528. private JLabel label;
  1529. private JLabel label_1;
  1530. private JLabel label_2;
  1531. private JRadioButton[] radioButton;
  1532. private JButton button;
  1533. private JButton button_1;
  1534. private ButtonGroup bg;
  1535. public static void main( String args[] )
  1536. {
  1537. //WenDuChang wd=new WenDuChang();
  1538. }
  1539. public WenDuChang() {
  1540. //获取你的屏幕的宽和高
  1541. //int width = Toolkit.getDefaultToolkit().getScreenSize().width;
  1542. // int height = Toolkit.getDefaultToolkit().getScreenSize().height;
  1543. // setLocation(width/2-200, height/2-150);
  1544. setTitle("温度转换器");
  1545. getContentPane().setLayout(null);
  1546. textField = new JTextField();//摄氏温度
  1547. textField.setText("0.0");
  1548. textField.setFont(new Font("宋体",Font.BOLD,20));
  1549. //textField.setBounds(50, 63, 84, 27);
  1550. textField.setBounds(170, 33, 104, 37);
  1551. getContentPane().add(textField);
  1552. textField.setColumns(10);
  1553. label = new JLabel("摄氏温度");
  1554. //label.setBounds(137,69,54,15);
  1555. label.setBounds(60,35,104,37);
  1556. getContentPane().add(label);
  1557. label_1 = new JLabel("<<==>>");
  1558. //label_1.setBounds(201, 69, 54, 15);
  1559. label_1.setBounds(130, 84, 104, 45);
  1560. getContentPane().add(label_1);
  1561. textField_1 = new JTextField();//华氏温度
  1562. textField_1.setText("0.0");
  1563. textField_1.setFont(new Font("宋体",Font.BOLD,20));
  1564. //textField_1.setBounds(, 66, 90, 24);
  1565. textField_1.setBounds(170, 140, 104, 37);
  1566. getContentPane().add(textField_1);
  1567. textField_1.setColumns(10);
  1568. label_2 = new JLabel("华氏温度");
  1569. //label_2.setBounds(323, 69, 104, 37);
  1570. label_2.setBounds(60, 150, 54, 15);
  1571. getContentPane().add(label_2);
  1572. radioButton = new JRadioButton[2];
  1573. radioButton[0] = new JRadioButton("摄氏转华氏",true);
  1574. //radioButton[0].setBounds(100, 142, 121, 23);
  1575. radioButton[0].setBounds(55, 210, 121, 23);
  1576. getContentPane().add(radioButton[0]);
  1577. radioButton[1] = new JRadioButton("华氏转摄氏");
  1578. //radioButton[1].setBounds(223, 142, 121, 23);
  1579. radioButton[1].setBounds(180, 210, 121, 23);
  1580. getContentPane().add(radioButton[1]);
  1581. bg=new ButtonGroup();
  1582. bg.add(radioButton[0]);
  1583. bg.add(radioButton[1]);
  1584. button = new JButton("温度转化");
  1585. //button.setBounds(100, 196, 93, 23);
  1586. button.setBounds(50, 270, 93, 23);
  1587. button.setBackground(new Color(233,240,247));
  1588. button.setForeground(new Color(30,57,91));
  1589. button.addActionListener(this);
  1590. getContentPane().add(button);
  1591. button_1 = new JButton("返回");
  1592. // button_1.setBounds(237,196,93,23);
  1593. button_1.setBounds(180,270,93,23);
  1594. button_1.setBackground(new Color(233,240,247));
  1595. button_1.setForeground(new Color(30,57,91));
  1596. button_1.addActionListener(this);
  1597. getContentPane().add(button_1);
  1598. //setSize(450, 300);
  1599. this.setLocation( 300,200 );
  1600. this.setSize( 328,424);
  1601. this.setResizable( true );
  1602. this.setResizable(false);
  1603. setVisible(true);
  1604. }
  1605. //用于温度转换的方法-----业务处理
  1606. public String WenDuChang(final double tc,final double tf,boolean flag){
  1607. double wdc=0.0;
  1608. //小数格式化,引号中的0.000表示保留小数点后二位(第四位四舍五入)
  1609. DecimalFormat df = new DecimalFormat("0.00");
  1610. String str;
  1611. if(flag){
  1612. wdc=9*(tc-10)/5+50; //摄氏温度转化为华氏温度
  1613. }
  1614. else{
  1615. wdc=5*(tf-50)/9+10; //华氏温度转化为摄氏温度
  1616. }
  1617. str=df.format(wdc);
  1618. return str;
  1619. }
  1620. @Override
  1621. public void actionPerformed(ActionEvent e) {
  1622. double tc = Double.parseDouble(textField.getText().trim());
  1623. double tf = Double.parseDouble(textField_1.getText().trim());
  1624. if(e.getSource()==button){
  1625. //System.out.println("按钮被选中.....");
  1626. if(radioButton[0].isSelected()){//判断radioButton是否被选中
  1627. textField_1.setText(WenDuChang(tc,tf,true));
  1628. }
  1629. if(radioButton[1].isSelected()){
  1630. textField.setText(WenDuChang(tc,tf,false));
  1631. }
  1632. }
  1633. if(e.getSource()==button_1){
  1634. //System.exit(0);
  1635. JSQ calculator = new JSQ();
  1636. this.dispose();
  1637. }
  1638. }
  1639. }

 

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号