Advertisement
Sinux1

FlameBay.java

Oct 10th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.GridLayout;
  3. import java.awt.Panel;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.text.DecimalFormat;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11. import javax.swing.JTextArea;
  12.  
  13. public class FlameBay extends JFrame{
  14.    
  15.     private JButton clearArea = new JButton("Clear");
  16.     private JButton b1 = new JButton("Egg Noodles $1.00");
  17.     private JButton b2 = new JButton("Spaghetti $0.95");
  18.     private JButton b3 = new JButton("Ramen $0.50");
  19.     private JButton b4 = new JButton("Beef $2.00");
  20.     private JButton b5 = new JButton("Chicken $1.50");
  21.     private JButton b6 = new JButton("Pork $1.25");
  22.     private JButton b7 = new JButton("Kung Pao $0.50");
  23.     private JButton b8 = new JButton("Teriyaki $0.25");
  24.     private JButton b9 = new JButton("Curry $0.45");
  25.     private JButton checkout = new JButton("Check Out");
  26.    
  27.    
  28.     String defaultWidth = "\t\t              \r";
  29.     JTextArea area = new JTextArea(25,20);
  30.    
  31.     public FlameBay()
  32.     {
  33.         FoodCallback callback = new FoodCallback(area);
  34.         clearArea.addActionListener(callback);
  35.         b1.addActionListener(callback);
  36.         b2.addActionListener(callback);
  37.         b3.addActionListener(callback);
  38.         b4.addActionListener(callback);
  39.         b5.addActionListener(callback);
  40.         b6.addActionListener(callback);
  41.         b7.addActionListener(callback);
  42.         b8.addActionListener(callback);
  43.         b9.addActionListener(callback);
  44.         checkout.addActionListener(callback);
  45.        
  46.        
  47.         JPanel panel = new JPanel();
  48.         //Add buttons into the panel
  49.         panel.add(b1);
  50.         panel.add(b2);
  51.         panel.add(b3);
  52.         panel.add(b4);
  53.         panel.add(b5);
  54.         panel.add(b6);
  55.         panel.add(b7);
  56.         panel.add(b8);
  57.         panel.add(b9);
  58.        
  59.        
  60.                
  61.         // Create grid layout , with column, row arguments
  62.         GridLayout g = new GridLayout(3,3);
  63.         // Set panel to the grid lay out
  64.         panel.setLayout(g);
  65.        
  66.         // Panel is added to default border layout, default center location
  67.         add(panel);
  68.         add(clearArea, BorderLayout.NORTH);
  69.         add(checkout, BorderLayout.SOUTH);
  70.         add(area, BorderLayout.EAST);
  71.        
  72.         setTitle("FlameBay");
  73.         setSize(650,410);
  74.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  75.         setVisible(true);
  76.        
  77.        
  78.     }
  79.    
  80.  
  81.     // Foodcallback is created, implementing necessary actionlistener interface
  82.     private class FoodCallback implements ActionListener{
  83.        
  84.         // Text area created to receive object passed to FoodCallback
  85.         private JTextArea area;
  86.         private float total = 0;
  87.        
  88.         public FoodCallback(JTextArea area)
  89.         {
  90.             // Field initialized
  91.             this.area = area;
  92.         }
  93.        
  94.         @Override
  95.         public void actionPerformed(ActionEvent e)
  96.         {
  97.             DecimalFormat twoDecimalPlace = new DecimalFormat("#0.00");
  98.  
  99.             //Button ID saved to variable
  100.             JButton button = (JButton)e.getSource();
  101.             //ID which button
  102.             if(button == b1)
  103.                 total += 1.00;
  104.             else if(button == b2)
  105.                 total += 0.95;
  106.             else if(button == b3)
  107.                 total += 0.50;
  108.             else if(button == b4)
  109.                 total += 2.00;
  110.             else if(button == b5)
  111.                 total += 1.50;
  112.             else if(button == b6)
  113.                 total += 1.25;
  114.             else if(button == b7)
  115.                 total += 0.50;
  116.             else if(button == b8)
  117.                 total += 0.25;
  118.             else if(button == b9)
  119.                 total += 0.45;
  120.            
  121.             if(button == checkout)
  122.                 area.append("--- Cost: $" + twoDecimalPlace.format(total) + "\n");
  123.             else if(button == clearArea)
  124.             {
  125.                 area.setText(null);
  126.                 total = 0;
  127.             }
  128.             else
  129.                 area.append(button.getText()+"\n");
  130.            
  131.         }
  132.  
  133.     }
  134.  
  135.     public static void main(String[] args)
  136.     {
  137.         new FlameBay();
  138.  
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement