MathBits.com
Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use

Message Boxes

A message box displays a message on the screen.  When activated by a button, the message string appears in a separate box.  A literal statement can be sent to the message box (as seen below) or a variable may be sent to the message box.

messageBox ("I love Java!!!");

To close the box, click the OK button
(or the x in the upper right corner of the window).

The message box can accept a String, double or integer as a parameter.

// Example of message box

import java.io.*;
import java.awt.*;
import BreezyGUI.*;

public class Box extends GBFrame
{

     Button displayButton = addButton("Display Message",4,1,2,1);

     public void buttonClicked (Button buttonObj)
     {
          String outputstring = "I love Java!!!";  // build an output string
          messageBox(outputstring);                // send the output string to the box
     }
     public static void main(String[] args)
     {
          Frame frm=new Box( );
          frm.setSize(200,150);
          frm.setVisible(true);
     }
}

To print a money amount in a message box, use DecimalFormat
(don't forget import java.text.*;)

//display calculations to look like a true money amount
DecimalFormat money= new DecimalFormat ("0.00");
outputString="Your profit will be: $"+money.format(profit)+".";
messageBox(outputString);

 


Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use