import java.applet.Applet; import java.awt.*; public class GridBagLayout1 extends Applet { private Button formatButton; FormatDialog formatDialog; public void init() { formatButton = new Button("Text Format"); add(formatButton); } public boolean action(Event evt, Object arg) { if (arg.equals("Text Format")) { formatDialog = new FormatDialog("Text Format Dialog"); } else return super.action(evt,arg); return true; } } class FormatDialog extends Frame { private List style; private Checkbox bold; private Checkbox italic; private TextField size; private TextArea sample; FormatDialog(String title) { super(title); GridBagLayout gbl = new GridBagLayout(); setLayout(gbl); style = new List(3, false); style.addItem("Courier"); style.addItem("Dialog"); style.addItem("DialogInput"); style.addItem("Helvetica"); style.addItem("Times Roman"); style.addItem("Zapf Dingbats"); style.select(0); bold = new Checkbox("Bold"); italic = new Checkbox("Italic"); Label label = new Label("Size "); size = new TextField("14"); size.setEditable(false); sample = new TextArea(4,20); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 20; gbc.weighty = 100; add(style, gbl, gbc, 0, 0, 1, 3); gbc.weightx = 100; gbc.fill = GridBagConstraints.NONE; gbc.anchor = GridBagConstraints.CENTER; add(bold, gbl, gbc, 1, 0, 2, 1); add(italic, gbl, gbc, 1, 1, 2, 1); add(label, gbl, gbc, 1, 2, 1, 1); gbc.fill = GridBagConstraints.HORIZONTAL; add(size, gbl, gbc, 2, 2, 1, 1); gbc.anchor = GridBagConstraints.SOUTH; gbc.weighty = 0; add(sample, gbl, gbc, 0, 10, 10, 0); sample.setText("This is the default font attributes............"); resize(350,200); show(); } private void add(Component c, GridBagLayout gbl, GridBagConstraints gbc, int x, int y, int w, int h) { gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; gbl.setConstraints(c, gbc); add(c); } public boolean handleEvent(Event evt) { if ((evt.target.equals(bold) || evt.target.equals(italic) || evt.target.equals(style))) { int i = (bold.getState() ? Font.BOLD : 0) + (italic.getState() ? Font.ITALIC : 0); int fontSize = Integer.valueOf(size.getText()).intValue(); sample.setFont(new Font(style.getSelectedItem(), i, fontSize)); sample.setText(" Style: " + style.getSelectedItem() + "\n Bold: " + bold.getState() + "\n Italic: " + italic.getState()); sample.show(); } else if (evt.id == Event.WINDOW_DESTROY) hide(); return super.handleEvent(evt); } }