import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class Order extends JFrame implements ItemListener, ActionListener { private final int NTITLES = 3, NOPTIONS = 3; private JPanel mainPanel = new JPanel(), centerPanel = new JPanel(), choicePanel = new JPanel(), optionPanel = new JPanel(), buttonPanel = new JPanel(); private ButtonGroup optGroup = new ButtonGroup(); private JCheckBox titles[] = new JCheckBox[NTITLES]; private JRadioButton options[] = new JRadioButton[NOPTIONS]; private String titleLabels[] = {"Chess Master - $59.95", "Checkers Pro - $39.95","Crossword Maker - $19.95"}; private String optionLabels[] = {"Credit Card", "Debit Card", "E-cash"}; private JTextArea display = new JTextArea(7, 25); private JButton submit = new JButton("Submit Order"), cancel = new JButton("Cancel"); /** * Constructor */ public Order() { super("Order"); // call JFrame's constructor and give it a title setSize(400,400); // set the width of the window. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); mainPanel.setBorder(BorderFactory.createTitledBorder("Acme Software Titles")); mainPanel.setLayout(new GridLayout(3, 1, 1, 1)); cancel.addActionListener(this); submit.addActionListener(this); initChoices(); initOptions(); buttonPanel.setBorder( BorderFactory.createTitledBorder("Order Today")); buttonPanel.add(cancel); buttonPanel.add(submit); centerPanel.add(choicePanel); centerPanel.add(optionPanel); mainPanel.add( display); mainPanel.add(centerPanel); mainPanel.add( buttonPanel); getContentPane().add(mainPanel); } /** * initChoices() initializes the JCheckBoxes which are organized * into a separate panel, layed out in BoxLayout format with a * border around it. */ private void initChoices() { choicePanel.setBorder(BorderFactory.createTitledBorder("Titles")); choicePanel.setLayout(new BoxLayout(choicePanel, BoxLayout.Y_AXIS)); for (int k = 0; k < titles.length; k++) { titles[k] = new JCheckBox(titleLabels[k]); titles[k].addItemListener(this); choicePanel.add(titles[k]); } } // initChoices() /** * initOptions() initializes the JRadioButtons which are organized * into a separate panel, layed out in BoxLayout format with a * border around it. */ private void initOptions() { optionPanel.setBorder(BorderFactory.createTitledBorder("Payment By")); optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.Y_AXIS)); for (int k = 0; k < options.length; k++) { options[k] = new JRadioButton(optionLabels[k]); options[k].addItemListener(this); optionPanel.add(options[k]); optGroup.add(options[k]); } options[0].setSelected(true); } // initOptions() /** * itemStateChanged() handles the user's check box and radio button * selections. In each case, it appends some text to the main * display area. * @param e -- the ItemEvent that led to this method call */ public void itemStateChanged(ItemEvent e) { display.setText("Your order so far (Payment by: "); for (int k = 0; k < options.length; k++ ) if (options[k].isSelected()) display.append(options[k].getText() + ")\n"); for (int k = 0; k < titles.length; k++ ) if (titles[k].isSelected()) display.append("\t" + titles[k].getText() + "\n"); } // itemStateChanged() /** * actionPerformed() handles the applet's action events * Note that the submit button changes labels depending on the state * of the user's order. In effect, the button's label keeps track of * the applet's state -- either submitting or confirming an order. * @param e -- the ActionEvent that led to this method call */ public void actionPerformed(ActionEvent e){ String label = submit.getText(); if (e.getSource() == submit) { if (label.equals("Submit Order")) { display.append("Thank you. Press 'Confirm' to submit for your order!\n"); submit.setText("Confirm Order"); } else { display.append("Thank you. You will receive your order tomorrow!\n"); submit.setText("Submit Order"); } } else display.setText("Thank you. Maybe we can serve you next time!\n"); } // actionPerformed() /** * Main routine, Note, this routine can be in any class. */ public static void main(String[] arguments){ Order fr = new Order(); // create the sample 1 frame fr.setVisible(true); // allow it to be seen } }