Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy don't events work in my applet? (they work in the original application)
    primarykey
    data
    text
    <p>My applet is basically two combo boxes that store values in variables with their events (when the user selects an option). The confirm button generates an event that adds those two values together. The appliction works fine, but when I try to convert it into an applet, the text field doesn't show up, and there seems to be some warning sign that comes whenever I click a combo option Any advice please?</p> <p>Here is the code for the applet:</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DormPlanApplet extends JApplet { private JPanel selectionPanel; private JPanel costPanel; private JComboBox dormListBox; private JComboBox mealPlanListBox; private JLabel costLabel; private JTextField costField; private JButton confirmButton; double dormCost; double mealCost; double totalCost; int checker1; int checker2; String costString; private String[] dormListArray = {"Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" }; private String[] mealPlanListArray = {"7 Meals", "14 Meals", "Unlimited Meals" }; public void init() { //super("College Cost Calculator"); //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); buildSelectionPanel(); buildCostPanel(); add(selectionPanel, BorderLayout.CENTER); add(costPanel, BorderLayout.SOUTH); //pack(); //setVisible(true); } private void buildSelectionPanel() { selectionPanel = new JPanel(); selectionPanel.setLayout(new FlowLayout()); dormListBox = new JComboBox(dormListArray); mealPlanListBox = new JComboBox(mealPlanListArray); dormListBox.addActionListener(new dormCostListener()); mealPlanListBox.addActionListener(new mealCostListener()); selectionPanel.add(dormListBox); selectionPanel.add(mealPlanListBox); } private void buildCostPanel() { costPanel = new JPanel(); costPanel.setLayout(new FlowLayout()); costLabel = new JLabel("The total cost is:"); confirmButton = new JButton("Confirm"); confirmButton.addActionListener(new calcButtonListener()); costField = new JTextField(12); costField.setEditable(false); costPanel.add(confirmButton); costPanel.add(costLabel); costPanel.add(costField); } private class dormCostListener implements ActionListener { public void actionPerformed(ActionEvent e) { checker1 = 1; switch (dormListBox.getSelectedIndex()) { case 0: dormCost = 1500; break; case 1: dormCost = 1600; break; case 2: dormCost = 1200; break; case 3: dormCost = 1800; break; default: dormCost = 0; break; } } } private class mealCostListener implements ActionListener { public void actionPerformed(ActionEvent e) { checker2 = 1; switch (mealPlanListBox.getSelectedIndex()) { case 0: mealCost = 560; break; case 1: mealCost = 1095; break; case 2: mealCost = 1500; break; default: mealCost = 0; break; } } } private class calcButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if ((checker1 == 1) &amp;&amp; (checker2 == 1)) { totalCost = dormCost + mealCost; costString = Double.toString(totalCost); costField.setText(costString); } else { costField.setText("It doesn't work!"); } } } } </code></pre> <p>Here is the code for the oringal appliction:</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DormPlanApp extends JFrame { private JPanel selectionPanel; private JPanel costPanel; private JComboBox dormListBox; private JComboBox mealPlanListBox; private JLabel costLabel; private JTextField costField; private JButton confirmButton; double dormCost; double mealCost; double totalCost; int checker1; int checker2; String costString; private String[] dormListArray = {"Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" }; private String[] mealPlanListArray = {"7 Meals", "14 Meals", "Unlimited Meals" }; public DormPlanApp() { super("College Cost Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); buildSelectionPanel(); buildCostPanel(); add(selectionPanel, BorderLayout.CENTER); add(costPanel, BorderLayout.SOUTH); pack(); setVisible(true); } private void buildSelectionPanel() { selectionPanel = new JPanel(); selectionPanel.setLayout(new FlowLayout()); dormListBox = new JComboBox(dormListArray); mealPlanListBox = new JComboBox(mealPlanListArray); dormListBox.addActionListener(new dormCostListener()); mealPlanListBox.addActionListener(new mealCostListener()); selectionPanel.add(dormListBox); selectionPanel.add(mealPlanListBox); } private void buildCostPanel() { costPanel = new JPanel(); costPanel.setLayout(new FlowLayout()); costLabel = new JLabel("The total cost is:"); confirmButton = new JButton("Confirm"); confirmButton.addActionListener(new calcButtonListener()); costField = new JTextField(12); costField.setEditable(false); costPanel.add(confirmButton); costPanel.add(costLabel); costPanel.add(costField); } private class dormCostListener implements ActionListener { public void actionPerformed(ActionEvent e) { checker1 = 1; switch (dormListBox.getSelectedIndex()) { case 0: dormCost = 1500; break; case 1: dormCost = 1600; break; case 2: dormCost = 1200; break; case 3: dormCost = 1800; break; default: dormCost = 0; break; } } } private class mealCostListener implements ActionListener { public void actionPerformed(ActionEvent e) { checker2 = 1; switch (mealPlanListBox.getSelectedIndex()) { case 0: mealCost = 560; break; case 1: mealCost = 1095; break; case 2: mealCost = 1500; break; default: mealCost = 0; break; } } } private class calcButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if ((checker1 == 1) &amp;&amp; (checker2 == 1)) { totalCost = dormCost + mealCost; costString = Double.toString(totalCost); costField.setText(costString); } else { costField.setText("It doesn't work!"); } } } public static void main(String[] args) { new DormPlanApp(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload