0% found this document useful (0 votes)
9 views5 pages

Ajp 4

1. The document describes an experiment involving the use of GridBagLayout in Java. It includes code for two exercises that create GUI frames with buttons and text fields arranged in a grid using GridBagConstraints. 2. The first exercise creates a frame with five buttons in a 2x3 grid. The second creates a simple form with labels, text fields, a text area, and a submit button arranged in a grid. 3. Both exercises demonstrate the use of GridBagLayout and GridBagConstraints to position components in a GUI grid layout.

Uploaded by

Ganesh Ekambe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Ajp 4

1. The document describes an experiment involving the use of GridBagLayout in Java. It includes code for two exercises that create GUI frames with buttons and text fields arranged in a grid using GridBagConstraints. 2. The first exercise creates a frame with five buttons in a 2x3 grid. The second creates a simple form with labels, text fields, a text area, and a submit button arranged in a grid. 3. Both exercises demonstrate the use of GridBagLayout and GridBagConstraints to position components in a GUI grid layout.

Uploaded by

Ganesh Ekambe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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