import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class FlameBay extends JFrame{ private JButton clearArea = new JButton("Clear"); private JButton b1 = new JButton("Egg Noodles $1.00"); private JButton b2 = new JButton("Spaghetti $0.95"); private JButton b3 = new JButton("Ramen $0.50"); private JButton b4 = new JButton("Beef $2.00"); private JButton b5 = new JButton("Chicken $1.50"); private JButton b6 = new JButton("Pork $1.25"); private JButton b7 = new JButton("Kung Pao $0.50"); private JButton b8 = new JButton("Teriyaki $0.25"); private JButton b9 = new JButton("Curry $0.45"); private JButton checkout = new JButton("Check Out"); String defaultWidth = "\t\t \r"; JTextArea area = new JTextArea(25,20); public FlameBay() { FoodCallback callback = new FoodCallback(area); clearArea.addActionListener(callback); b1.addActionListener(callback); b2.addActionListener(callback); b3.addActionListener(callback); b4.addActionListener(callback); b5.addActionListener(callback); b6.addActionListener(callback); b7.addActionListener(callback); b8.addActionListener(callback); b9.addActionListener(callback); checkout.addActionListener(callback); JPanel panel = new JPanel(); //Add buttons into the panel panel.add(b1); panel.add(b2); panel.add(b3); panel.add(b4); panel.add(b5); panel.add(b6); panel.add(b7); panel.add(b8); panel.add(b9); // Create grid layout , with column, row arguments GridLayout g = new GridLayout(3,3); // Set panel to the grid lay out panel.setLayout(g); // Panel is added to default border layout, default center location add(panel); add(clearArea, BorderLayout.NORTH); add(checkout, BorderLayout.SOUTH); add(area, BorderLayout.EAST); setTitle("FlameBay"); setSize(650,410); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } // Foodcallback is created, implementing necessary actionlistener interface private class FoodCallback implements ActionListener{ // Text area created to receive object passed to FoodCallback private JTextArea area; private float total = 0; public FoodCallback(JTextArea area) { // Field initialized this.area = area; } @Override public void actionPerformed(ActionEvent e) { DecimalFormat twoDecimalPlace = new DecimalFormat("#0.00"); //Button ID saved to variable JButton button = (JButton)e.getSource(); //ID which button if(button == b1) total += 1.00; else if(button == b2) total += 0.95; else if(button == b3) total += 0.50; else if(button == b4) total += 2.00; else if(button == b5) total += 1.50; else if(button == b6) total += 1.25; else if(button == b7) total += 0.50; else if(button == b8) total += 0.25; else if(button == b9) total += 0.45; if(button == checkout) area.append("--- Cost: $" + twoDecimalPlace.format(total) + "\n"); else if(button == clearArea) { area.setText(null); total = 0; } else area.append(button.getText()+"\n"); } } public static void main(String[] args) { new FlameBay(); } }