Making a calculator in Java, how to allow numeric inputs only


SUBMITTED BY: Guest

DATE: Oct. 12, 2013, 10:06 p.m.

FORMAT: Text only

SIZE: 6.2 kB

HITS: 1104

  1. import java.awt.Color;
  2. import java.awt.FlowLayout;
  3. import java.awt.LayoutManager;
  4. import java.awt.TextField;
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.JTextArea;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import java.text.ParseException;
  12. import java.util.Scanner;
  13. public class calculator_gui<reutrn> implements ActionListener {
  14. JFrame frame = new JFrame("Calculator");
  15. JPanel Panel = new JPanel (new java.awt.FlowLayout());
  16. JTextArea text = new JTextArea(1,20);
  17. JButton but1= new JButton("1");
  18. JButton but2= new JButton("2");
  19. JButton but3= new JButton("3");
  20. JButton but4= new JButton("4");
  21. JButton but5= new JButton("5");
  22. JButton but6= new JButton("6");
  23. JButton but7= new JButton("7");
  24. JButton but8= new JButton("8");
  25. JButton but9= new JButton("9");
  26. JButton but0= new JButton("0");
  27. JButton butadd= new JButton("+");
  28. JButton butsub= new JButton("-");
  29. JButton butmulti= new JButton("*");
  30. JButton butdiv= new JButton("/");
  31. JButton buteq= new JButton("=");
  32. JButton butclear= new JButton("C");
  33. Double number1,number2,result;
  34. int addc=0,subc=0,multic=0,divc=0;
  35. public void gui(){
  36. Panel.setLayout(FlowLayout());
  37. frame.setVisible(true);
  38. frame.setBounds(100, 100, 450, 285);
  39. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. // frame.setResizable(false);
  41. frame.add(Panel);
  42. Panel.setBackground(Color.green);
  43. Panel.add(text);
  44. text.setBounds(10, 32, 361, 29);
  45. Panel.add(but1);
  46. but1.setBackground(Color.red);
  47. but1.setBounds(10, 81, 89, 23);
  48. Panel.add(but2);
  49. but2.setBounds(126, 81, 89, 23);
  50. Panel.add(but3);
  51. but3.setBounds(225, 81, 89, 23);
  52. Panel.add(but4);
  53. but4.setBounds(10, 115, 89, 23);
  54. Panel.add(but5);
  55. but5.setBounds(126, 115, 89, 23);
  56. Panel.add(but6);
  57. but6.setBounds(225, 115, 89, 23);
  58. Panel.add(but7);
  59. but7.setBounds(10, 149, 89, 23);
  60. Panel.add(but8);
  61. but8.setBounds(126, 149, 89, 23);
  62. Panel.add(but9);
  63. but9.setBounds(225, 149, 89, 23);
  64. Panel.add(but0);
  65. but0.setBounds(126, 183, 89, 23);
  66. Panel.add(butadd);
  67. butadd.setBounds(324, 81, 89, 23);
  68. Panel.add(butsub);
  69. butsub.setBounds(324, 115, 89, 23);
  70. Panel.add(butmulti);
  71. butmulti.setBounds(324, 183, 89, 23);
  72. Panel.add(butdiv);
  73. butdiv.setBounds(324, 149, 89, 23);
  74. Panel.add(buteq);
  75. buteq.setBounds(225, 183, 89, 23);
  76. Panel.add(butclear);
  77. butclear.setBounds(10, 183, 89, 23);
  78. but1.addActionListener(this);
  79. but2.addActionListener(this);
  80. but3.addActionListener(this);
  81. but4.addActionListener(this);
  82. but5.addActionListener(this);
  83. but6.addActionListener(this);
  84. but7.addActionListener(this);
  85. but8.addActionListener(this);
  86. but9.addActionListener(this);
  87. but0.addActionListener(this);
  88. butadd.addActionListener(this);
  89. butsub.addActionListener(this);
  90. butmulti.addActionListener(this);
  91. butdiv.addActionListener(this);
  92. buteq.addActionListener(this);
  93. butclear.addActionListener(this);
  94. }
  95. private LayoutManager FlowLayout() {
  96. // TODO Auto-generated method stub
  97. return null;
  98. }
  99. @Override
  100. public void actionPerformed(ActionEvent e){
  101. Object source = e.getSource();
  102. if(source==butclear){
  103. number1=0.0;
  104. number2=0.0;
  105. text.setText(null);
  106. }
  107. if(source==but1){
  108. text.append("1");
  109. }
  110. if(source==but2){
  111. text.append("2");
  112. }
  113. if(source==but3){
  114. text.append("3");
  115. }
  116. if(source==but4){
  117. text.append("4");
  118. }
  119. if(source==but5){
  120. text.append("5");
  121. }
  122. if(source==but6){
  123. text.append("6");
  124. }
  125. if(source==but7){
  126. text.append("7");
  127. }
  128. if(source==but8){
  129. text.append("8");
  130. }
  131. if(source==but9){
  132. text.append("9");
  133. }
  134. if(source==but0){
  135. text.append("0");
  136. }
  137. if(source==butadd){
  138. number1=number_reader();
  139. text.setText("");
  140. addc=1;
  141. subc=0;
  142. multic=0;
  143. divc=0;
  144. }
  145. if(source==butsub){
  146. number1=number_reader();
  147. text.setText("");
  148. addc=0;
  149. subc=1;
  150. multic=0;
  151. divc=0;
  152. }
  153. if(source==butmulti){
  154. number1=number_reader();
  155. text.setText("");
  156. addc=0;
  157. subc=0;
  158. multic=1;
  159. divc=0;
  160. }
  161. if(source==butdiv){
  162. number1=number_reader();
  163. text.setText("");
  164. addc=0;
  165. subc=0;
  166. multic=0;
  167. divc=1;
  168. }
  169. if(source==buteq){
  170. number2=number_reader();
  171. if(addc>0){
  172. result=number1+number2;
  173. text.setText(Double.toString(result));
  174. }
  175. if(subc>0){
  176. result=number1-number2;
  177. text.setText(Double.toString(result));
  178. }
  179. if(multic>0){
  180. result=number1*number2;
  181. text.setText(Double.toString(result));
  182. }
  183. }
  184. if(divc>0){
  185. result=number1/number2;
  186. text.setText(Double.toString(result));
  187. }
  188. }
  189. public double number_reader(){
  190. Double num1;
  191. String s;
  192. s=text.getText();
  193. num1=Double.valueOf(s);
  194. return num1;
  195. }
  196. }

comments powered by Disqus