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

Java Practical4

Uploaded by

Priyanka Thadke
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 views6 pages

Java Practical4

Uploaded by

Priyanka Thadke
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/ 6

Name:- Priyanka Khandu Thadke

Class:-A(CO5I) Roll no:- 60


Practical no:-04 Use of CardLayout to write a program to create a two-level card deck that
allows the user to select an operating system.

Program Code:-
package hello;
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();
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(b1, "a");
c.add(b2, "b");
c.add(b3, "c");
}

public void actionPerformed(ActionEvent e) {


card.next(c);
}

public static void main(String[] args) {


CardLayoutExample c1 = new CardLayoutExample();
c1.setSize(400, 400);
c1.setVisible(true);
c1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output:-

Exercise:-
Q.1) Write Java program to display following output.

Program:-

import java.awt.*;

import javax.swing.*;

public class GridBagLayoutE1 extends JFrame

{ public static void main(String[] args) {

GridBagLayoutE1 e1 = new GridBagLayoutE1();

public GridBagLayoutE1(){

GridBagLayout grid = new GridBagLayout();

GridBagConstraints gc = new GridBagConstraints();

setLayout(grid);

setTitle("GridBagLayoutExample");

GridBagLayout layout = new GridBagLayout();

this.setLayout(layout);

gc.fill = GridBagConstraints.HORIZONTAL;

gc.gridx = 0;
gc.gridy = 0;

this.add(new Button("Button One"),gc);

gc.gridx = 1;

gc.gridy = 0;

this.add(new Button("Button Two"),gc);

gc.fill = GridBagConstraints.HORIZONTAL;

gc.ipadx = 20;

gc.gridx = 0;

gc.gridy = 1;

this.add(newButton("Butto Three"),gc);

gc.gridx = 1;

gc.gridy = 1;

this.add(new Button("Button Four"),gc);

gc.gridx = 0;

gc.gridy = 2;

gc.fill= GridBagConstraints.HORIZONTAL;

gc.gridwidth = 2;

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

setSize(200,200);

setPreferredSize(getSize());

setVisible(true);

}}
Output:-

Q.2) Write a java program to display following output.

Program:-

import java.awt.*;

public class GridBagLayoutE2 extends Frame {

GridBagLayoutE2() {

setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.fill = GridBagConstraints.HORIZONTAL;

Label l1 = new Label("Name:");

gbc.gridx = 0;

gbc.gridy = 0;

add(l1, gbc);

TextField tf = new TextField();

gbc.gridx = 1;

gbc.gridy = 0;
gbc.gridwidth = 2;

add(tf, gbc);

Label l2 = new Label("Comments:");

gbc.gridx = 0;

gbc.gridy = 1;

gbc.gridwidth = 1;

add(l2, gbc);

TextArea t1 = new TextArea(6, 20);

gbc.gridx = 1;

gbc.gridy = 1;

gbc.gridwidth = 2;

add(t1, gbc);

Button b1 = new Button("Submit");


gbc.gridx = 1;

gbc.gridy = 2;

gbc.gridwidth = 1;

add(b1, gbc);

setTitle("GridBagLayout Demo");

setSize(400, 300);

setVisible(true);

public static void main(String args[]) {

new GridBagLayoutE2();

}
}

Output:-

You might also like