By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,742 Members | 793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,742 IT Pros & Developers. It's quick & easy.

A Program from "Java by Examples" does not run

P: n/a
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("Dialog 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(String title) {
super(title);
menuBar = new MenuBar();
setMenuBar(menuBar);
Menu menu = new Menu("Test");
menuBar.add(menu);
MenuItem item = new MenuItem("Dialog box");
menu.add(item);
str = "";
}
public void paint(Graphics g) {
resize(300, 250);
g.drawString("THE TEXT YOU ENTERED IS:", 20, 80);
g.drawString(str, 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.getText();
repaint();
}
}
return true;
}
protected void ShowDialogBox() {
dialog = new Dialog(this, "Test Dialog", true);
FlowLayout layout = new FlowLayout();
dialog.setLayout(layout);
textField = new TextField("", 20);
Button button = new Button("OK");
dialog.add(button);
dialog.add(textField);
dialog.show();
dialog.resize(200, 100);
}
}
/************* End of the program *****************************/
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.