1 import java.applet.Applet; 2 import java.awt.*; 3 4 public class GridBagLayout1 extends Applet { 5 private Button formatButton; 6 FormatDialog formatDialog; 7 8 public void init() { 9 formatButton = new Button("Text Format"); 10 add(formatButton); 11 } 12 13 public boolean action(Event evt, Object arg) { 14 if (arg.equals("Text Format")) { 15 formatDialog = new FormatDialog("Text Format Dialog"); 16 } 17 else return super.action(evt,arg); 18 return true; 19 } 20 } 21 22 class FormatDialog extends Frame { 23 private List style; 24 private Checkbox bold; 25 private Checkbox italic; 26 private TextField size; 27 private TextArea sample; 28 29 FormatDialog(String title) { 30 super(title); 31 GridBagLayout gbl = new GridBagLayout(); 32 setLayout(gbl); 33 34 style = new List(3, false); 35 style.addItem("Courier"); 36 style.addItem("Dialog"); 37 style.addItem("DialogInput"); 38 style.addItem("Helvetica"); 39 style.addItem("Times Roman"); 40 style.addItem("Zapf Dingbats"); 41 style.select(0); 42 43 bold = new Checkbox("Bold"); 44 italic = new Checkbox("Italic"); 45 Label label = new Label("Size "); 46 size = new TextField("14"); 47 size.setEditable(false); 48 sample = new TextArea(4,20); 49 50 GridBagConstraints gbc = new GridBagConstraints(); 51 gbc.fill = GridBagConstraints.BOTH; 52 gbc.weightx = 20; 53 gbc.weighty = 100; 54 add(style, gbl, gbc, 0, 0, 1, 3); 55 gbc.weightx = 100; 56 gbc.fill = GridBagConstraints.NONE; 57 gbc.anchor = GridBagConstraints.CENTER; 58 add(bold, gbl, gbc, 1, 0, 2, 1); 59 add(italic, gbl, gbc, 1, 1, 2, 1); 60 add(label, gbl, gbc, 1, 2, 1, 1); 61 gbc.fill = GridBagConstraints.HORIZONTAL; 62 add(size, gbl, gbc, 2, 2, 1, 1); 63 gbc.anchor = GridBagConstraints.SOUTH; 64 gbc.weighty = 0; 65 add(sample, gbl, gbc, 0, 10, 10, 0); 66 sample.setText("This is the default font attributes............"); 67 resize(350,200); 68 show(); 69 } 70 71 private void add(Component c, GridBagLayout gbl, GridBagConstraints gbc, 72 int x, int y, int w, int h) { 73 gbc.gridx = x; 74 gbc.gridy = y; 75 gbc.gridwidth = w; 76 gbc.gridheight = h; 77 gbl.setConstraints(c, gbc); 78 add(c); 79 } 80 81 public boolean handleEvent(Event evt) { 82 if ((evt.target.equals(bold) || evt.target.equals(italic) 83 || evt.target.equals(style))) { 84 int i = (bold.getState() ? Font.BOLD : 0) + 85 (italic.getState() ? Font.ITALIC : 0); 86 int fontSize = Integer.valueOf(size.getText()).intValue(); 87 sample.setFont(new Font(style.getSelectedItem(), i, fontSize)); 88 sample.setText(" Style: " + style.getSelectedItem() + 89 "\n Bold: " + bold.getState() + 90 "\n Italic: " + italic.getState()); 91 sample.show(); 92 } 93 else if (evt.id == Event.WINDOW_DESTROY) hide(); 94 return super.handleEvent(evt); 95 } 96 }