Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.TextComponent.*;
  5. import java.awt.event.TextListener.*;
  6. class LabelExample {
  7. public static void main(String args[]) {
  8.  
  9. Frame f = new Frame("Calculator");
  10. f.setSize(400, 450);
  11. Button b1 = new Button("1");
  12. Button b2 = new Button("2");
  13. Button b3 = new Button("3");
  14. Button b4 = new Button("4");
  15. Button b5 = new Button("5");
  16. Button b6 = new Button("6");
  17. Button b7 = new Button("7");
  18. Button b8 = new Button("8");
  19. Button b9 = new Button("9");
  20. Button plus = new Button("+");
  21. //Button minus = new Button("-");
  22. plus.setBounds(70, 345, 70, 50);
  23. f.add(plus);
  24. // minus.setBounds(270, 345, 70, 50);
  25. // f.add(minus);
  26. b1.setBounds(70, 105, 70, 50);
  27. f.add(b1);
  28. b2.setBounds(170, 105, 70, 50);
  29. f.add(b2);
  30. b3.setBounds(270, 105, 70, 50);
  31. f.add(b3);
  32. b4.setBounds(70, 185, 70, 50);
  33. f.add(b4);
  34. b5.setBounds(170, 185, 70, 50);
  35. f.add(b5);
  36. b6.setBounds(270, 185, 70, 50);
  37. f.add(b6);
  38. b7.setBounds(70, 265, 70, 50);
  39. f.add(b7);
  40. b8.setBounds(170, 265, 70, 50);
  41. f.add(b8);
  42. b9.setBounds(270, 265, 70, 50);
  43. f.add(b9);
  44. Button ednakvo = new Button("=");
  45. ednakvo.setBounds(170, 345, 70, 50);
  46. f.add(ednakvo);
  47. TextField t1, t2;
  48. t2 = new TextField();
  49. t2.setText("0");
  50. t2.setVisible(false);
  51. t1 = new TextField();
  52. t1.setBounds(70, 50, 270, 25);
  53. f.add(t1);
  54. t2.setBounds(70, 150, 70, 25);
  55. f.add(t2);
  56. b1.addActionListener(new ActionListener() {
  57. public void actionPerformed(ActionEvent e) {
  58. String a = t1.getText();
  59. t1.setText(a + "1");
  60. }
  61. });
  62. b2.addActionListener(new ActionListener() {
  63. public void actionPerformed(ActionEvent e) {
  64. String a = t1.getText();
  65. t1.setText(a + "2");
  66. }
  67. });
  68. b3.addActionListener(new ActionListener() {
  69. public void actionPerformed(ActionEvent e) {
  70. String a = t1.getText();
  71. t1.setText(a + "3");
  72. }
  73. });
  74. b4.addActionListener(new ActionListener() {
  75. public void actionPerformed(ActionEvent e) {
  76. String a = t1.getText();
  77. t1.setText(a + "4");
  78. }
  79. });
  80. b5.addActionListener(new ActionListener() {
  81. public void actionPerformed(ActionEvent e) {
  82. String a = t1.getText();
  83. t1.setText(a + "5");
  84. }
  85. });
  86. b6.addActionListener(new ActionListener() {
  87. public void actionPerformed(ActionEvent e) {
  88. String a = t1.getText();
  89. t1.setText(a + "7");
  90. }
  91. });
  92. b7.addActionListener(new ActionListener() {
  93. public void actionPerformed(ActionEvent e) {
  94. String a = t1.getText();
  95. t1.setText(a + "7");
  96. }
  97. });
  98. b8.addActionListener(new ActionListener() {
  99. public void actionPerformed(ActionEvent e) {
  100. String a = t1.getText();
  101. t1.setText(a + "8");
  102. }
  103. });
  104. b9.addActionListener(new ActionListener() {
  105. public void actionPerformed(ActionEvent e) {
  106. String a = t1.getText();
  107. t1.setText(a + "9");
  108. }
  109. });
  110.  
  111. plus.addActionListener(new ActionListener() {
  112. public void actionPerformed(ActionEvent e) {
  113. String a = t1.getText();
  114. t1.setText("");
  115. int a1 = Integer.parseInt(a);
  116. String b = t2.getText();
  117. int b1 = Integer.parseInt(b);
  118. int c1 = a1 + b1;
  119. String c = Integer.toString(c1);
  120. t2.setText(c);
  121.  
  122.  
  123. }
  124. });
  125. ednakvo.addActionListener(new ActionListener() {
  126. @Override
  127. public void actionPerformed(ActionEvent e) {
  128. String a = t1.getText();
  129. int a1 = Integer.parseInt(a);
  130. String b = t2.getText();
  131. int b1 = Integer.parseInt(b);
  132. int c1 = a1 + b1;
  133. String c = Integer.toString(c1);
  134. t1.setText(c);
  135. t2.setText("0");
  136.  
  137. }
  138. });
  139. f.setLayout(null);
  140. f.setVisible(true);
  141. f.addWindowListener(new WindowAdapter() {
  142. public void windowClosing(WindowEvent e) {
  143. super.windowClosing(e);
  144. System.exit(0);
  145. }
  146. });
  147.  
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement