Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 18th, 2013  |  syntax: None  |  size: 1.44 KB  |  views: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package prog02;
  2.  
  3. import javax.swing.*;
  4.  
  5. /**
  6.  *
  7.  * @author vjm
  8.  */
  9. public class GUI implements UserInterface {
  10.  
  11.         /** Creates a new instance of GUI */
  12.         public GUI() {
  13.         }
  14.  
  15.         /** presents set of commands for user to choose one of
  16.         @param commands the commands to choose from
  17.         @return the index of the command in the array
  18.          */
  19.         public int getCommand (String[] commands) {
  20.                 return JOptionPane.showOptionDialog
  21.                                 (null, // No parent
  22.                                                 "Select a Command", // Prompt message
  23.                                                 "PhoneDirectory", // Window title
  24.                                                 JOptionPane.YES_NO_CANCEL_OPTION, // Option type
  25.                                                 JOptionPane.QUESTION_MESSAGE, // Message type
  26.                                                 null, // Icon
  27.                                                 commands, // List of commands
  28.                                                 commands[commands.length - 1]);
  29.         }
  30.  
  31.         /** tell the user something
  32.         @param message string to print out to the user
  33.          */
  34.         public void sendMessage (String message) {
  35.                 JOptionPane.showMessageDialog(null, message);
  36.         }
  37.  
  38.         /** prompts the user for a string
  39.         @param prompt the request
  40.         @return what the user enters, null if nothing
  41.          */
  42.         public String getInfo (String prompt) {
  43.                 return JOptionPane.showInputDialog(prompt);
  44.         }
  45.  
  46.         public static void main (String[] args) {
  47.                 UserInterface ui = new GUI();
  48.                 String[] commands = { "hello", "how", "are", "you" };
  49.                 int choice = ui.getCommand(commands);
  50.                 ui.sendMessage("You chose " + choice);
  51.                 String result = ui.getInfo("say something");
  52.                 ui.sendMessage("You said " + result);
  53.         }
  54. }
clone this paste RAW Paste Data