0% found this document useful (0 votes)
16 views4 pages

Ajp 4

1. The document describes an experiment involving two Java programs that use GridBagLayout to create GUI applications with multiple buttons and form elements. 2. The first program creates a window with four buttons arranged in a 2x2 grid using GridBagConstraints to control each button's position. 3. The second program creates a simple form with a name and comment text field along with submit button, using GridBagLayout and constraints to position the elements in a grid.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Ajp 4

1. The document describes an experiment involving two Java programs that use GridBagLayout to create GUI applications with multiple buttons and form elements. 2. The first program creates a window with four buttons arranged in a 2x2 grid using GridBagConstraints to control each button's position. 3. The second program creates a simple form with a name and comment text field along with submit button, using GridBagLayout and constraints to position the elements in a grid.

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 PDF, TXT or read online on Scribd
You are on page 1/ 4

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);
}

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;
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 04 DOS:

add(comp, gc);
}

public static void main(String[] args) {


new Pr4Ex2();
}
}

Output

You might also like