I am having trouble adding an Exit button to my program: (Please help) //Librari
ID: 643820 • Letter: I
Question
I am having trouble adding an Exit button to my program: (Please help)
//Libraries
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.text.NumberFormat;
//Create class SubsPanel
public class SubsPanel extends JPanel {
ArrayList subsOrder;
ButtonGroup sizeGroup;
ButtonGroup breadGroup;
ButtonGroup condimentGroup;
JCheckBox[] cheeseCheckBoxes;
JCheckBox[] meatsCheckBoxes;
JCheckBox[] veggieToppingCheckBoxes;
JLabel numSubsLabel;
JLabel totalPriceLabel;
float previousPrice;
SubsOrderTest portal;
//Create GUI
public SubsPanel(SubsOrderTest orderPortal) {
portal = orderPortal;
previousPrice = 0.0f;
subsOrder = new ArrayList();
setLayout(new BorderLayout());
add(createSizePanel(), BorderLayout.NORTH);
add(createSubsPanel(),BorderLayout.CENTER);
add(createOrderButtonPanel(), BorderLayout.SOUTH);
//add(createExitButtonPanel(), BorderLayout.WEST);
}
//Create panel
JPanel createSizePanel()
{
sizeGroup = new ButtonGroup();
JPanel sizePanel = new JPanel();
sizePanel.setBorder(new EtchedBorder());
sizePanel.add(new JLabel("Size: "));
//Add sizes from class Subs
JRadioButton rButton;
for (int i = 0; i < Subs.SIZES.length; i++)
{
rButton = new JRadioButton(Subs.SIZES[i]);
rButton.setActionCommand(String.valueOf(i));
sizeGroup.add(rButton);
sizePanel.add(rButton);
if (i==0)
rButton.setSelected(true);
}
return sizePanel;
}
//Creates the upper panel in the main Subs panel
JPanel createUpperPanel()
{
JPanel upperPanel = new JPanel();
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
//Create panels for bread choices
breadGroup = new ButtonGroup();
JPanel breadPanel = new JPanel();
breadPanel.setLayout(new BoxLayout(breadPanel, BoxLayout.Y_AXIS));
breadPanel.setBorder(new TitledBorder(new EtchedBorder(), "Bread"));
//Add Bread types from class subs
JRadioButton rButton;
for (int i = 0; i < Subs.BREAD_TYPES.length; i++)
{
rButton = new JRadioButton(Subs.BREAD_TYPES[i]);
rButton.setActionCommand(String.valueOf(i));
breadGroup.add(rButton);
breadPanel.add(rButton);
if (i==0)
rButton.setSelected(true);
}
container.add(breadPanel);
//Create panels condiment choices
condimentGroup = new ButtonGroup();
JPanel condimentPanel = new JPanel();
condimentPanel.setLayout(new BoxLayout(condimentPanel, BoxLayout.Y_AXIS));
condimentPanel.setBorder(new TitledBorder(new EtchedBorder(), "Sauce"));
//Add condiment types from class Subs
for (int i = 0; i < Subs.CONDIMENT_TYPES.length; i++)
{
rButton = new JRadioButton(Subs.CONDIMENT_TYPES[i]);
rButton.setActionCommand(String.valueOf(i));
condimentGroup.add(rButton);
condimentPanel.add(rButton);
if (i==0)
rButton.setSelected(true);
}
container.add(condimentPanel);
//Create panel for cheese choices
cheeseCheckBoxes = new JCheckBox[Subs.CHEESE_TYPES.length];
JPanel cheesePanel = new JPanel();
cheesePanel.setLayout(new BoxLayout(cheesePanel, BoxLayout.Y_AXIS));
cheesePanel.setBorder(new TitledBorder(new EtchedBorder(), "Cheeses"));
//Add cheese types from class Subs
JCheckBox checkBox;
for (int i = 0; i < Subs.CHEESE_TYPES.length; i++)
{
checkBox = new JCheckBox(Subs.CHEESE_TYPES[i]);
checkBox.setActionCommand(String.valueOf(i));
cheeseCheckBoxes[i] = checkBox;
cheesePanel.add(checkBox);
}
container.add(cheesePanel);
upperPanel.add(container);
return upperPanel;
}
//Create the lower panel in the main subs panel
JPanel createLowerPanel()
{
JPanel lowerPanel = new JPanel();
//Create toppings panel
JPanel toppingsPanel = new JPanel();
toppingsPanel.setLayout(new BoxLayout(toppingsPanel, BoxLayout.X_AXIS));
toppingsPanel.setBorder(new TitledBorder(new EtchedBorder(), "Toppings"));
//Create meats panel
JPanel meatsPanel = new JPanel();
meatsPanel.setLayout(new BoxLayout(meatsPanel, BoxLayout.Y_AXIS));
meatsPanel.setBorder(new TitledBorder(new EtchedBorder(), "Meats"));
//Add meats from class Subs
JCheckBox checkBox;
meatsCheckBoxes = new JCheckBox[Subs.MEATS.length];
for (int i = 0; i < Subs.MEATS.length; i++)
{
checkBox = new JCheckBox(Subs.MEATS[i]);
checkBox.setActionCommand(String.valueOf(i));
meatsCheckBoxes[i] = checkBox;
meatsPanel.add(checkBox);
}
toppingsPanel.add(meatsPanel);
//Create veggie toppings panel
JPanel veggieToppingsPanel = new JPanel();
veggieToppingsPanel.setLayout(new BoxLayout(veggieToppingsPanel, BoxLayout.X_AXIS));
veggieToppingsPanel.setBorder(new TitledBorder(new EtchedBorder(), "Veggies"));
//create two panels to display veggies in columns
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
//Add veggie toppings from class Subs
int listHalf = Subs.VEGGIE_TOPPINGS.length / 2;
veggieToppingCheckBoxes = new JCheckBox[Subs.VEGGIE_TOPPINGS.length];
for (int i = 0; i < Subs.VEGGIE_TOPPINGS.length; i++)
{
checkBox = new JCheckBox(Subs.VEGGIE_TOPPINGS[i]);
checkBox.setActionCommand(String.valueOf(i));
veggieToppingCheckBoxes[i] = checkBox;
if (i < listHalf)
panel1.add(checkBox);
else
panel2.add(checkBox);
}
veggieToppingsPanel.add(panel1);
veggieToppingsPanel.add(panel2);
toppingsPanel.add(veggieToppingsPanel);
lowerPanel.add(toppingsPanel);
return lowerPanel;
}
//Creates the main subs panel
JPanel createSubsPanel()
{
JPanel subsPanel = new JPanel();
subsPanel.setLayout(new GridLayout(2,1));
subsPanel.add(createUpperPanel());
subsPanel.add(createLowerPanel());
return subsPanel;
}
//Creates the order button panel
JPanel createOrderButtonPanel()
{
JPanel orderPanel = new JPanel();
orderPanel.setLayout(new BorderLayout());
//Add order button
JButton orderButton = new JButton("Order This Gourmet Sub");
orderButton.addActionListener(new OrderButtonListener());
orderPanel.add(orderButton, BorderLayout.NORTH);
//Add stats
JPanel statPanel = new JPanel();
statPanel.setLayout(new BoxLayout(statPanel, BoxLayout.Y_AXIS));
statPanel.setBorder(new TitledBorder(new EtchedBorder(),"Current Order"));
JPanel numSubsPanel = new JPanel();
numSubsPanel.add(new JLabel("Number of Subs: "));
numSubsLabel = new JLabel("0");
numSubsPanel.add(numSubsLabel);
statPanel.add(numSubsPanel);
JPanel totalPricePanel = new JPanel();
totalPricePanel.add(new JLabel("Total Price of Subs: "));
totalPriceLabel = new JLabel("$0.00");
totalPricePanel.add(totalPriceLabel);
statPanel.add(totalPricePanel);
orderPanel.add(statPanel, BorderLayout.CENTER);
return orderPanel;
}
//Creates the Exit Button panel
/* JPanel createExitButtonPanel()
{
JPanel exitPanel = new JPanel();
exitPanel.setLayout(new BorderLayout());
//Add exit button
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ExitButtonListener());
exitPanel.add(exitButton, BorderLayout.WEST);
return exitPanel;
}*/
//Resets all the checkboxes
void resetOptions()
{
for (int i=0;i<cheeseCheckBoxes.length;i++)
cheeseCheckBoxes[i].setSelected(false);
for (int i=0;i<meatsCheckBoxes.length;i++)
meatsCheckBoxes[i].setSelected(false);
for (int i=0;i<veggieToppingCheckBoxes.length;i++)
veggieToppingCheckBoxes[i].setSelected(false);
}
//Compute the total price of the subs order
float getTotalPrice()
{
float price = 0;
for (int i=0; i<subsOrder.size(); i++)
price += ((Subs)subsOrder.get(i)).getPrice();
return price;
}
//Listener class for the order button
class OrderButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Subs currentSubs = new Subs();
try
{
//Get size
String cmdString = sizeGroup.getSelection().getActionCommand();
currentSubs.size = Integer.parseInt(cmdString);
//Get bread type
cmdString = breadGroup.getSelection().getActionCommand();
currentSubs.bread = Integer.parseInt(cmdString);
//Get condiment type
cmdString = condimentGroup.getSelection().getActionCommand();
currentSubs.condiment = Integer.parseInt(cmdString);
//Get selected cheeses
for (int i=0; i < cheeseCheckBoxes.length; i++)
if (cheeseCheckBoxes[i].isSelected())
{
cmdString = cheeseCheckBoxes[i].getActionCommand();
currentSubs.cheeses.add(new Integer(cmdString));
}
//Get selected meat toppings
for (int i=0; i < meatsCheckBoxes.length; i++)
if (meatsCheckBoxes[i].isSelected())
{
cmdString = meatsCheckBoxes[i].getActionCommand();
currentSubs.meats.add(new Integer(cmdString));
}
//Get selected veggie toppings
for (int i=0; i < veggieToppingCheckBoxes.length; i++)
if (veggieToppingCheckBoxes[i].isSelected())
{
cmdString = veggieToppingCheckBoxes[i].getActionCommand();
currentSubs.veggieToppings.add(new Integer(cmdString));
}
}
catch (NumberFormatException e)
{
}
//Add subs to order
subsOrder.add(currentSubs);
//Update stats
numSubsLabel.setText(String.valueOf(subsOrder.size()));
NumberFormat money = NumberFormat.getCurrencyInstance();
float currentPrice = getTotalPrice();
totalPriceLabel.setText(money.format(currentPrice));
//Update grand total
portal.updateGrandTotal(currentPrice - previousPrice);
previousPrice = currentPrice;
resetOptions();
}
//The action performed method executes when the user
//clicks on the Exit button
class ExitButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//Exit the application
System.exit(0);
}
}
}
}
Explanation / Answer
//Exit button is added to the program.
//Libraries
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.text.NumberFormat;
//Create class SubsPanel
public class SubsPanel extends JPanel
{
ArrayList subsOrder;
ButtonGroup sizeGroup;
ButtonGroup breadGroup;
ButtonGroup condimentGroup;
JCheckBox[] cheeseCheckBoxes;
JCheckBox[] meatsCheckBoxes;
JCheckBox[] veggieToppingCheckBoxes;
JLabel numSubsLabel;
JLabel totalPriceLabel;
//Create a button variable of static type
private static JButton exitButton;
float previousPrice;
SubsOrderTest portal;
//Create GUI
public SubsPanel() {
portal = orderPortal;
previousPrice = 0.0f;
subsOrder = new ArrayList();
setLayout(new BorderLayout());
add(createSizePanel(), BorderLayout.NORTH);
add(createSubsPanel(),BorderLayout.CENTER);
add(createOrderButtonPanel(), BorderLayout.SOUTH);
//create a new panel with exit button and add to the west direction
//of borderlayout
add(createExitButtonPanel(), BorderLayout.WEST);
}
//The method createExitButtonPanel creates a panel
//with a new button exit and add the actionlistner
//to the exit button that close the application
//when it is clicked.
private Component createExitButtonPanel()
{
JPanel exitPanel=new JPanel();
exitButton=new JButton("Exit");
exitPanel.add(exitButton);
//The action performed method executes when the user
//clicks on the Exit button
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
//exit the application
System.exit(0);
}
});
return exitPanel;
}
//Create panel
JPanel createSizePanel()
{
sizeGroup = new ButtonGroup();
JPanel sizePanel = new JPanel();
sizePanel.setBorder(new EtchedBorder());
sizePanel.add(new JLabel("Size: "));
//Add sizes from class Subs
JRadioButton rButton;
for (int i = 0; i < Subs.SIZES.length; i++)
{
rButton = new JRadioButton(Subs.SIZES[i]);
rButton.setActionCommand(String.valueOf(i));
sizeGroup.add(rButton);
sizePanel.add(rButton);
if (i==0)
rButton.setSelected(true);
}
return sizePanel;
}
//Creates the upper panel in the main Subs panel
JPanel createUpperPanel()
{
JPanel upperPanel = new JPanel();
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
//Create panels for bread choices
breadGroup = new ButtonGroup();
JPanel breadPanel = new JPanel();
breadPanel.setLayout(new BoxLayout(breadPanel, BoxLayout.Y_AXIS));
breadPanel.setBorder(new TitledBorder(new EtchedBorder(), "Bread"));
//Add Bread types from class subs
JRadioButton rButton;
for (int i = 0; i < Subs.BREAD_TYPES.length; i++)
{
rButton = new JRadioButton(Subs.BREAD_TYPES[i]);
rButton.setActionCommand(String.valueOf(i));
breadGroup.add(rButton);
breadPanel.add(rButton);
if (i==0)
rButton.setSelected(true);
}
container.add(breadPanel);
//Create panels condiment choices
condimentGroup = new ButtonGroup();
JPanel condimentPanel = new JPanel();
condimentPanel.setLayout(new BoxLayout(condimentPanel, BoxLayout.Y_AXIS));
condimentPanel.setBorder(new TitledBorder(new EtchedBorder(), "Sauce"));
//Add condiment types from class Subs
for (int i = 0; i < Subs.CONDIMENT_TYPES.length; i++)
{
rButton = new JRadioButton(Subs.CONDIMENT_TYPES[i]);
rButton.setActionCommand(String.valueOf(i));
condimentGroup.add(rButton);
condimentPanel.add(rButton);
if (i==0)
rButton.setSelected(true);
}
container.add(condimentPanel);
//Create panel for cheese choices
cheeseCheckBoxes = new JCheckBox[Subs.CHEESE_TYPES.length];
JPanel cheesePanel = new JPanel();
cheesePanel.setLayout(new BoxLayout(cheesePanel, BoxLayout.Y_AXIS));
cheesePanel.setBorder(new TitledBorder(new EtchedBorder(), "Cheeses"));
//Add cheese types from class Subs
JCheckBox checkBox;
for (int i = 0; i < Subs.CHEESE_TYPES.length; i++)
{
checkBox = new JCheckBox(Subs.CHEESE_TYPES[i]);
checkBox.setActionCommand(String.valueOf(i));
cheeseCheckBoxes[i] = checkBox;
cheesePanel.add(checkBox);
}
container.add(cheesePanel);
upperPanel.add(container);
return upperPanel;
}
//Create the lower panel in the main subs panel
JPanel createLowerPanel()
{
JPanel lowerPanel = new JPanel();
//Create toppings panel
JPanel toppingsPanel = new JPanel();
toppingsPanel.setLayout(new BoxLayout(toppingsPanel, BoxLayout.X_AXIS));
toppingsPanel.setBorder(new TitledBorder(new EtchedBorder(), "Toppings"));
//Create meats panel
JPanel meatsPanel = new JPanel();
meatsPanel.setLayout(new BoxLayout(meatsPanel, BoxLayout.Y_AXIS));
meatsPanel.setBorder(new TitledBorder(new EtchedBorder(), "Meats"));
//Add meats from class Subs
JCheckBox checkBox;
meatsCheckBoxes = new JCheckBox[Subs.MEATS.length];
for (int i = 0; i < Subs.MEATS.length; i++)
{
checkBox = new JCheckBox(Subs.MEATS[i]);
checkBox.setActionCommand(String.valueOf(i));
meatsCheckBoxes[i] = checkBox;
meatsPanel.add(checkBox);
}
toppingsPanel.add(meatsPanel);
//Create veggie toppings panel
JPanel veggieToppingsPanel = new JPanel();
veggieToppingsPanel.setLayout(new BoxLayout(veggieToppingsPanel, BoxLayout.X_AXIS));
veggieToppingsPanel.setBorder(new TitledBorder(new EtchedBorder(), "Veggies"));
//create two panels to display veggies in columns
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
//Add veggie toppings from class Subs
int listHalf = Subs.VEGGIE_TOPPINGS.length / 2;
veggieToppingCheckBoxes = new JCheckBox[Subs.VEGGIE_TOPPINGS.length];
for (int i = 0; i < Subs.VEGGIE_TOPPINGS.length; i++)
{
checkBox = new JCheckBox(Subs.VEGGIE_TOPPINGS[i]);
checkBox.setActionCommand(String.valueOf(i));
veggieToppingCheckBoxes[i] = checkBox;
if (i < listHalf)
panel1.add(checkBox);
else
panel2.add(checkBox);
}
veggieToppingsPanel.add(panel1);
veggieToppingsPanel.add(panel2);
toppingsPanel.add(veggieToppingsPanel);
lowerPanel.add(toppingsPanel);
return lowerPanel;
}
//Creates the main subs panel
JPanel createSubsPanel()
{
JPanel subsPanel = new JPanel();
subsPanel.setLayout(new GridLayout(2,1));
subsPanel.add(createUpperPanel());
subsPanel.add(createLowerPanel());
return subsPanel;
}
//Creates the order button panel
JPanel createOrderButtonPanel()
{
JPanel orderPanel = new JPanel();
orderPanel.setLayout(new BorderLayout());
//Add order button
JButton orderButton = new JButton("Order This Gourmet Sub");
orderButton.addActionListener(new OrderButtonListener());
orderPanel.add(orderButton, BorderLayout.NORTH);
//Add stats
JPanel statPanel = new JPanel();
statPanel.setLayout(new BoxLayout(statPanel, BoxLayout.Y_AXIS));
statPanel.setBorder(new TitledBorder(new EtchedBorder(),"Current Order"));
JPanel numSubsPanel = new JPanel();
numSubsPanel.add(new JLabel("Number of Subs: "));
numSubsLabel = new JLabel("0");
numSubsPanel.add(numSubsLabel);
statPanel.add(numSubsPanel);
JPanel totalPricePanel = new JPanel();
totalPricePanel.add(new JLabel("Total Price of Subs: "));
totalPriceLabel = new JLabel("$0.00");
totalPricePanel.add(totalPriceLabel);
statPanel.add(totalPricePanel);
orderPanel.add(statPanel, BorderLayout.CENTER);
return orderPanel;
}
//Creates the Exit Button panel
/* JPanel createExitButtonPanel()
{
JPanel exitPanel = new JPanel();
exitPanel.setLayout(new BorderLayout());
//Add exit button
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ExitButtonListener());
exitPanel.add(exitButton, BorderLayout.WEST);
return exitPanel;
}*/
//Resets all the checkboxes
void resetOptions()
{
for (int i=0;i<cheeseCheckBoxes.length;i++)
cheeseCheckBoxes[i].setSelected(false);
for (int i=0;i<meatsCheckBoxes.length;i++)
meatsCheckBoxes[i].setSelected(false);
for (int i=0;i<veggieToppingCheckBoxes.length;i++)
veggieToppingCheckBoxes[i].setSelected(false);
}
//Compute the total price of the subs order
float getTotalPrice()
{
float price = 0;
for (int i=0; i<subsOrder.size(); i++)
price += ((Subs)subsOrder.get(i)).getPrice();
return price;
}
//Listener class for the order button
class OrderButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Subs currentSubs = new Subs();
try
{
//Get size
String cmdString = sizeGroup.getSelection().getActionCommand();
currentSubs.size = Integer.parseInt(cmdString);
//Get bread type
cmdString = breadGroup.getSelection().getActionCommand();
currentSubs.bread = Integer.parseInt(cmdString);
//Get condiment type
cmdString = condimentGroup.getSelection().getActionCommand();
currentSubs.condiment = Integer.parseInt(cmdString);
//Get selected cheeses
for (int i=0; i < cheeseCheckBoxes.length; i++)
if (cheeseCheckBoxes[i].isSelected())
{
cmdString = cheeseCheckBoxes[i].getActionCommand();
currentSubs.cheeses.add(new Integer(cmdString));
}
//Get selected meat toppings
for (int i=0; i < meatsCheckBoxes.length; i++)
if (meatsCheckBoxes[i].isSelected())
{
cmdString = meatsCheckBoxes[i].getActionCommand();
currentSubs.meats.add(new Integer(cmdString));
}
//Get selected veggie toppings
for (int i=0; i < veggieToppingCheckBoxes.length; i++)
if (veggieToppingCheckBoxes[i].isSelected())
{
cmdString = veggieToppingCheckBoxes[i].getActionCommand();
currentSubs.veggieToppings.add(new Integer(cmdString));
}
}
catch (NumberFormatException e)
{
}
//Add subs to order
subsOrder.add(currentSubs);
//Update stats
numSubsLabel.setText(String.valueOf(subsOrder.size()));
NumberFormat money = NumberFormat.getCurrencyInstance();
float currentPrice = getTotalPrice();
totalPriceLabel.setText(money.format(currentPrice));
//Update grand total
portal.updateGrandTotal(currentPrice - previousPrice);
previousPrice = currentPrice;
resetOptions();
}
}
}
------------------------------------------------------------------------------------
//Subs.java
public class Subs
{
//Assumed to create the arrays for Subs class
//as used in the class SubsPanle.
public static String BREAD_TYPES[]={};
public static String SIZES[]={"Small","Medium","Large"};
public static String CONDIMENT_TYPES[]={"Salt","Mastard","Pickle"};
public static String CHEESE_TYPES[]={"Fresh","Whey","Stretched"};
public static String MEATS[]={"Chicken","Beef","Pork"};
public static String VEGGIE_TOPPINGS[]={"Salads","Mix","Cocktails"};
public int size;
public int bread;
public int condiment;
public Object cheeses;
public float getPrice()
{
return 0;
}
}
Note : Only Exit button part of the program is added , rest of the program
has not implemented some actions and have some errors. Only exit button
and its implementation is added to the current program.
Hope this helps you...