Advertisement
devankransteuber

Untitled

Mar 5th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.GridBagConstraints;
  5. import java.awt.GridLayout;
  6. import java.awt.TextArea;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12.  
  13. public class Window{
  14.  
  15.     static int i;
  16.     JButton buttons;
  17.    
  18.     public static void main(String args[]){
  19.    
  20.     JFrame frame = new JFrame("Calculator");
  21.     frame.setVisible(true);
  22.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.     frame.setResizable(false);
  24.    
  25.     TextArea text = new TextArea(2,25);
  26.     text.setEditable(false);
  27.    
  28.     JPanel gui = new JPanel(new BorderLayout(5,5));
  29.     frame.setContentPane(gui);
  30.     gui.add(text, BorderLayout.NORTH);
  31.     JPanel panelButtons = new JPanel(new GridLayout(3,0,3,3));
  32.    
  33.     JPanel buttonPanel = new JPanel();
  34.     buttonPanel.setVisible(true);
  35.     buttonPanel.setPreferredSize(new Dimension(150,200));
  36.     frame.getContentPane().add(buttonPanel);
  37.  
  38.     Font font = new Font("SansSerif", Font.PLAIN, 30);
  39.  
  40.     JButton[] buttons = new JButton[10];
  41.  
  42.     for(i=0; i<buttons.length; i++){
  43.         buttons[i] = new JButton(String.valueOf(i));
  44.  
  45.         buttons[i].setFont(font);
  46.     }
  47.    
  48.     JButton plusButton = new JButton("+");
  49.     JButton minusButton = new JButton("-");
  50.     JButton equalsButton = new JButton("=");
  51.     JButton clearButton = new JButton("C");
  52.    
  53.     ActionListener listener = new ActionListener() {
  54.    
  55.     public void actionPerformed(ActionEvent e) {
  56.    
  57.     JButton source = (JButton) e.getSource();
  58.  
  59.     if(source.getText().equals("1")) {
  60.        text.append("1");
  61.     }
  62.     if(source.getText().equals("2")) {
  63.         text.append("2");
  64.      }
  65.     if(source.getText().equals("3")) {
  66.         text.append("3");
  67.      }
  68.     if(source.getText().equals("4")) {
  69.         text.append("4");
  70.      }
  71.     if(source.getText().equals("0")) {
  72.         text.append("0");
  73.      }
  74.     if(source.getText().equals("5")) {
  75.         text.append("5");
  76.      }
  77.     if(source.getText().equals("6")){
  78.         text.append("6");
  79.     }
  80.     if(source.getText().equals("7")){
  81.         text.append("7");
  82.     }
  83.     if(source.getText().equals("8")){
  84.         text.append("8");
  85.     }
  86.     if(source.getText().equals("9")){
  87.         text.append("9");
  88.     }
  89.     if(source.getText().equals("+")){
  90.         text.append("+");
  91.     }
  92.     if(source.getText().equals("-")){
  93.         text.append("-");
  94.     }
  95.     if(source.getText().equals("C")){
  96.         text.setText(" ");
  97.     }
  98.     if(source.getText().equals("=")){
  99.         String textStuff = text.getText();
  100.         if(textStuff.contains("-")){
  101.             String[] parts = textStuff.split("-");
  102.             String part1 = parts[0];
  103.             String part2 = parts[1];
  104.             double num1 = Double.parseDouble(parts[0]);
  105.             double num2 = Double.parseDouble(parts[1]);
  106.             double total = (num1 - num2);
  107.             String finalNum = Double.toString(total);
  108.             text.setText(finalNum);
  109.         }else{
  110.             String[] parts = textStuff.split("[+]");
  111.             String part1 = parts[0];
  112.             String part2 = parts[1];
  113.             double num1 = Double.parseDouble(parts[0]);
  114.             double num2 = Double.parseDouble(parts[1]);
  115.             double total = (num1 + num2);
  116.             String finalNum = Double.toString(total);
  117.             text.setText(finalNum);
  118.         }
  119.        
  120.     }
  121.     }
  122.    
  123.     };
  124.    
  125.     for(i=0; i<buttons.length; i++){
  126.             buttons[i].addActionListener(listener);
  127.         }
  128.  
  129.      minusButton.addActionListener(listener);
  130.      plusButton.addActionListener(listener);
  131.      equalsButton.addActionListener(listener);
  132.      clearButton.addActionListener(listener);
  133.        
  134.  
  135.     GridBagConstraints c = new GridBagConstraints();
  136.  
  137.     for(i=0; i<buttons.length; i++){
  138.             panelButtons.add(buttons[i], c);
  139.         }
  140.  
  141.      panelButtons.add(minusButton);
  142.      panelButtons.add(plusButton);
  143.      panelButtons.add(equalsButton);
  144.      panelButtons.add(clearButton);
  145.  
  146.      gui.add(panelButtons, BorderLayout.CENTER);
  147.      frame.pack();
  148.  
  149.  
  150.  
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement