import javax.swing.*; import java.awt.*; import java.awt.event.*; public class VoteDialog extends JPanel { JLabel label; JFrame frame; String simpleDialogDesc = "THE OPTIONS"; public VoteDialog(JFrame frame) { this.frame = frame; JLabel title; // Create the components. JPanel choicePanel = createSimpleDialogBox(); title = new JLabel("***TELEPHONE DIRECTORY***" + "\n***CLICK AND CHOOSE AN OPTION***", JLabel.CENTER); label = new JLabel("...!ENJOY THE PROGRAM!..", JLabel.CENTER); label.setBorder(BorderFactory.createEmptyBorder(10,10,10,50)); choicePanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,20)); // Set the layout. setLayout(new BorderLayout()); add(title, BorderLayout.NORTH); add(label, BorderLayout.SOUTH); add(choicePanel, BorderLayout.CENTER); } void setLabel(String newText) { label.setText(newText); } private JPanel createSimpleDialogBox() { final int numButtons = 5; JRadioButton[] radioButtons = new JRadioButton[numButtons]; final ButtonGroup group = new ButtonGroup(); JButton voteButton = null; final String defaultMessageCommand = "default"; final String yesNoCommand = "yesno"; final String yeahNahCommand = "yeahnah"; final String yncCommand = "ync"; final String yesncommand = "yeah"; radioButtons[0] = new JRadioButton("OPTION.. 1: INPUT YOUR DETAILS"); radioButtons[0].setActionCommand(defaultMessageCommand); radioButtons[1] = new JRadioButton("OPTION.. 2: SAVE"); radioButtons[1].setActionCommand(yesNoCommand); radioButtons[2] = new JRadioButton("OPTION.. 3: PRINT TO SCREEN"); radioButtons[2].setActionCommand(yeahNahCommand); radioButtons[3] = new JRadioButton("OPTION.. 4: SEARCHcopyright bond"); radioButtons[3].setActionCommand(yncCommand); radioButtons[4] = new JRadioButton ("OPTION.. 4: SORTcopyright bond"); radioButtons[4].setActionCommand(yesncommand); for (int i = 0; i < numButtons; i++) { group.add(radioButtons[i]); } // Select the first button by default. radioButtons[0].setSelected(true); voteButton = new JButton("!RUN!"); voteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String command = group.getSelection().getActionCommand(); // ok dialog if (command == defaultMessageCommand) { JOptionPane.showMessageDialog(frame, "YOU HAVE CHOSEN TO RUN THE MAIN PROGRAM"); // yes/no dialog } else if (command == yesNoCommand) { int n = JOptionPane.showConfirmDialog( frame, "YOU HAVE DECIDED TO SAVE", "DISPLAY VALUES", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) { setLabel("YOU ARE SAVING"); } else if (n == JOptionPane.NO_OPTION) { setLabel("YOU ARE BACK TO MAIN SCREEN"); } else { setLabel("BE SURE TO RUN IT"); } // yes/no (with customized wording) } else if (command == yeahNahCommand) { Object[] options = {"Yes, Run", "No, DON`T RUN"}; int n = JOptionPane.showOptionDialog(frame, "THIS IS THE OPENING PROGRAM", "A Follow-up Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (n == JOptionPane.YES_OPTION) { setLabel("YOU ARE DISPLAYING"); } else if (n == JOptionPane.NO_OPTION) { setLabel("!YOU ARE BACK TO MAIN SCREEN!"); } else { setLabel("YOU HAVE ONLY TWO CHOICES SORRY!"); } // yes/no/cancel (with customized wording) } else if (command == yncCommand) { Object[] options = {"Yes!", "NO, I'll pass", "OPTIONS"}; int n = JOptionPane.showOptionDialog(frame, "YOU ARE RUNNING SEARCH " + "ENJOY!", "A Follow-up Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]); if (n == JOptionPane.YES_OPTION) { setLabel("YOU ARE RUNNING SEARCH"); } else if (n == JOptionPane.NO_OPTION) { setLabel("YOU ARE NOT RUNNING THE SEARCH"); } else if (n == JOptionPane.CANCEL_OPTION) { setLabel("QUIT"); } else { setLabel("BE SURE TO RUN THE PROGRAM"); } } else if (command == yesncommand) { Object[] optionsP = {"Yes!", "NO, I'll pass", "OPTIONS"}; int n2 = JOptionPane.showOptionDialog(frame, "YOU ARE RUNNING SORT " + "ENJOY!", "A Follow-up Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, optionsP, optionsP[2]); if (n2 == JOptionPane.YES_OPTION) { setLabel("YOU ARE RUNNING SEARCH"); } else if (n2 == JOptionPane.NO_OPTION) { setLabel("YOU ARE NOT RUNNING THE SORT"); } else if (n2 == JOptionPane.CANCEL_OPTION) { setLabel("QUIT"); } else { setLabel("BE SURE TO RUN THE PROGRAM"); } } return; } }); return createPane(simpleDialogDesc + ":",radioButtons,voteButton); } private JPanel createPane(String description, JRadioButton[] radioButtons, JButton showButton) { int numChoices = radioButtons.length; JPanel box = new JPanel(); JLabel label = new JLabel(description); box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS)); box.add(label); for (int i = 0; i < numChoices; i++) { box.add(radioButtons[i]); } JPanel pane = new JPanel(); pane.setLayout(new BorderLayout()); pane.add(box, BorderLayout.NORTH); pane.add(showButton, BorderLayout.SOUTH); return pane; } public static void main(String[] args) { JFrame frame = new JFrame("THE TELEPHONE DIRECTORY"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(1,1)); contentPane.add(new VoteDialog(frame)); // Exit when the window is closed. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }