0% found this document useful (0 votes)
6 views6 pages

AJP4

The document contains Java programming exercises that demonstrate the use of CardLayout and GridBagLayout in graphical user interface applications. It includes code examples for creating buttons and layout management in Java Swing. The output of the programs is also mentioned, indicating the expected results of executing the provided code.

Uploaded by

sumedhr56
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)
6 views6 pages

AJP4

The document contains Java programming exercises that demonstrate the use of CardLayout and GridBagLayout in graphical user interface applications. It includes code examples for creating buttons and layout management in Java Swing. The output of the programs is also mentioned, indicating the expected results of executing the provided code.

Uploaded by

sumedhr56
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/ 6

dd

NAME: SUMEDH SANJAY RAUT


CLASS: CO5I(A)
ROLL NO: 532
SUBJECT: AJP(22517)

 PRATICAL NO :- 4

X. Execute the following Program and write the output.


CODE:-

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample extends JFrame implements ActionListener
{
CardLayout card;
JButton b1, b2, b3;
Container c;
CardLayoutExample()
{
c=getContentPane();
card=new CardLayout(40,30);
//create CardLayout object with 40 hor space and 30 ver space
c.setLayout(card);
b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);c.add("b",b2);c.add("c",b3);
}
public void actionPerformed(ActionEvent e)
{
card.next(c);
}
public static void main(String[] args)
{
CardLayoutExample cl=new CardLayoutExample();
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
dd

OUTPUT:-

Exercise
1. Write Java program to display following output
Code:-
import java.awt.Button;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import javax.swing.*;
dd

public class GridBagLayoutProg extends JFrame

public static void main(String[] args) {

new GridBagLayoutProg();

public GridBagLayoutProg()

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;
dd

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;

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

setSize(300, 300);

setPreferredSize(getSize());

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}
Output:-
dd

2. Write Java Program to display following output.


Code:-

import java.awt.*;

class GridBagLayoutProg2 extends Frame {

GridBagLayoutProg2() {

Label l1 = new Label("Name");

TextField t1 = new TextField(10);

Label l2 = new Label("Comments");

TextArea T1 = new TextArea(6, 15);

Button b1 = new Button("Submit");

setLayout(new GridBagLayout());

GridBagConstraints gc = new GridBagConstraints();

addComponent(l1, gc, 0, 0, 1, 1, 0, 0);

addComponent(t1, gc, 1, 0, 1, 1, 0, 20);

addComponent(l2, gc, 0, 1, 1, 1, 0, 0);

addComponent(T1, gc, 1, 1, 1, 1, 0, 60);

addComponent(b1, gc, 0, 2, 2, 1, 0, 20);

setSize(400, 250);

setVisible(true);

void addComponent(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;
dd

gc.gridheight = h;

gc.weightx = wx;

gc.weighty = wy;

add(comp, gc);

public static void main(String[] args) {

new GridBagLayoutProg2();

Output:-

You might also like