Advertisement
Guest User

Untitled

a guest
May 5th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. // Simplified P5 with one button for addition
  2. // Author: Irena Pevac
  3. // Date: Nov 23, 2013
  4. // Project: Calculator
  5. // Event driven programming
  6. import java.text.DecimalFormat;
  7. import java.awt.*;
  8. import javax.swing.*;
  9. import java.awt.event.*;
  10.  
  11. public class Calculator extends JApplet implements ActionListener
  12. {
  13. private String text1,text2,textResult;
  14. private JLabel label,labelEast,labelWest, labelResult;
  15. private JButton buttonPlus, buttonSubtract, buttonMultiply, buttonDivide, buttonModulo;
  16. private JTextField t1, t2;
  17. private JPanel panel, northPanel, midPanel, eastPanel, westPanel,
  18. southPanel;
  19. private int n1, n2, nResult;
  20. private double one,two,Result;
  21. private Font f = new Font("Arial", Font.BOLD, 22);
  22.  
  23. public void init()
  24. {
  25. BorderLayout bLayout = new BorderLayout();
  26. GridLayout gLayout = new GridLayout(4,1);
  27. panel = new JPanel();
  28. panel.setLayout(bLayout);
  29. text1 = "";
  30. text2 = "";
  31. textResult = "";
  32. t1 = new JTextField(10);
  33. t1.setFont(new Font("Arial", Font.BOLD, 16));
  34. t1.setText(text1);
  35. t2 = new JTextField(10);
  36. t2.setFont(new Font("Arial", Font.BOLD, 16));
  37. t2.setText(text2);
  38. label = new JLabel("Type in two integer numbers and " +
  39. "press operation to calculate result.");
  40. labelEast = new JLabel( " Enter number2 ");
  41. labelWest = new JLabel( " Enter number1 ");
  42. labelResult = new JLabel(" Result: ");
  43. labelResult.setFont(f);
  44.  
  45. northPanel = new JPanel();
  46. northPanel.setPreferredSize(new Dimension ( 500, 50));
  47. eastPanel = new JPanel();
  48. eastPanel.setPreferredSize(new Dimension ( 200, 100));
  49. westPanel = new JPanel();
  50. westPanel.setPreferredSize(new Dimension ( 200, 100));
  51. midPanel = new JPanel();
  52. midPanel.setPreferredSize(new Dimension ( 100, 100));
  53. midPanel.setBackground(Color.yellow);
  54. southPanel = new JPanel();
  55. southPanel.setPreferredSize(new Dimension ( 500, 250));
  56.  
  57. eastPanel.add(labelEast);
  58. eastPanel.add(t2);
  59. westPanel.add(labelWest);
  60. westPanel.add(t1);
  61. northPanel.add(label);
  62. southPanel.add(labelResult);
  63.  
  64. midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS));
  65. buttonPlus = (new JButton(" + "));
  66. buttonPlus.setFont(f);
  67. buttonPlus.setAlignmentX(Component.CENTER_ALIGNMENT);
  68. buttonPlus.setAlignmentY(Component.CENTER_ALIGNMENT);
  69. midPanel.add(buttonPlus);
  70. buttonSubtract = (new JButton(" - "));
  71. buttonSubtract.setFont(f);
  72. buttonSubtract.setAlignmentX(Component.CENTER_ALIGNMENT);
  73. buttonSubtract.setAlignmentY(Component.CENTER_ALIGNMENT);
  74. midPanel.add(buttonSubtract);
  75. buttonMultiply= (new JButton(" * "));
  76. buttonMultiply.setFont(f);
  77. buttonMultiply.setAlignmentX(Component.CENTER_ALIGNMENT);
  78. buttonMultiply.setAlignmentY(Component.CENTER_ALIGNMENT);
  79. midPanel.add(buttonMultiply);
  80. buttonDivide= (new JButton(" / "));
  81. buttonDivide.setFont(f);
  82. buttonDivide.setAlignmentX(Component.CENTER_ALIGNMENT);
  83. buttonDivide.setAlignmentY(Component.CENTER_ALIGNMENT);
  84. midPanel.add(buttonDivide);
  85. buttonModulo= (new JButton(" % "));
  86. buttonModulo.setFont(f);
  87. buttonModulo.setAlignmentX(Component.CENTER_ALIGNMENT);
  88. buttonModulo.setAlignmentY(Component.CENTER_ALIGNMENT);
  89. midPanel.add(buttonModulo);
  90.  
  91. panel.add(northPanel, BorderLayout.NORTH);
  92. panel.add(southPanel, BorderLayout.SOUTH);
  93. panel.add(eastPanel, BorderLayout.EAST);
  94. panel.add(westPanel, BorderLayout.WEST);
  95. panel.add(midPanel, BorderLayout.CENTER);
  96.  
  97. buttonPlus.addActionListener(this);
  98. buttonSubtract.addActionListener(this);
  99. buttonMultiply.addActionListener(this);
  100. buttonDivide.addActionListener(this);
  101. buttonModulo.addActionListener(this);
  102. add(panel);
  103. t1.setFocusable(true);
  104. }
  105.  
  106. public void actionPerformed(ActionEvent e)
  107. {
  108. if (e.getSource() == buttonPlus)
  109. {
  110. text1 = t1.getText();
  111. n1 = Integer.parseInt(text1);
  112. text2 = t2.getText();
  113. n2 = Integer.parseInt(text2);
  114. nResult = n1+n2;
  115. labelResult.setText( "RESULT: " + text1 + " + " + text2 + " = " + nResult);
  116. }
  117. if(e.getSource()== buttonSubtract)
  118. {
  119. text1 = t1.getText();
  120. n1 = Integer.parseInt(text1);
  121. text2 = t2.getText();
  122. n2 = Integer.parseInt(text2);
  123. nResult = n1-n2;
  124. labelResult.setText( "RESULT: " + text1 + " - " + text2 + " = " + nResult);
  125. }
  126. if(e.getSource()== buttonMultiply)
  127. {
  128. text1 = t1.getText();
  129. n1 = Integer.parseInt(text1);
  130. text2 = t2.getText();
  131. n2 = Integer.parseInt(text2);
  132. nResult = n1*n2;
  133. labelResult.setText( "RESULT: " + text1 + " * " + text2 + " = " + nResult);
  134. }
  135. if(e.getSource()== buttonDivide)
  136. {
  137. DecimalFormat twoDForm = new DecimalFormat("#.##");
  138. text1 = t1.getText();
  139. one = Double.parseDouble(text1);
  140. text2 = t2.getText();
  141. two = Double.parseDouble(text2);
  142. Result = one/two;
  143. labelResult.setText( "RESULT: " + text1 + " / " + text2 + " = " + twoDForm.format(Result));
  144. }
  145. if(e.getSource()== buttonModulo)
  146. {
  147. text1 = t1.getText();
  148. n1 = Integer.parseInt(text1);
  149. text2 = t2.getText();
  150. n2 = Integer.parseInt(text2);
  151. nResult = n1%n2;
  152. labelResult.setText( "RESULT: " + text1 + " % " + text2 + " = " + nResult);
  153. }
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement