1  // File:  GridBagLayout1.java
  2  
  3  import java.applet.Applet;
  4  import java.awt.*;
  5  import java.awt.event.*;
  6  
  7  public class GridBagLayout1 extends Applet {
  8    private Button formatButton;
  9    public void init() {
 10      formatButton = new Button("Text Format");
 11      formatButton.addActionListener(new ButtonHandler());
 12      add(formatButton);
 13    }
 14  }
 15  
 16  class ButtonHandler implements ActionListener 
 17  {
 18    FormatDialog formatDialog;
 19    public ButtonHandler(){}
 20    public void actionPerformed(ActionEvent e)  {
 21      formatDialog = new FormatDialog("Text Format Dialog");
 22    }
 23  }
 24  
 25  class FormatDialog extends Frame implements ActionListener, ItemListener{
 26    private List style;
 27    private Checkbox bold;
 28    private Checkbox italic;
 29    private TextField size;
 30    private TextArea sample;
 31    private Button quit;
 32    
 33    FormatDialog(String title) {
 34      super(title);
 35      GridBagLayout gbl = new GridBagLayout();
 36      setLayout(gbl);
 37  
 38      style = new List(3, false);
 39      style.addItem("Courier");
 40      style.addItem("Dialog");
 41      style.addItem("DialogInput");
 42      style.addItem("Helvetica");
 43      style.addItem("Times Roman");
 44      style.addItem("Zapf Dingbats");
 45      style.select(0);
 46      style.addItemListener(this);
 47      
 48  
 49      bold = new Checkbox("Bold");
 50      bold.addItemListener(this);
 51      italic = new Checkbox("Italic");
 52      italic.addItemListener(this);
 53      
 54      Label label = new Label("Size ");
 55      size = new TextField("14");
 56      size.setEditable(false);
 57      sample = new TextArea(4,20);
 58      
 59      quit=new Button("QUIT");
 60      quit.addActionListener(this);
 61      quit.setBackground( Color.blue );
 62      quit.setForeground( Color.white ); 
 63      quit.setFont(new Font("SansSerif", Font.BOLD,10));
 64      GridBagConstraints gbc = new GridBagConstraints();
 65      gbc.fill = GridBagConstraints.BOTH;
 66      gbc.weightx = 20;
 67      gbc.weighty = 100;
 68      add(style, gbl, gbc, 0, 0, 1, 3);
 69      add(quit,gbl, gbc,2,0,1,2);
 70      gbc.weightx = 100;
 71      gbc.fill = GridBagConstraints.NONE;
 72      gbc.anchor = GridBagConstraints.CENTER;
 73      add(bold, gbl, gbc, 1, 0, 1, 1);
 74      add(italic, gbl, gbc, 1, 1, 1, 1);
 75      add(label, gbl, gbc, 1, 2, 1, 1);
 76      
 77      gbc.fill = GridBagConstraints.HORIZONTAL;
 78      add(size, gbl, gbc, 2, 2, 1, 1);
 79      gbc.anchor = GridBagConstraints.SOUTH;
 80      gbc.weighty = 0;
 81      add(sample, gbl, gbc, 0, 10, 10, 0);
 82      sample.setText("This is the default font attributes............");
 83      setSize(350,200);
 84      setVisible(true);
 85    }
 86  
 87    private void add(Component c, GridBagLayout gbl, GridBagConstraints gbc, 
 88                     int x, int y, int w, int h) {
 89      gbc.gridx = x;
 90      gbc.gridy = y;
 91      gbc.gridwidth = w;
 92      gbc.gridheight = h;
 93      gbl.setConstraints(c, gbc);
 94      add(c);
 95    }
 96    public void actionPerformed( ActionEvent e) {
 97      if (e.getSource()==quit)
 98        dispose();
 99    }
100    
101    public void itemStateChanged(ItemEvent e) 
102    {
103      int valBold= (bold.getState() ? Font.BOLD: Font.PLAIN);
104      int valItalic=(italic.getState() ? Font.ITALIC: Font.PLAIN);
105      int fontSize = Integer.valueOf(size.getText()).intValue();
106      sample.setFont(new Font(style.getSelectedItem(), valBold+valItalic, fontSize));
107      sample.setText(" Style: " + style.getSelectedItem() + 
108  		     "\n Bold: " + bold.getState() + 
109  		     "\n Italic: " + italic.getState());
110      sample.setVisible(true);
111    }
112  }
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123