Guest User

Untitled

a guest
Sep 3rd, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class English extends JPanel {
  6.     private JButton display;
  7.     private JPanel panel;
  8.     private double result;
  9.     private String lastCommand;
  10.     private boolean start;
  11.    
  12.     public English() {
  13.         setLayout(new BorderLayout());
  14.         result = 0;
  15.         lastCommand = "=";
  16.         start = true;
  17.         display = new JButton("0");
  18.         display.setEnabled(false);
  19.         add(display, BorderLayout.NORTH);
  20.        
  21.         ActionListener insert = new InsertAction();
  22.         ActionListener command = new CommandAction();
  23.        
  24.         panel = new JPanel();
  25.         panel.setLayout(new GridLayout(4, 4));
  26.        
  27.         addButton("7", insert);
  28.         addButton("8", insert);
  29.         addButton("9", insert);
  30.         addButton("/", command);
  31.         addButton("4", insert);
  32.         addButton("5", insert);
  33.         addButton("6", insert);
  34.         addButton("*", command);
  35.         addButton("1", insert);
  36.         addButton("2", insert);
  37.         addButton("3", insert);
  38.         addButton("-", command);
  39.         addButton("0", insert);
  40.         addButton(".", insert);
  41.         addButton("=", command);
  42.         addButton("+", command);
  43.        
  44.         add(panel, BorderLayout.CENTER);
  45.     }
  46.    
  47.     private void addButton(String label, ActionListener listener) {
  48.         JButton button = new JButton(label);
  49.         button.addActionListener(listener);
  50.         panel.add(button);
  51.     }
  52.    
  53.     private class InsertAction implements ActionListener {
  54.         public void actionPerfomed(ActionEvent event) {
  55.             String input = event.getActionCommand();
  56.             if (start) {
  57.                 display.setText("");
  58.                 start = false;
  59.             }
  60.             display.setText(display.getText() + input);
  61.         }
  62.     }
  63.    
  64.     private class CommandAction implements ActionListener {
  65.         public void actionPerfomed(ActionEvent event) {
  66.             String command = event.getActionCommand();
  67.             if (start) {
  68.                 if (command.equals("-")) {
  69.                     display.setText(command);
  70.                     start = false;
  71.                 } else {
  72.                     calculate(Double.parseDouble(display.getText()));
  73.                     lastCommand = command;
  74.                     start = true;
  75.                 }
  76.             }
  77.         }
  78.        
  79.         public void calculate(double x) {
  80.             if (lastCommand.equals("+")) result += x;
  81.             else if (lastCommand.equals("-")) result -= x;
  82.             else if (lastCommand.equals("*")) result *= x;
  83.             else if (lastCommand.equals("/")) result /= x;
  84.             else if (lastCommand.equals("=")) result = x;
  85.             display.setText("" + result);
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment