FileDocCategorySizeDatePackage
DisplayMessage.javaAPI DocExample3095Mon Oct 11 15:07:22 BST 1999None

DisplayMessage

public class DisplayMessage extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    /*
     * Step 1: Create the components
     */
    JLabel msgLabel = new JLabel();      // Component to display the question
    JButton yesButton = new JButton();   // Button for an affirmative response
    JButton noButton = new JButton();    // Button for a negative response

    /*
     * Step 2: Set properties of the components
     */
    msgLabel.setText(args[0]);                           // The msg to display
    msgLabel.setBorder(new EmptyBorder(10,10,10,10));    // A 10-pixel margin 
    yesButton.setText((args.length >= 2)?args[1]:"Yes"); // Text for Yes button
    noButton.setText((args.length >= 3)?args[2]:"No");   // Text for no button

    /*
     * Step 3: Create containers to hold the components
     */
    JFrame win = new JFrame("Message");  // The main application window
    JPanel buttonbox = new JPanel();     // A container for the two buttons
    
    /*
     * Step 4: Specify LayoutManagers to arrange components in the containers
     */
    win.getContentPane().setLayout(new BorderLayout()); // layout on borders
    buttonbox.setLayout(new FlowLayout());              // layout left-to-right

    /*
     * Step 5: Add components to containers, with optional layout constraints
     */
    buttonbox.add(yesButton);            // add yes button to the panel
    buttonbox.add(noButton);             // add no button to the panel
    
    // add JLabel to window, telling the BorderLayout to put it in the middle
    win.getContentPane().add(msgLabel, "Center");        

    // add panel to window, telling the BorderLayout to put it at the bottom
    win.getContentPane().add(buttonbox, "South");  
    
    /*
     * Step 6: Arrange to handle events in the user interface.
     */
    yesButton.addActionListener(new ActionListener() {  // Note: inner class
      // This method is called when the Yes button is clicked.
      public void actionPerformed(ActionEvent e) { System.exit(0); }
    });

    noButton.addActionListener(new ActionListener() {   // Note: inner class
      // This method is called when the No button is clicked.
      public void actionPerformed(ActionEvent e) { System.exit(1); }
    });

    /*
     * Step 7: Display the GUI to the user
     */
    win.pack();   // Set the size of the window based its children's sizes.
    win.show();   // Make the window visible.