import java.awt.*; import java.awt.event.*; /** * Layout games part 1. * (part 2 is in file ConverterDemo1a.java - BorderLayout) * GridLayouts and FlowLayouts * Effect of pack() * Default layout of Frame is BorderLayout. */ public class ConverterDemo1gridnp extends Frame { Button btnConvert, btnClear, btnExit; Label lblFrom, lblTo; TextField tfFrom, tfTo; ConverterDemo1gridnp () { super("Unit Conversion Demo"); // call a parent constructor // (must come first) btnConvert = new Button("Convert"); btnClear = new Button("Clear"); btnExit = new Button("Exit"); lblFrom = new Label("FROM:"); lblTo = new Label("TO:"); tfFrom = new TextField(20); tfTo = new TextField(20); //setLayout(new FlowLayout()); setLayout(new GridLayout(0,2)); // 2 cols, any num rows add(lblFrom); add(lblTo); add(tfFrom); add(tfTo); add(btnConvert); add(btnClear); add(btnExit); /** * Notice that the width and height args of setBounds() have * no effect on flow layout. The same is true of * with GridLayout if you use pack(); Without pack setSize() * works with Frames. */ setBounds(400, 300, 300, 400); //pack(); // Window class. Sets window to preferred size. setVisible(true); this.addWindowListener(new WindowAdapter () { public void windowClosing(WindowEvent wev) { dispose(); } } ); } public static void main(String [] args) { ConverterDemo1 cd1 = new ConverterDemo1(); } }