GridLayout
class is a layout manager that
lays out a container's components in a rectangular grid.
The container is divided into equal-sized rectangles,
and one component is placed in each rectangle. For example, the following is an applet that lays out 3 textfields and 3 labels into three rows and two columns:import javax.swing.*;
import java.awt.*;
public class DemoGridLayout extends JFrame{
public DemoGridLayout(){
setTitle("Demo Grid Layout");
JPanel p1 = new JPanel();
GridLayout f = new GridLayout(3, 2, 5, 5);
p1.setLayout(f);
JLabel lblFirstName = new JLabel("First Name");
JLabel lblMiddleName = new JLabel("Middle Name");
JLabel lblLastName = new JLabel("Last Name");
JTextField txtFirstName = new JTextField(8);
JTextField txtMiddleName = new JTextField(1);
JTextField txtLastName = new JTextField(8);
p1.add(lblFirstName); p1.add(txtFirstName);
p1.add(lblMiddleName); p1.add(txtMiddleName);
p1.add(lblLastName); p1.add(txtLastName);
add(p1);
}
public static void main(String[]args) {
JFrame frame = new DemoGridLayout();
frame.setVisible(true);
frame.setSize(200,125);
frame.setDefaultCloseOperation(2);
frame.setLocationRelativeTo(null);
}
}
output :
0 comments
Post a Comment