A Program from "Java by Examples" does not run

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • King W.Wang

    A Program from "Java by Examples" does not run

    Hi Java gurus,
    I've copied the following program from the book "Java by Examples".
    It compiles well with javac. But it does not run as expected.

    EXPECTED:
    It is expected that, when you input something in the text field on the
    dialog window and click the OK button, the dialog window should
    disappear and the text should be printed on the second window.

    REAL BEHAVIOR:
    After the OK button is clicked, the dialog window does not disappear,
    and no text is printed on the second window, and the program cannot
    be closed.

    MY QUESTION:
    How should the program be modified so that it will behave as expected.
    Many thanks in advance!

    Weichao

    /*************** *** the program *************** *******/
    import java.awt.*;
    import java.applet.*;
    public class DialogApplet extends Applet {
    DialogFrame frame;
    Button button;
    public void init() {
    frame = new DialogFrame("Di alog Window");
    button = new Button("Show Window");
    add(button);
    }
    public boolean action(Event evt, Object arg) {
    boolean visible = frame.isShowing ();
    if (visible) {
    frame.hide();
    button.setLabel ("Show Window");
    } else {
    frame.show();
    button.setLabel ("Hide Window");
    }
    return true;
    }
    }
    class DialogFrame extends Frame {
    MenuBar menuBar;
    Dialog dialog;
    TextField textField;
    String str;
    DialogFrame(Str ing title) {
    super(title);
    menuBar = new MenuBar();
    setMenuBar(menu Bar);
    Menu menu = new Menu("Test");
    menuBar.add(men u);
    MenuItem item = new MenuItem("Dialo g box");
    menu.add(item);
    str = "";
    }
    public void paint(Graphics g) {
    resize(300, 250);
    g.drawString("T HE TEXT YOU ENTERED IS:", 20, 80);
    g.drawString(st r, 20, 100);
    }
    public boolean action(Event evt, Object arg) {
    if (evt.target instanceof MenuItem) {
    if (arg == "Dialog box")
    ShowDialogBox() ;
    } else if (evt.target instanceof Button) {
    if (arg == "OK") {
    dialog.dispose( );
    str = textField.getTe xt();
    repaint();
    }
    }
    return true;
    }
    protected void ShowDialogBox() {
    dialog = new Dialog(this, "Test Dialog", true);
    FlowLayout layout = new FlowLayout();
    dialog.setLayou t(layout);
    textField = new TextField("", 20);
    Button button = new Button("OK");
    dialog.add(butt on);
    dialog.add(text Field);
    dialog.show();
    dialog.resize(2 00, 100);
    }
    }
    /************* End of the program *************** **************/
Working...