eimarhiagbe

calc1

May 4th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.util.Vector;
  6.  
  7. public class CalculatorLab extends JFrame implements ActionListener
  8. {
  9. private JPanel keypad;
  10. private JPanel inputs;
  11. private JTextField firstInput = new JTextField(25);
  12. private JTextField secondInput = new JTextField(25);
  13. private JTextField results = new JTextField(25);
  14. private JLabel operatorLbl = new JLabel("+");
  15. private JLabel equalsLbl = new JLabel("=");
  16. private String operator;
  17. private boolean first = true;
  18. private boolean second = false;
  19.  
  20. public static void main(String[] args)
  21. {
  22. CalculatorLab frame = new CalculatorLab();
  23. frame.setSize(500,300);
  24. frame.setVisible(true);
  25. }
  26.  
  27. public CalculatorLab()
  28. {
  29. super( "My Calculator" );
  30. init();
  31. }
  32.  
  33. private void init()
  34. {
  35. setLayout( new BorderLayout() );
  36. keypad = new JPanel(new GridLayout(6,3));
  37. String[] labels = {"0","1","2","3","4","5","6","7","8","9","+","-","*","/","="," ","Clear"," "};
  38. for (int i = 0; i < labels.length; i++)
  39. {
  40. keypad.add(createButton(labels[i]));
  41. }
  42. add("East", keypad);
  43.  
  44. JButton clear = new JButton("Clear");
  45.  
  46. inputs = new JPanel(new GridLayout(4,2));
  47. inputs.add(new JLabel("Enter First Input:"));
  48. inputs.add(firstInput);
  49. operatorLbl.setHorizontalAlignment(SwingConstants.RIGHT);
  50. inputs.add(operatorLbl);
  51. inputs.add(new JLabel(""));
  52. inputs.add(new JLabel("Enter Second Input:"));
  53. inputs.add(secondInput);
  54. equalsLbl.setHorizontalAlignment(SwingConstants.RIGHT);
  55. inputs.add(equalsLbl);
  56. inputs.add(results);
  57. add("Center", inputs);
  58.  
  59. clear.addActionListener(this);
  60.  
  61. }
  62. private JButton createButton( String name )
  63. {
  64. JButton b = new JButton( name );
  65. b.addActionListener( this );
  66. return b;
  67.  
  68. }
  69. public void actionPerformed( ActionEvent e )
  70. {
  71. String cmd = e.getActionCommand();
  72. if (cmd.equals("+")||cmd.equals("-")||cmd.equals("*")||cmd.equals("/"))
  73. {
  74. firstInput.setBackground(null);
  75. secondInput.setBackground(Color.YELLOW);
  76. operator = cmd;
  77. operatorLbl.setText(cmd);
  78. first = false;
  79. second = true;
  80. }
  81. else if (first)
  82. {
  83. firstInput.setBackground(Color.YELLOW);
  84. firstInput.setText(firstInput.getText()+cmd);
  85. results.setText("");
  86. }
  87. else if (cmd.equals("="))
  88. {
  89. secondInput.setBackground(null);
  90. results.setBackground(Color.YELLOW);
  91. second = false;
  92. results.setText(calculateResults());
  93. resetCalc();
  94. }
  95. else if (second)
  96. {
  97. secondInput.setText(secondInput.getText()+cmd);
  98. }
  99. else if (clear)
  100.  
  101. }
  102. public String calculateResults()
  103. {
  104. double resultsValue = 0;
  105. if (operator.equals("+"))
  106. {
  107. resultsValue = Double.parseDouble(firstInput.getText()) + Double.parseDouble(secondInput.getText());
  108. }
  109. else if (operator.equals("-"))
  110. {
  111. resultsValue = Double.parseDouble(firstInput.getText()) - Double.parseDouble(secondInput.getText());
  112. }
  113. else if (operator.equals("*"))
  114. {
  115. resultsValue = Double.parseDouble(firstInput.getText()) * Double.parseDouble(secondInput.getText());
  116. }
  117. else if (operator.equals("/"))
  118. {
  119. resultsValue = Double.parseDouble(firstInput.getText()) / Double.parseDouble(secondInput.getText());
  120. }
  121.  
  122. return Double.toString(resultsValue);
  123. }
  124. public void resetCalc()
  125. {
  126. firstInput.setText("");
  127. secondInput.setText("");
  128. first = true;
  129. second = false;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment