import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* This example shows AWT Dialog.
* @author TogetherSoft
*/
public class TestDialog extends Dialog {
public TestDialog(Frame frame) {
super(frame);
initGUI();
}
/** This method is called from within the constructor to initialize the form. */
void initGUI() {
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) { thisWindowClosing(e); }
});
setBounds(new Rectangle(0, 0, 500, 300));
setResizable(false);
setTitle("Test Dialog");
add(buttonPanel, BorderLayout.SOUTH);
add(contentPanel, BorderLayout.CENTER);
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(butttonHolder);
butttonHolder.setLayout(new GridLayout(1, 0, 5, 0));
butttonHolder.add(okButton);
butttonHolder.add(cancelButton);
okButton.setLabel("OK");
cancelButton.setLabel("Cancel");
okButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) { okButtonActionPerformed(e); }
});
cancelButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) { cancelButtonActionPerformed(e); }
});
contentPanel.setLayout(null);
contentPanel.add(optionLabel);
optionLabel.setText("Select one of the options below");
optionLabel.setBounds(new java.awt.Rectangle(121, 70, 186, 35));
}
void cancel() {
dispose();
System.exit(0);
}
void ok() {
Checkbox box = checkboxGroup1.getSelectedCheckbox();
System.out.println(box.getLabel() + " is selected");
cancel();
}
public void thisWindowClosing(WindowEvent e) {
cancel();
}
public void okButtonActionPerformed(ActionEvent e) {
ok();
}
public void cancelButtonActionPerformed(ActionEvent e) {
cancel();
}
public static void main(String[] args) {
Frame frame = new Frame();
TestDialog dialog = new TestDialog(frame);
dialog.show();
}
private Panel contentPanel = new Panel();
private Panel buttonPanel = new Panel();
private Panel butttonHolder = new Panel();
private Button okButton = new Button();
private Button cancelButton = new Button();
private CheckboxGroup checkboxGroup1 = new CheckboxGroup();
private Label optionLabel = new Label();
}
|