// Example using Swing classes JRadioButton and JCheckBox import java.applet.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class lunch extends JFrame implements ActionListener, ItemListener { JCheckBox mayoBox; JCheckBox lettuceBox; JCheckBox tomatoBox; JCheckBox onionBox; boolean mayo, lettuce, tomato, onion; JRadioButton chickenRB; JRadioButton beefRB; JRadioButton veggieRB; boolean chicken, beef, veggie; ButtonGroup entree; JButton showButton; lunch() { setSize(300,300); setLocation(250,250); setVisible(true); Container c = getContentPane(); c.setLayout(new BorderLayout()); chickenRB = new JRadioButton("Chicken", true); chicken = true; beefRB = new JRadioButton("Beef", false); veggieRB = new JRadioButton("Vegetarian", false); // put the 3 mutually exclusive entree options in the same group // this means ONLY ONE of these can be true at any time entree = new ButtonGroup(); entree.add(chickenRB); entree.add(beefRB); entree.add(veggieRB); // now deal with physical layout issues as always Panel entreepanel = new Panel(); entreepanel.setLayout(new GridLayout(3, 1)); entreepanel.add(chickenRB); entreepanel.add(beefRB); entreepanel.add(veggieRB); mayoBox = new JCheckBox("Mayonnaise"); lettuceBox = new JCheckBox("Lettuce"); tomatoBox = new JCheckBox("Tomatoes"); onionBox = new JCheckBox("Onions"); Panel optionpanel = new Panel(); optionpanel.setLayout(new GridLayout(4, 1)); optionpanel.add(mayoBox); optionpanel.add(lettuceBox); optionpanel.add(tomatoBox); optionpanel.add(onionBox); showButton = new JButton("Display"); Panel commandpanel = new Panel(); commandpanel.setLayout(new FlowLayout(FlowLayout.CENTER)); commandpanel.add(showButton); c.add(entreepanel, BorderLayout.WEST); c.add(optionpanel, BorderLayout.CENTER); c.add(commandpanel, BorderLayout.SOUTH); mayoBox.addItemListener(this); lettuceBox.addItemListener(this); tomatoBox.addItemListener(this); onionBox.addItemListener(this); beefRB.addItemListener(this); veggieRB.addItemListener(this); chickenRB.addItemListener(this); showButton.addActionListener(this); } public void itemStateChanged (ItemEvent i) { if (i.getSource() == beefRB) { if (i.getStateChange() == ItemEvent.SELECTED) beef = true; else beef = false; } if (i.getSource() == chickenRB) { if (i.getStateChange() == ItemEvent.SELECTED) chicken = true; else chicken = false; } if (i.getSource() == veggieRB) { if (i.getStateChange() == ItemEvent.SELECTED) veggie = true; else veggie = false; } if (i.getSource() == mayoBox) { if (i.getStateChange() == ItemEvent.SELECTED) mayo = true; else mayo = false; } else if (i.getSource() == lettuceBox) { if (i.getStateChange() == ItemEvent.SELECTED) lettuce = true; else lettuce = false; } else if (i.getSource() == tomatoBox) { if (i.getStateChange() == ItemEvent.SELECTED) tomato = true; else tomato = false; } else if (i.getSource() == onionBox) { if (i.getStateChange() == ItemEvent.SELECTED) onion = true; else onion = false; } } public void actionPerformed(ActionEvent a) { System.err.println("Current Meal Selection: "); if (chicken) System.err.println( "CHICKEN"); else if (beef) System.err.println( "BEEF"); else System.err.println( "VEGETARIAN"); System.err.println("Options as follows:"); if (mayo) System.err.println("\tWith Mayo"); else System.err.println("\tNo Mayo"); if (lettuce) System.err.println("\tWith Lettuce"); else System.err.println( "\tNo Lettuce"); if (onion) System.err.println("\tWith Onion"); else System.err.println("\tNo Onion"); if (tomato) System.err.println("\tWith Tomato"); else System.err.println("\tNo Tomato"); } public static void main (String args[]) { lunch app = new lunch(); } }