4. AWT 1.1 Containers

4.1. Frames

The Frame class provides windows for applets and applications. Every application needs at least one Frame.

4.1.1. Code Example

public class MenuWindow extends Frame {
       boolean inAnApplet = true;
       TextArea output;

       public MenuWindow() {
              //Calls the Frame constructor and adds components to the window
              addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
                      if (inAnApplet) {
                          dispose();
                      } else {
                          System.exit(0);
                      }
                   }
              }

         . . .

         public static void main(String args[]) {
                MenuWindow window = new MenuWindow();
                window.inAnApplet = false;
                window.setTitle("MenuWindow Application");
                window.setSize(450, 200);
                window.setVisible(true);
         }
}

4.1.2. Other methods provided by Frame

  • String getTitle() and void setTitle(String)

    Returns or sets (respectively) the title of the frame's window.

  • Image getIconImage() and void setIconImage(Image)

    Returns or sets (respectively) the image displayed when the window is iconified.

  • MenuBar getMenuBar() and void setMenuBar(MenuBar)

    Returns or sets (respectively) the menu bar for this Frame.

  • void remove(MenuComponent)

    Removes the specified menu bar from this Frame.

4.2. Panels

The Panel class is a general-purpose Container subclass. You can use it as-is to hold Components, or you can define a subclass to perform special functionality, such as event handling for the objects the Panel contains.

[Note]Note

The Applet class is a Panel subclass with special hooks to run in a browser or other applet viewer.

4.2.1. Code Example

Panel p1 = new Panel();
p1.add(new Button("Button 1"));
p1.add(new Button("Button 2"));
p1.add(new Button("Button 3"));

A Panel subclass that draws a frame around its contents.

Figure 23.16. Panel Subclass

Panel Subclass

class FramedArea extends Panel {
    public FramedArea(CoordinatesDemo controller) {
        ...//Set the layout manager.
           //Add any Components this Panel contains...
    }

    //Ensure that no Component is placed on top of the frame.
    //The inset values were determined by trail and error.
    public Insets getInsets() {
        return new Insets(4,4,5,5);
    }

    //Draw the frame at this Panel's edges.
    public void paint(Graphics g) {
        Dimension d = getSize();
        Color bg = getBackground();
 
        g.setColor(bg);
        g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
        g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
    }
}

4.3. Dialogs

The AWT provides support for dialogs -- windows that are dependent on other windows -- with the Dialog class. It provides a useful subclass, FileDialog,that provides dialogs to help the user open and save files.

Figure 23.17. Dialog

Dialog

4.3.1. Code Example

class SimpleDialog extends Dialog implements ActionListener {
      TextField field;
      DialogWindow parent;
      Button setButton;

      SimpleDialog(Frame dw, String title) {
            super(dw, title, false);
            parent = (DialogWindow)dw;
           
            ...//Create and add components, such as the set button.
           
            //Initialize this dialog to its preferred size.
            pack();
      }

      public void actionPerformed(ActionEvent event) {
             Object source= event.getSource();
             if ((source == setButton) | (source == field))
                      parent.setText(field.getText());
             field.selectAll();
             setVisible(false);
      }
}
     
//Here's the code that brings up the dialog: 

if (dialog == null)
   dialog = new SimpleDialog(this, "A Simple Dialog");

dialog.setVisible(true);