Dialog 类是weinow 的子类,但是它必须依赖于别的窗口
package dialog;
import java.awt.*;
import java.awt.event.*;
class MyDialogs extends Dialog implements ActionListener {
static final int YES = 1,NO = 0;
int message = -1;
Button yes,no;
MyDialogs(Frame f,String s,boolean b){
super(f,s,b);
yes = new Button("YES");
no = new Button("NO");
no.addActionListener(this);
yes.addActionListener(this);
setLayout(new FlowLayout());
add(yes);
add(no);
setBounds(60, 60, 100, 100);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
message = -1;
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == yes){
System.out.println("click yes");
message = YES;
setVisible(false);
} else if(e.getSource() == no){
message = NO;
setVisible(false);
}
}
public int getMessage(){
return message;
}
}
class Dwindow extends Frame implements ActionListener{
TextArea text;
Button button;
MyDialogs dialog;
Dwindow(String s){
super(s);
text = new TextArea(10,22);
button = new Button("open dialog");
button.addActionListener(this);
setLayout(new FlowLayout());
add(button);
add(text);
dialog = new MyDialogs(this,"my have mode", true);
setBounds(60, 60, 300,300);
setVisible(true);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == button){
dialog.setVisible(true);
if(dialog.getMessage() == MyDialogs.YES){
text.append("\n你单击了对话框的yes按钮");
}else if(dialog.getMessage() == MyDialogs.NO){
text.append("\n你单击了对话框的NO按钮");
}
}
}
}
public class MyDialog{
public static void main(String args[]){
new Dwindow("带对话框的窗口");
}
}