I have my program working but i need to replace the JOptionPane.showMessageDialog(null, "The cost is " + totalCost); and just have the totalCost output to the totalCharges textfield.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DormAndMealPlanCalculator extends JFrame
{
private JPanel dormPanel, mealPanel, buttonPanel, totalChargesPanel;
private JComboBox dormPanelCom, mealPanelCom;
private JButton calcButton;
private JTextField totalCharges;
private JLabel label1;
//Constants for the price of each dorm.
public final int ALLEN_HALL = 1500;
public final int PIKE_HALL = 1600;
public final int FARTHING_HALL = 1200;
public final int UNIVERSITY_SUITES = 1800;
//Constants for meal costs.
public final int SEVEN_MEAL = 560;
public final int FOURTEEN_MEAL = 1095;
public final int UNLIMITED_MEAL = 1500;
private int dormCost = 0;
private int mealCost = 0;
private int totalCost = 0;
//String for dormChoice combobox.
private String[]dormChoice =
{ "Allen Hall $ 1,750 per semester", "Pike Hall: $1,600 per semester",
"Farthing Hall: $1,200 per semester", "University Suites: $1,800 per semester" };
private String[]mealChoice =
{ "7 meals per week $ 650 per semester", "14 meals per week: $1,095 per semester",
"Unlimited meals: $1,500 per semester" };
public DormAndMealPlanCalculator()
{
//Displays the Name
super("Dorm Room/Meal Calculator");
//Set action when closing
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create border layout
setLayout(new BorderLayout());
buildDormPanel();
buildMealPanel();
buildSelectedTotalChargesPanel();
buildButtonPanel();
add(dormPanel, BorderLayout.WEST);
add(mealPanel, BorderLayout.EAST);
add(totalChargesPanel, BorderLayout.SOUTH);
add(buttonPanel, BorderLayout.NORTH);
pack();
setVisible(true);
}
private void buildDormPanel()
{
dormPanel = new JPanel();
dormPanelCom = new JComboBox(dormChoice);
dormPanelCom.addActionListener(new ComboBoxListener());
dormPanel.add(dormPanelCom);
}
private void buildMealPanel()
{
mealPanel = new JPanel();
mealPanelCom = new JComboBox(mealChoice);
mealPanelCom.addActionListener(new MealBoxListener());
mealPanel.add(mealPanelCom);
}
// The buttonPanel method builds the bottun panel.
private void buildButtonPanel()
{
buttonPanel = new JPanel();
calcButton = new JButton("Calculate");
buttonPanel.add(calcButton);
}
// The buildSelectedDormPanel builds the selected totalCharges panel.
private void buildSelectedTotalChargesPanel()
{
// Create the totalChargesPanel for the label.
totalChargesPanel = new JPanel();
label1 = new JLabel("Total charges per semester: ");
// Create the totalCharges textfield.
totalCharges = new JTextField (15);
totalCharges.setEditable(false);
// Add the totalChargesPanel to the panel.
totalChargesPanel.add(label1);
totalChargesPanel.add(totalCharges);
}
private class ComboBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = (String) dormPanelCom.getSelectedItem();
if (selection.equals("Allen Hall $ 1,750 per semester"))
dormCost = ALLEN_HALL;
else if (selection.equals("Pike Hall: $1,600 per semester"))
dormCost = PIKE_HALL;
else if (selection.equals("Farthing Hall: $1,200 per semester"))
dormCost = FARTHING_HALL;
else if (selection.equals("University Suites: $1,800 per semester"))
dormCost = UNIVERSITY_SUITES;
}
}
private class MealBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = (String) mealPanelCom.getSelectedItem();
if (selection.equals("7 meals per week $ 650 per semester"))
mealCost = SEVEN_MEAL;
else if (selection.equals("14 meals per week: $1,095 per semester"))
mealCost = FOURTEEN_MEAL;
else if (selection.equals("Unlimited meals: $1,500 per semester"))
mealCost = UNLIMITED_MEAL;
totalCost = dormCost + mealCost;
JOptionPane.showMessageDialog(null, "The cost is " +
totalCost);
}
}
public static void main(String[] arguments){
DormAndMealPlanCalculator dam = new DormAndMealPlanCalculator();
}
}