/* This is a small JApplet starbucks order program It demonstrate the use of BevelBorder, JCheckBox, & JRadioButton * */ import javax.swing.*; import java.awt.event.*; import javax.swing.border.*; public class StarbucksApplet extends JApplet { JButton ok; JPanel radiopanel; JPanel checkboxpanel; JPanel mainpanel; JRadioButton tall, grande, venti; JCheckBox ice_green, coffee, orange_juice; public void init() { // Sets the size of the window setSize(550, 220); // Creates a button listener ButtonListener bl = new ButtonListener(); // Create two panels one for JRadioButton and one for the main panel radiopanel = new JPanel(); mainpanel = new JPanel(); // Creates a border for JRadioButton Border b = BorderFactory.createTitledBorder("Size"); // Adds the border to the radio panel radiopanel.setBorder(b); // Group the radio buttons together ButtonGroup radiogroup = new ButtonGroup(); // Add the radio buttons to the panel and group tall = new JRadioButton("Tall"); radiopanel.add(tall); radiogroup.add(tall); grande = new JRadioButton("Grande"); radiopanel.add(grande); radiogroup.add(grande); venti = new JRadioButton("Venti"); radiopanel.add(venti); radiogroup.add(venti); // Adds the JRadioButton panel to the main panel mainpanel.add(radiopanel); // Creates the JCheckBox panel checkboxpanel = new JPanel(); // Creates a border for the check box panel Border b2 = BorderFactory.createTitledBorder("Drinks"); // Adds the border to the check box checkboxpanel.setBorder(b2); // Create JCheckbox and add the check boxes into the panel ice_green = new JCheckBox("Ice Green Tea"); checkboxpanel.add(ice_green); coffee = new JCheckBox("Coffee"); checkboxpanel.add(coffee); orange_juice = new JCheckBox("Orange Juice"); checkboxpanel.add(orange_juice); // adds the check box panel to the main panel mainpanel.add(checkboxpanel); // Creates a ok button ok = new JButton("OK"); /* Creates a listener for ok button to listen for any events We pass the bl into the parameter, bl is created from the very beginning * */ ok.addActionListener(bl); // Adds the ok button into the main panel mainpanel.add(ok); // Adds the main panel into the content pane add(mainpanel); } /* Inside here, all the events of JCheckBox and JRadioButton will be handled * */ public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==ok) { String drinks = ""; // Determine what is been selected in JCheck Box if(ice_green.isSelected()) { drinks += "Ice Green Tea\n"; } if(coffee.isSelected()) { drinks += "Coffee\n"; } if(orange_juice.isSelected()) { drinks += "Orange Juice\n"; } // Determine what is been selected in JRadio Button String msg = "You ordered a "; if(tall.isSelected()) { msg += " tall " + drinks; } if(grande.isSelected()) { msg += " grande " + drinks; } if(venti.isSelected()) { msg += " venti " + drinks; } // Outputs the order in JOptionPane JOptionPane.showMessageDialog(null, msg); /* Deselected all the check box Selects tall in radio button * */ ice_green.setSelected(false); coffee.setSelected(false); orange_juice.setSelected(false); tall.setSelected(true); } } } }