// File: GridBagLayout1.java import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class GridBagLayout1 extends Applet { private Button formatButton; public void init() { formatButton = new Button("Text Format"); formatButton.addActionListener(new ButtonHandler()); add(formatButton); } } class ButtonHandler implements ActionListener { FormatDialog formatDialog; public ButtonHandler(){} public void actionPerformed(ActionEvent e) { formatDialog = new FormatDialog("Text Format Dialog"); } } class FormatDialog extends Frame implements ActionListener, ItemListener{ private List style; private Checkbox bold; private Checkbox italic; private TextField size; private TextArea sample; private Button quit; 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); style.addItemListener(this); bold = new Checkbox("Bold"); bold.addItemListener(this); italic = new Checkbox("Italic"); italic.addItemListener(this); Label label = new Label("Size "); size = new TextField("14"); size.setEditable(false); sample = new TextArea(4,20); quit=new Button("QUIT"); quit.addActionListener(this); quit.setBackground( Color.blue ); quit.setForeground( Color.white ); quit.setFont(new Font("SansSerif", Font.BOLD,10)); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 20; gbc.weighty = 100; add(style, gbl, gbc, 0, 0, 1, 3); add(quit,gbl, gbc,2,0,1,2); gbc.weightx = 100; gbc.fill = GridBagConstraints.NONE; gbc.anchor = GridBagConstraints.CENTER; add(bold, gbl, gbc, 1, 0, 1, 1); add(italic, gbl, gbc, 1, 1, 1, 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............"); setSize(350,200); setVisible(true); } 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 void actionPerformed( ActionEvent e) { if (e.getSource()==quit) dispose(); } public void itemStateChanged(ItemEvent e) { int valBold= (bold.getState() ? Font.BOLD: Font.PLAIN); int valItalic=(italic.getState() ? Font.ITALIC: Font.PLAIN); int fontSize = Integer.valueOf(size.getText()).intValue(); sample.setFont(new Font(style.getSelectedItem(), valBold+valItalic, fontSize)); sample.setText(" Style: " + style.getSelectedItem() + "\n Bold: " + bold.getState() + "\n Italic: " + italic.getState()); sample.setVisible(true); } }