I'm a bit confused about my program. I have it all working great, but my professor is expecting a different output. He wants a box where you select your dormitory and meal plan, with "Calculate charges" and "Exit" buttons. When you click "Calculate charges," a second box comes up with a message with your total charges. I've tried other stuff like:
JOptionPane.showMessageDialog(total, "Total Charges For the Semester: ");
But with no luck. Any tips or suggestions on how to rewrite my code to satisfy my professor will be greatly appreciated. Thanks
Here's my code:
import java.awt.*; // Import common GUI elements. import java.awt.event.*; // Import common GUI event listeners. import javax.swing.*; // Import more common GUI elements. /** DormAndMealPlanCalculator class. */ public class DormAndMealPlanCalculator extends JFrame { private static final long serialVersionUID = 1L; private JLabel label; // Display a message. private JPanel dormPanel; // To hold dorm panel components. private JPanel selectedDormPanel; // To hold selected dorm panel components. private JComboBox dormBox; // List of dorms. private JTextField selectedDorm; // Selected dorm. private JPanel mealPanel; // To hold meal panel components. private JPanel selectedMealPanel; // To hold selected meal panel components. private JComboBox mealBox; // List of meal plans. private JTextField selectedMeal; // Selected meal plan. private JPanel totalPanel; // To hold total panel component. private JTextField total; // Total plan. /** Array to hold the values of the dormitory combo box. */ private String[] dorm = { "Allen Hall $" + 1500, "Pike Hall $" + 1600, "Farthing Hall $" + 1200, "University Suites $" + 1800 }; /** Array to hold the dormitory rate. */ double[] dRate = {1500, 1600, 1200, 1800}; /** Array to hold the values of the meal combo box. */ private String[] meal = { "7 Meals Per Week $" + 560, "14 Meals Per Week $" + 1095, "Unlimited Meals $" + 1500 }; /** Array to hold the meal plan rate. */ double[] mRate = {560, 1095, 1500}; /** Constructor. */ public DormAndMealPlanCalculator() { setTitle("Dorm and Meal Plan Calculator."); // Set the title. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Specify action for close button. setLayout(new GridLayout(3,2)); // Create a border layout manager. // Build panels. buildDormPanel(); buildMealPanel(); buildTotalPanel(); // Add panels to content pane. add(dormPanel); add(mealPanel); add(totalPanel); // Pack and display. pack(); setVisible(true); } /** buildDormPanel function that adds a combo box with the dormitories to the panel. */ private void buildDormPanel() { dormPanel = new JPanel(); // Create panel to hold combo box. dormBox = new JComboBox(dorm); // Create the combo box. // Register an action listener. dormBox.addActionListener(new ComboBoxListener()); dormPanel.add(dormBox); } /** buildMealPanel function that adds a combo box with the types of meals to the panel. */ private void buildMealPanel() { mealPanel = new JPanel(); // Create panel to hold combo box. mealBox = new JComboBox(meal); // Create the combo box. // Register an action listener. mealBox.addActionListener(new ComboBoxListener()); mealPanel.add(mealBox); } /** buildSelectedDormPanel function adds a read-only text field to the panel. */ public void buildSelectedDormPanel() { selectedDormPanel = new JPanel(); // Create panel to hold components. label = new JLabel("Your Dormitory Is: "); // Create the label. // Create the uneditable text field. selectedDorm = new JTextField(20); selectedDorm.setEditable(false); // Add the label and text field // to the panel. selectedDormPanel.add(label); selectedDormPanel.add(selectedDorm); } /** buildSelectedMealPanel function adds a read-only text field to the panel. */ public void buildSelectedMealPanel() { selectedMealPanel = new JPanel(); // Create panel to hold components. label = new JLabel("Your Meal Plan Is: "); // Create the label. // Create the uneditable text field. selectedMeal = new JTextField(20); selectedMeal.setEditable(false); // Add the label and text field // to the panel. selectedMealPanel.add(label); selectedMealPanel.add(selectedMeal); } /** buildTotalPanel function adds a read-only text field to the panel. */ private void buildTotalPanel() { totalPanel = new JPanel(); // Create the panel. label = new JLabel("Your Total Charges For the Semester Is: $"); // Create the label. // Create the uneditable text field. total = new JTextField(15); total.setEditable(false); // Add the label and text field // to the panel. totalPanel.add(label); totalPanel.add(total); } /** Private inner class that handles the event when the user selects an item from the combo box. */ private class ComboBoxListener implements ActionListener { public void actionPerformed(ActionEvent e) { // Declare variables. int dorm; int meal; double total1; // String total. // Get the selected dormitory. dormBox.getSelectedItem(); dorm = dormBox.getSelectedIndex(); // Get the selected meal plan. mealBox.getSelectedItem(); meal = mealBox.getSelectedIndex(); // Add the selections. total1 = dRate[dorm] + mRate[meal]; total.setText("$" + total1); } } /** Main function. */ public static void main(String[] args) { new DormAndMealPlanCalculator(); } }