You are on page 1of 5

Name of Student: Ekambe Ganesh Roll No.

: 53

Experiment No.: 04 DOS:

Exercise:

1.

import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;

public class Pr4Ex1 extends JFrame {


public Pr4Ex1() {
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new Button("Button One"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(new Button("Button Two"), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
this.add(new Button("Button Three"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
this.add(new Button("Button Four"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 04 DOS:

this.add(new Button("Button Five"), gbc);


setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Pr4Ex1();
}
}

Output
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 04 DOS:

2.

import java.awt.*;

public class Pr4Ex2 extends Frame {


Pr4Ex2() {
setVisible(true);
setSize(500, 300);

Label lblName = new Label("Name");


TextField txtName = new TextField(" ");
Label lblComments = new Label("Comments");
TextArea textAreaComments = new TextArea(6, 15);
Button btnSubmit = new Button("Submit");

setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();

add(lblName, gc, 0, 0, 1, 1, 0, 0);


add(txtName, gc, 1, 0, 1, 1, 0, 20);
add(lblComments, gc, 0, 1, 1, 1, 0, 0);
add(textAreaComments, gc, 1, 1, 1, 1, 0, 60);
add(btnSubmit, gc, 0, 2, 2, 1, 0, 20);
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 04 DOS:

void add(Component comp, GridBagConstraints gc, int x, int y, int w, int h,


int wx, int wy) {
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = w;
gc.gridheight = h;
gc.weightx = wx;
gc.weighty = wy;
add(comp, gc);
}

public static void main(String[] args) {


new Pr4Ex2();
}
}

Output
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 04 DOS:

You might also like