Advertisement
Guest User

Untitled

a guest
Oct 29th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. /**
  6. *
  7. * @author aman
  8. */
  9. public class Calculator extends JPanel {
  10. private JButton display;
  11. private JPanel panel;
  12. private double result;
  13. private String lastCommand;
  14. private boolean start;
  15.  
  16. /**
  17. * Creates new form Calculator
  18. */
  19. public Calculator() {
  20. setLayout(new BorderLayout());
  21. result=0;
  22. lastCommand= "=";
  23. start =true;
  24.  
  25. display=new JButton("0");
  26. display.setEnabled(false);
  27. add(display,BorderLayout.NORTH);
  28.  
  29. ActionListener insert= new InsertAction();
  30. ActionListener command= new CommandAction();
  31.  
  32. panel= new JPanel();
  33. panel.setLayout(new GridLayout(4,4));
  34.  
  35. addButton("7",insert);
  36. addButton("8",insert);
  37. addButton("9",insert);
  38. addButton("/",command);
  39.  
  40. addButton("4",insert);
  41. addButton("5",insert);
  42. addButton("6",insert);
  43. addButton("*",command);
  44.  
  45. addButton("1",insert);
  46. addButton("2",insert);
  47. addButton("3",insert);
  48. addButton("-",command);
  49.  
  50. addButton("0",insert);
  51. addButton(".",insert);
  52. addButton("=",insert);
  53. addButton("+",insert);
  54.  
  55. add(panel,BorderLayout.CENTER);
  56.  
  57.  
  58.  
  59. }
  60. public void addButton(String label,ActionListener listener)
  61. {
  62. JButton button= new JButton(label);
  63. button.addActionListener(listener);
  64. panel.add(button);
  65. }
  66.  
  67. /**
  68. * This method is called from within the constructor to initialize the form.
  69. * WARNING: Do NOT modify this code. The content of this method is always
  70. * regenerated by the Form Editor.
  71. */
  72. @SuppressWarnings("unchecked")
  73. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  74. private void initComponents() {
  75.  
  76. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  77.  
  78. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  79. getContentPane().setLayout(layout);
  80. layout.setHorizontalGroup(
  81. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82. .addGap(0, 400, Short.MAX_VALUE)
  83. );
  84. layout.setVerticalGroup(
  85. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  86. .addGap(0, 300, Short.MAX_VALUE)
  87. );
  88.  
  89. pack();
  90. }// </editor-fold>
  91.  
  92. /**
  93. * @param args the command line arguments
  94. */
  95. public static void main(String args[]) {
  96. /* Set the Nimbus look and feel */
  97. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  98. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  99. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  100. */
  101. try {
  102. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  103. if ("Nimbus".equals(info.getName())) {
  104. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  105. break;
  106. }
  107. }
  108. } catch (ClassNotFoundException ex) {
  109. java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  110. } catch (InstantiationException ex) {
  111. java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  112. } catch (IllegalAccessException ex) {
  113. java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  114. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  115. java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  116. }
  117. //</editor-fold>
  118.  
  119. /* Create and display the form */
  120. java.awt.EventQueue.invokeLater(new Runnable() {
  121.  
  122. public void run() {
  123. new Calculator().setVisible(true);
  124.  
  125.  
  126. }
  127. });
  128. }
  129.  
  130. // Variables declaration - do not modify
  131. // End of variables declaration
  132.  
  133. private class InsertAction implements ActionListener {
  134.  
  135. public InsertAction() {
  136. }
  137.  
  138. @Override
  139. public void actionPerformed(ActionEvent e) {
  140. String input=e.getActionCommand();
  141. if (start)
  142. {
  143. display.setText("");
  144. start=false;
  145. } else {
  146. }
  147. display.setText(display.getText()+input);
  148. }
  149. }
  150.  
  151. private class CommandAction implements ActionListener {
  152.  
  153. public CommandAction() {
  154. }
  155.  
  156. @Override
  157. public void actionPerformed(ActionEvent e) {
  158. String command=e.getActionCommand();
  159. if (start)
  160. {
  161. if(command.equals("-"))
  162. {
  163. display.setText(command);
  164. start=false;
  165. }
  166. else
  167. lastCommand=command;
  168. }
  169. else
  170. {
  171. calculate(Double.parseDouble(display.getText()));
  172. lastCommand=command;
  173. start=true;
  174. }
  175.  
  176.  
  177. }
  178.  
  179. private void calculate(double x) {
  180. if(lastCommand.equals("+")) result +=x;
  181. else if(lastCommand.equals("-"))result -=x;
  182. else if(lastCommand.equals("*"))result *=x;
  183. else if(lastCommand.equals("/"))result /=x;
  184. else if(lastCommand.equals("="))result =x;
  185. display.setText(""+result);
  186.  
  187. }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement