Subclassing java.awt.Dialog

The previous example was a little artificial. Normally you create your own subclass of Dialog and instantiate that subclass from the main program. For example one of the simpler common dialogs is a notification dialog that gives the user a message to which they can say OK to signify that they've read it. The following program is such a Dialog subclass.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class YesNoDialog extends Dialog implements ActionListener {

  public YesNoDialog(Frame parent, String message) {
  
    super(parent, true);
    this.add("Center", new Label(message));
    Panel p = new Panel();
    p.setLayout(new FlowLayout());
    Button yes = new Button("Yes");
    yes.addActionListener(this);
    p.add(yes);
    Button no = new Button("No");
    no.addActionListener(this);
    p.add(no);
    this.add("South", p);
    this.setSize(300,100);
    this.setLocation(100, 200);
  
  }  
  
  public void actionPerformed(ActionEvent e) {
    this.setVisible(false);
    this.dispose();
  }
  
}


class AlertExample extends Applet {

  public void init () {
  
    Dialog d = new YesNoDialog(new Frame(), 
     "Are you sure you want to start global thermonuclear war?");
    d.show();
    
  }

}

Previous | Next | Top
Last Modified November 15, 1999
Copyright 1997-1999 Elliotte Rusty Harold
elharo@metalab.unc.edu