Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.60 KB | None | 0 0
  1. import java.awt.EventQueue;
  2. import java.text.DecimalFormat;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JTextField;
  6. import java.awt.BorderLayout;
  7. import java.awt.Font;
  8. import javax.swing.JButton;
  9. import javax.swing.JLabel;
  10. import javax.swing.border.BevelBorder;
  11. import javax.swing.border.MatteBorder;
  12. import java.awt.Color;
  13. import javax.swing.border.LineBorder;
  14. import java.awt.event.ActionListener;
  15. import java.awt.event.ActionEvent;
  16.  
  17. public class Window {
  18.  
  19. double first=0;
  20. double sec=0;
  21. int n=1;
  22. int art=0;
  23. boolean c=false;
  24. boolean onlysec=false;
  25. String zeichen=null;
  26.  
  27. private JFrame frame;
  28. private JLabel ausgabefeld;
  29.  
  30. String pattern = "###,###.##########";
  31. DecimalFormat format = new DecimalFormat(pattern);
  32. /**
  33. * Launch the application.
  34. */
  35. public static void main(String[] args) {
  36. EventQueue.invokeLater(new Runnable() {
  37. public void run() {
  38. try {
  39. Window window = new Window();
  40. window.frame.setVisible(true);
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. });
  46. }
  47.  
  48. /**
  49. * Create the application.
  50. */
  51. public Window() {
  52. initialize();
  53. }
  54.  
  55. /**
  56. * Initialize the contents of the frame.
  57. */
  58.  
  59. public static int dec_length(double input) {
  60. String param=(""+input);
  61. String vergleich =(".0");
  62. String sub= param.substring((param.indexOf('.')));
  63. if (sub.equals(vergleich)) {
  64. return 1;
  65. }else
  66. return sub.length();
  67.  
  68. }
  69.  
  70. private void initialize() {
  71. frame = new JFrame();
  72. frame.setBounds(100, 100, 279, 506);
  73. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  74. frame.getContentPane().setLayout(null);
  75.  
  76. JLabel lblNewLabel;
  77. ausgabefeld = new JLabel("");
  78. ausgabefeld.setBorder(new LineBorder(new Color(0, 0, 0), 3));
  79. ausgabefeld.setBounds(10, 35, 243, 77);
  80. frame.getContentPane().add(ausgabefeld);
  81.  
  82. JButton button_1 = new JButton("1");
  83. button_1.addActionListener(new ActionListener() {
  84. public void actionPerformed(ActionEvent e) {
  85. if(c==false) {
  86. if(n==1) {
  87. first=(first*10)+1;
  88. ausgabefeld.setText(""+format.format(first));
  89. }
  90. else if(n==2) {
  91. sec=(sec*10)+1;
  92. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  93. }
  94. else {
  95.  
  96. }
  97. }else if(c==true) {
  98. if(n==1) {
  99.  
  100.  
  101. System.out.println(first+"");
  102. System.out.println(dec_length(first));
  103.  
  104.  
  105.  
  106. first=((first*Math.pow(10, dec_length(first)))+1)/Math.pow(10, dec_length(first));
  107. ausgabefeld.setText(""+format.format(first));
  108. }
  109. else if(n==2) {
  110. sec=((sec*10)+1)/10;
  111. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  112. }
  113. else {
  114.  
  115. }
  116. }
  117. }
  118. });
  119. button_1.setBounds(3, 159, 50, 50);
  120. frame.getContentPane().add(button_1);
  121.  
  122. JButton button_2 = new JButton("2");
  123. button_2.addActionListener(new ActionListener() {
  124. public void actionPerformed(ActionEvent e) {
  125. if(c==false) {
  126. if(n==1) {
  127. first=(first*10)+2;
  128. ausgabefeld.setText(""+format.format(first));
  129. }
  130. else if(n==2) {
  131. sec=(sec*10)+2;
  132. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  133. }
  134. else {
  135.  
  136. }
  137. }else if(c==true) {
  138. if(n==1) {
  139. first=((first*10)+2)/10;
  140. ausgabefeld.setText(""+format.format(first));
  141. }
  142. else if(n==2) {
  143. sec=((sec*10)+2)/10;
  144. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  145. }
  146. else {
  147.  
  148. }
  149. }
  150. }
  151. });
  152. button_2.setBounds(63, 159, 50, 50);
  153. frame.getContentPane().add(button_2);
  154.  
  155. JButton button_3 = new JButton("3");
  156. button_3.addActionListener(new ActionListener() {
  157. public void actionPerformed(ActionEvent e) {
  158. if(c==false) {
  159. if(n==1) {
  160. first=(first*10)+3;
  161. ausgabefeld.setText(""+format.format(first));
  162. }
  163. else if(n==2) {
  164. sec=(sec*10)+3;
  165. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  166. }
  167. else {
  168.  
  169. }
  170. }else if(c==true) {
  171. if(n==1) {
  172. first=((first*10)+3)/10;
  173. ausgabefeld.setText(""+format.format(first));
  174. }
  175. else if(n==2) {
  176. sec=((sec*10)+3)/10;
  177. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  178. }
  179. else {
  180.  
  181. }
  182. }
  183. }
  184. });
  185. button_3.setBounds(123, 159, 50, 50);
  186. frame.getContentPane().add(button_3);
  187.  
  188. JButton button_4 = new JButton("4");
  189. button_4.addActionListener(new ActionListener() {
  190. public void actionPerformed(ActionEvent e) {
  191. if(c==false) {
  192. if(n==1) {
  193. first=(first*10)+4;
  194. ausgabefeld.setText(""+format.format(first));
  195. }
  196. else if(n==2) {
  197. sec=(sec*10)+4;
  198. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  199. }
  200. else {
  201.  
  202. }
  203. }else if(c==true) {
  204. if(n==1) {
  205. first=((first*10)+4)/10;
  206. ausgabefeld.setText(""+format.format(first));
  207. }
  208. else if(n==2) {
  209. sec=((sec*10)+4)/10;
  210. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  211. }
  212. else {
  213.  
  214. }
  215. }
  216. }
  217. });
  218. button_4.setBounds(3, 220, 50, 50);
  219. frame.getContentPane().add(button_4);
  220.  
  221. JButton button_5 = new JButton("5");
  222. button_5.addActionListener(new ActionListener() {
  223. public void actionPerformed(ActionEvent e) {
  224. if(c==false) {
  225. if(n==1) {
  226. first=(first*10)+5;
  227. ausgabefeld.setText(""+format.format(first));
  228. }
  229. else if(n==2) {
  230. sec=(sec*10)+5;
  231. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  232. }
  233. else {
  234.  
  235. }
  236. }else if(c==true) {
  237. if(n==1) {
  238. first=((first*10)+5)/10;
  239. ausgabefeld.setText(""+format.format(first));
  240. }
  241. else if(n==2) {
  242. sec=((sec*10)+5)/10;
  243. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  244. }
  245. else {
  246.  
  247. }
  248. }
  249. }
  250. });
  251. button_5.setBounds(63, 220, 50, 50);
  252. frame.getContentPane().add(button_5);
  253.  
  254. JButton button_6 = new JButton("6");
  255. button_6.addActionListener(new ActionListener() {
  256. public void actionPerformed(ActionEvent e) {
  257. if(c==false) {
  258. if(n==1) {
  259. first=(first*10)+6;
  260. ausgabefeld.setText(""+format.format(first));
  261. }
  262. else if(n==2) {
  263. sec=(sec*10)+6;
  264. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  265. }
  266. else {
  267.  
  268. }
  269. }else if(c==true) {
  270. if(n==1) {
  271. first=((first*10)+6)/10;
  272. ausgabefeld.setText(""+format.format(first));
  273. }
  274. else if(n==2) {
  275. sec=((sec*10)+6)/10;
  276. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  277. }
  278. else {
  279.  
  280. }
  281. }
  282. }
  283. });
  284. button_6.setBounds(123, 220, 50, 50);
  285. frame.getContentPane().add(button_6);
  286.  
  287. JButton button_7 = new JButton("7");
  288. button_7.addActionListener(new ActionListener() {
  289. public void actionPerformed(ActionEvent e) {
  290. if(c==false) {
  291. if(n==1) {
  292. first=(first*10)+7;
  293. ausgabefeld.setText(""+format.format(first));
  294. }
  295. else if(n==2) {
  296. sec=(sec*10)+7;
  297. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  298. }
  299. else {
  300.  
  301. }
  302. }else if(c==true) {
  303. if(n==1) {
  304. first=((first*10)+7)/10;
  305. ausgabefeld.setText(""+format.format(first));
  306. }
  307. else if(n==2) {
  308. sec=((sec*10)+7)/10;
  309. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  310. }
  311. else {
  312.  
  313. }
  314. }
  315. }
  316. });
  317. button_7.setBounds(3, 281, 50, 50);
  318. frame.getContentPane().add(button_7);
  319.  
  320. JButton button_8 = new JButton("8");
  321. button_8.addActionListener(new ActionListener() {
  322. public void actionPerformed(ActionEvent e) {
  323. if(c==false) {
  324. if(n==1) {
  325. first=(first*10)+8;
  326. ausgabefeld.setText(""+format.format(first));
  327. }
  328. else if(n==2) {
  329. sec=(sec*10)+8;
  330. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  331. }
  332. else {
  333.  
  334. }
  335. }else if(c==true) {
  336. if(n==1) {
  337. first=((first*10)+8)/10;
  338. ausgabefeld.setText(""+format.format(first));
  339. }
  340. else if(n==2) {
  341. sec=((sec*10)+8)/10;
  342. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  343. }
  344. else {
  345.  
  346. }
  347. }
  348. }
  349. });
  350. button_8.setBounds(63, 281, 50, 50);
  351. frame.getContentPane().add(button_8);
  352.  
  353. JButton button_9 = new JButton("9");
  354. button_9.addActionListener(new ActionListener() {
  355. public void actionPerformed(ActionEvent e) {
  356. if(c==false) {
  357. if(n==1) {
  358. first=(first*10)+9;
  359. ausgabefeld.setText(""+format.format(first));
  360. }
  361. else if(n==2) {
  362. sec=(sec*10)+9;
  363. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  364. }
  365. else {
  366.  
  367. }
  368. }else if(c==true) {
  369. if(n==1) {
  370. first=((first*10)+9)/10;
  371. ausgabefeld.setText(""+format.format(first));
  372. }
  373. else if(n==2) {
  374. sec=((sec*10)+9)/10;
  375. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  376. }
  377. else {
  378.  
  379. }
  380. }
  381. }
  382. });
  383. button_9.setBounds(123, 281, 50, 50);
  384. frame.getContentPane().add(button_9);
  385.  
  386. JButton button_0 = new JButton("0");
  387. button_0.addActionListener(new ActionListener() {
  388. public void actionPerformed(ActionEvent e) {
  389. if(c==false) {
  390. if(n==1) {
  391. first=(first*10);
  392. ausgabefeld.setText(""+format.format(first));
  393. }
  394. else if(n==2) {
  395. sec=(sec*10);
  396. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  397. }
  398. else {
  399.  
  400. }
  401. }else if(c==true) {
  402. if(n==1) {
  403. first=((first*10))/10;
  404. ausgabefeld.setText(""+format.format(first));
  405. }
  406. else if(n==2) {
  407. sec=((sec*10))/10;
  408. ausgabefeld.setText(""+format.format(first)+zeichen+format.format(sec));
  409. }
  410. else {
  411.  
  412. }
  413. }
  414. }
  415. });
  416. button_0.setBounds(63, 342, 50, 50);
  417. frame.getContentPane().add(button_0);
  418.  
  419. JButton button_add = new JButton("+");
  420. button_add.addActionListener(new ActionListener() {
  421. public void actionPerformed(ActionEvent e) {
  422. art=1;
  423. c=false;
  424. zeichen=("+");
  425.  
  426. System.out.println(""+n);
  427.  
  428. if(n==1) {
  429. n=2;
  430. onlysec=false;
  431. ausgabefeld.setText(format.format(first)+"+");
  432. }else {
  433.  
  434. }
  435.  
  436. }
  437. });
  438. button_add.setBounds(208, 159, 50, 50);
  439. frame.getContentPane().add(button_add);
  440.  
  441. JButton button_sub = new JButton("-");
  442. button_sub.addActionListener(new ActionListener() {
  443. public void actionPerformed(ActionEvent e) {
  444. art=2;
  445. c=false;
  446. zeichen=("-");
  447. if(n==1) {
  448. n=2;
  449. onlysec=false;
  450. ausgabefeld.setText(format.format(first)+"-");
  451. }else {
  452.  
  453. }
  454. }
  455. });
  456. button_sub.setBounds(208, 220, 50, 50);
  457. frame.getContentPane().add(button_sub);
  458.  
  459. JButton button_div = new JButton("/");
  460. button_div.addActionListener(new ActionListener() {
  461. public void actionPerformed(ActionEvent e) {
  462. art=3;
  463. c=false;
  464. zeichen=("/");
  465. if(n==1) {
  466. n=2;
  467. onlysec=false;
  468. ausgabefeld.setText(format.format(first)+"/");
  469. }else {
  470.  
  471. }
  472. }
  473. });
  474. button_div.setBounds(208, 281, 50, 50);
  475. frame.getContentPane().add(button_div);
  476.  
  477.  
  478.  
  479. JButton button_mult = new JButton("*");
  480. button_mult.addActionListener(new ActionListener() {
  481. public void actionPerformed(ActionEvent e) {
  482. art=4;
  483. c=false;
  484. zeichen=("*");
  485. if(n==1) {
  486. n=2;
  487. onlysec=false;
  488. ausgabefeld.setText(format.format(first)+"*");
  489. }else {
  490.  
  491. }
  492. }
  493. });
  494. button_mult.setBounds(208, 342, 50, 50);
  495. frame.getContentPane().add(button_mult);
  496.  
  497. JButton button_ergebnis = new JButton("=");
  498. button_ergebnis.setFont(new Font("Rockwell", Font.PLAIN, 32));
  499. button_ergebnis.addActionListener(new ActionListener() {
  500. public void actionPerformed(ActionEvent e) {
  501. n=3;
  502. c=false;
  503. double erg=0;
  504. switch(art) {
  505. case 1:
  506. erg=(first+sec);
  507. ausgabefeld.setText(""+format.format(erg));
  508. break;
  509. case 2:
  510. erg=(first-sec);
  511. ausgabefeld.setText(""+format.format(erg));
  512. break;
  513. case 3:
  514. erg=(first/sec);
  515. ausgabefeld.setText(""+format.format(erg));
  516. break;
  517. case 4:
  518. erg=(first*sec);
  519. ausgabefeld.setText(""+format.format(erg));
  520. break;
  521. }
  522.  
  523. first=erg;
  524. sec=0;
  525. n=1;
  526. art=0;
  527. onlysec=true;
  528.  
  529. }
  530. });
  531. button_ergebnis.setBounds(3, 403, 255, 50);
  532. frame.getContentPane().add(button_ergebnis);
  533.  
  534. JButton button_delete = new JButton("C");
  535. button_delete.addActionListener(new ActionListener() {
  536. public void actionPerformed(ActionEvent e) {
  537. first=0;
  538. sec=0;
  539. c=false;
  540. n=1;
  541. art=0;
  542. ausgabefeld.setText("");
  543. }
  544. });
  545. button_delete.setBounds(3, 342, 50, 50);
  546. frame.getContentPane().add(button_delete);
  547.  
  548. JButton button_comma = new JButton(",");
  549. button_comma.addActionListener(new ActionListener() {
  550. public void actionPerformed(ActionEvent e) {
  551. c=true;
  552. if(n==1) {
  553. ausgabefeld.setText(format.format(first)+",");
  554. }else if(n==2) {
  555. ausgabefeld.setText(format.format(first)+zeichen+format.format(sec)+",");
  556. }else {
  557.  
  558. }
  559. }
  560. });
  561. button_comma.setBounds(123, 342, 50, 50);
  562. frame.getContentPane().add(button_comma);
  563.  
  564.  
  565. }
  566. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement