Advertisement
corot

Untitled

Nov 17th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. import java.util.*;
  2. import java.awt.*;
  3. //import java.awt.ActionListener;
  4. //Leornado da vinte kkkk
  5. import javax.swing.*;
  6. class Calc{
  7. static char aux=' ';
  8. static int num1=0,num2=0,result=0;
  9.  
  10. static String n="";
  11. public static void main(String args[]){
  12.  
  13. JFrame jan=new JFrame();
  14.  
  15. jan.setVisible(true);
  16. jan.setResizable(false);
  17.  
  18.  
  19. jan.setBounds(0,0,0,0);
  20. jan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. jan.setSize(240,160);
  22. JPanel painel = new JPanel();
  23.  
  24. jan.add(painel);
  25.  
  26. JButton btnresult = new JButton("+");
  27. JButton btnSubt = new JButton("-");
  28. JButton btnMult = new JButton("*");
  29. JButton btnDiv = new JButton("/");
  30. JButton btnIgual = new JButton("=");
  31. JButton btnApagar =new JButton("CE");
  32.  
  33. JButton btnOne = new JButton("1");
  34. JButton btnTwo = new JButton("2");
  35. JButton btnThree = new JButton("3");
  36. JButton btnFour = new JButton("4");
  37. JButton btnFive = new JButton("5");
  38. JButton btnSix = new JButton("6");
  39. JButton btnSeven = new JButton("7");
  40. JButton btnHeigh = new JButton("8");
  41. JButton btnNine = new JButton("9");
  42. JButton btnZero = new JButton("0");
  43.  
  44.  
  45. JTextField campo = new JTextField(10);
  46. jan.setTitle("Calculadora");
  47.  
  48. jan.add(painel);
  49. painel.add(campo);
  50. painel.add(btnresult);
  51. painel.add(btnSubt);
  52. painel.add(btnMult);
  53. painel.add(btnDiv);
  54. painel.add(btnApagar);
  55.  
  56. painel.add(btnOne);
  57. painel.add(btnTwo);
  58. painel.add(btnThree);
  59. painel.add(btnFour);
  60. painel.add(btnFive);
  61. painel.add(btnSix);
  62. painel.add(btnSeven);
  63. painel.add(btnHeigh);
  64. painel.add(btnNine);
  65. painel.add(btnZero);
  66. painel.add(btnIgual);
  67.  
  68. //JOptionPane.showMessageDialog(null,"oo");
  69.  
  70.  
  71. btnOne.addActionListener(new java.awt.event.ActionListener(){
  72. public void actionPerformed(java.awt.event.ActionEvent e){
  73. n+="1";
  74. campo.setText(n);
  75.  
  76.  
  77. if(aux==' '){
  78. num1=1;
  79. //a1++;
  80. }else{
  81. // a1=0;
  82. num2=1;
  83. }
  84.  
  85. //JOptionPane.showMessageDialog(null,num1);
  86. //JOptionPane.showMessageDialog(null,num2);
  87. }
  88.  
  89. });
  90.  
  91. btnTwo.addActionListener(new java.awt.event.ActionListener(){
  92. public void actionPerformed(java.awt.event.ActionEvent e){
  93. n+="2";
  94. campo.setText(n);
  95. if (aux==' '){
  96. num1=2;
  97.  
  98. }else{
  99. num2=2;
  100. }
  101. }
  102. });
  103.  
  104. btnThree.addActionListener(new java.awt.event.ActionListener(){
  105. public void actionPerformed(java.awt.event.ActionEvent e){
  106. n+="3";
  107. campo.setText(n);
  108. if(aux==' '){
  109. num1=3;
  110. }else{
  111. num2=3;
  112. }
  113.  
  114.  
  115. }
  116.  
  117. });
  118.  
  119. btnFour.addActionListener(new java.awt.event.ActionListener(){
  120. public void actionPerformed(java.awt.event.ActionEvent e){
  121. n+="4";
  122. campo.setText(n);
  123. if(aux==' '){
  124. num1=4;
  125. }else{
  126. num2=4;
  127. }
  128. }
  129.  
  130. });
  131. btnFive.addActionListener(new java.awt.event.ActionListener(){
  132. public void actionPerformed(java.awt.event.ActionEvent e){
  133. n+="5";
  134. campo.setText(n);
  135. if(aux==' '){
  136. num1=5;
  137. }else{
  138. num2=5;
  139. }
  140. }
  141.  
  142. });
  143. btnSix.addActionListener(new java.awt.event.ActionListener(){
  144. public void actionPerformed(java.awt.event.ActionEvent e){
  145. n+="6";
  146. campo.setText(""+n);
  147. if(aux==' '){
  148. num1=6;
  149. }else{
  150. num2=6;
  151. }
  152. }
  153.  
  154. });
  155. btnSeven.addActionListener(new java.awt.event.ActionListener(){
  156. public void actionPerformed(java.awt.event.ActionEvent e){
  157. n+="7";
  158. campo.setText(n);
  159. if(aux==' '){
  160. num1=7;
  161. }else{
  162. num2=7;
  163. }
  164. }
  165.  
  166. });
  167. btnHeigh.addActionListener(new java.awt.event.ActionListener(){
  168. public void actionPerformed(java.awt.event.ActionEvent e){
  169. n+="8";
  170. campo.setText(n);
  171. if(aux==' '){
  172. num1=8;
  173. }else{
  174. num2=8;
  175. }
  176.  
  177. }
  178.  
  179. });
  180. btnNine.addActionListener(new java.awt.event.ActionListener(){
  181. public void actionPerformed(java.awt.event.ActionEvent e){
  182. n+="9";
  183. campo.setText(n);
  184. if(aux==' '){
  185. num1=9;
  186. }else{
  187. num2=9;
  188. }
  189. }
  190.  
  191. });
  192. btnZero.addActionListener(new java.awt.event.ActionListener(){
  193. public void actionPerformed(java.awt.event.ActionEvent e){
  194. n+="0";
  195. campo.setText(n);
  196. if(aux==' '){
  197. num1=0;
  198. }else{
  199. num2=0;
  200. }
  201.  
  202. }
  203.  
  204. });
  205.  
  206. btnApagar.addActionListener(new java.awt.event.ActionListener(){
  207. public void actionPerformed(java.awt.event.ActionEvent e){
  208. n="";
  209. campo.setText(n);
  210. num1=0;
  211. num2=0;
  212. // aux_n=0;
  213. }
  214.  
  215. });
  216. btnresult.addActionListener(new java.awt.event.ActionListener(){
  217. public void actionPerformed(java.awt.event.ActionEvent e){
  218.  
  219. n+="+";
  220. campo.setText(n);
  221. aux='+';
  222.  
  223. }
  224.  
  225. });
  226.  
  227. btnSubt.addActionListener(new java.awt.event.ActionListener(){
  228. public void actionPerformed(java.awt.event.ActionEvent e){
  229. aux='-';
  230. n+="-";
  231. campo.setText(n);
  232. }
  233. });//fim btnSubt
  234.  
  235. btnMult.addActionListener(new java.awt.event.ActionListener(){
  236. public void actionPerformed(java.awt.event.ActionEvent e){
  237. aux='*';
  238. n+="*";
  239. campo.setText(n);
  240. }
  241. });
  242. btnDiv.addActionListener(new java.awt.event.ActionListener(){
  243. public void actionPerformed(java.awt.event.ActionEvent e){
  244. aux='/';
  245. n+="/";
  246. campo.setText(n);
  247. }
  248. });
  249.  
  250. btnIgual.addActionListener(new java.awt.event.ActionListener(){
  251. public void actionPerformed(java.awt.event.ActionEvent e){
  252. //campo.setText("o valor de num1 "+num1);
  253. switch(aux){
  254. case '+':
  255. result=0;
  256. result=num1+num2;
  257. num1=0;
  258. num2=0;
  259. campo.setText(" "+result);
  260. break;
  261. case '-':
  262. result=0;
  263. result=num1-num2;
  264. num1=0;
  265. num2=0;
  266. campo.setText(" "+result);
  267. break;
  268. case '*':
  269. result=0;
  270. result=num1*num2;
  271. num1=0;
  272. num2=0;
  273. campo.setText(" "+result);
  274. break;
  275.  
  276. case '/':
  277. result=0;
  278. if(num2!=0){
  279. result=num1/num2;
  280. }else{
  281. campo.setText("Erro!");
  282. }
  283. campo.setText(" "+result);
  284. break;
  285.  
  286. default:
  287. campo.setText("Erro!");
  288. break;
  289.  
  290. }
  291. result=0;
  292. n=" ";
  293. num1=0;
  294. num2=0;
  295. aux=' ';
  296.  
  297. }
  298. //result=0;
  299. });
  300. //fim btnIgual
  301.  
  302.  
  303.  
  304.  
  305. }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement