yesとnoの選択ボタンを持つダイアログと、
その応答による処理の分岐方法は以下のプログラムでわかると思います。
import java.awt.*;
import java.awt.event.*;
class yesnodialog extends Dialog implements ActionListener {
private Button YesButton, NoButton;
private Panel ButtonBase;
public int ResultVal;
public yesnodialog(Frame owner) {
super(owner, "yesnodialog", true);
setSize(200, 100);
add(new Label("Yes or No"), BorderLayout.CENTER);
ButtonBase = new Panel();
add(ButtonBase, BorderLayout.SOUTH);
YesButton = new Button("Yes");
NoButton = new Button("No");
ButtonBase.add(YesButton);
ButtonBase.add(NoButton);
YesButton.addActionListener(this);
YesButton.setActionCommand("1");
NoButton.addActionListener(this);
NoButton.setActionCommand("2");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { setVisible(false); };
});
};
public void actionPerformed(ActionEvent ev) {
if (ev.getActionCommand() == "1") ResultVal = 1;
if (ev.getActionCommand() == "2") ResultVal = 2;
setVisible(false);
};
public int Execute() {
ResultVal = -1;
setVisible(true);
return(ResultVal);
};
}
class mainform extends Frame implements ActionListener {
private Button ActionButton;
private yesnodialog YNDialog;
public mainform() { super ("MainFrame");
setSize(300, 300);
ActionButton = new Button("Show Dialog");
add(ActionButton, BorderLayout.CENTER);
ActionButton.addActionListener(this);
ActionButton.setActionCommand("a");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); };
});
setVisible(true);
YNDialog = new yesnodialog(this);
};
public void actionPerformed(ActionEvent ev) {
if (ev.getActionCommand() == "a") {
switch (YNDialog.Execute()) {
case -1: ActionButton.setLabel("[x]"); break;
case 1: ActionButton.setLabel("Yes"); break;
case 2: ActionButton.setLabel("No"); break;
default: ActionButton.setLabel("Error");
};
};
};
}
public class yesno {
public static void main(String args[]) {
mainform MainForm = new mainform();
};
}