Dialog Boxes
Next
Java Source
Applet
// Once a <yes|no|maybe> query box and "application" three seconds after
// every choice. Applet does not run in Netscape.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
class QueryBox extends Dialog implements ActionListener {
private String result;
Button yes, may, not;
QueryBox (Frame f, String question) {
super (f, question);
add ("Center", new Label(question, Label.CENTER));
Panel p = new Panel();
p.add (yes = new Button("Yes"));
p.add (may = new Button("Maybe"));
p.add (not = new Button("No"));
yes.addActionListener(this);
may.addActionListener(this);
not.addActionListener(this);
add("South", p);
setSize(200,100);
show();
}
synchronized public String answer () {
try {
wait();
}
catch (Exception e) { }
return result;
}
synchronized public void actionPerformed (ActionEvent evt) {
if (evt.getSource() == yes) result = "Yes";
else if (evt.getSource() == may) result = "Maybe";
else if (evt.getSource() == not) result = "No";
notifyAll();
dispose();
}
}
class Application extends Frame implements ActionListener {
Button b;
JTextField t, out;
int i=0;
Application() {
setLayout(new BorderLayout());
add ("North", out = new JTextField());
Panel p = new Panel();
p.setLayout(new GridLayout(1,2));
p.add(b = new Button("Press Here"));
p.add(t = new JTextField());
b.addActionListener(this);
add("Center", p);
out.setEditable(false);
setSize(200,100);
show();
}
public void actionPerformed (ActionEvent evt) {
if (evt.getSource() == b) t.setText(String.valueOf(i++));
}
}
public class Query extends Applet implements Runnable, ActionListener {
String ans;
Button go;
JTextField text, out;
Thread runner;
int i=0;
public void init () {
setLayout(new BorderLayout());
add("North", text = new JTextField());
Panel p = new Panel();
p.setLayout(new GridLayout(1,2));
p.add(go = new Button("Press Here"));
p.add(out = new JTextField());
go.addActionListener(this);
add ("Center", p);
text.setEditable(false);
setSize(150,70);
runner = new Thread(this);
runner.start();
}
public void actionPerformed (ActionEvent evt) {
if (evt.getSource() == go) out.setText(String.valueOf(i++));
}
public void run () {
while (true) {
try {
runner.sleep(3000);
}
catch (Exception e) { }
Application app = new Application();
QueryBox query = new QueryBox (app, "Hello There");
text.setText(ans=query.answer());
app.out.setText(ans);
}
}
}