0% found this document useful (0 votes)
244 views

Use of Cardlayout To Write A Two-Level Card Deck That Operating System

This document provides code to create a two-level card layout that allows a user to select an operating system. The code uses CardLayout to display buttons for different operating systems and navigates between them when the buttons are clicked.

Uploaded by

Samreen Chowlkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views

Use of Cardlayout To Write A Two-Level Card Deck That Operating System

This document provides code to create a two-level card layout that allows a user to select an operating system. The code uses CardLayout to display buttons for different operating systems and navigates between them when the buttons are clicked.

Uploaded by

Samreen Chowlkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Practical No.

4
Use of CardLayout to write a
program to create a
two-level card deck that

i
allows the user to select an

sar
operating system.
An
ira
un
.M
Ms
X. Program Code: Teacher must assign a separate program statement to group of 3-4
students.
Execute the following Program and write the output.
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
public class EX4Q2 extends JFrame implements ActionListener
{
CardLayout card;
JButton b1,b2,b3;
Container c;
EX4Q2()
{
c=getContentPane();

i
card=new CardLayout(40,30);

sar
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);
An
ira
}
public void actionPerformed(ActionEvent e)
{
card.next(c);
un

}
public static void main(String args[])
{
EX4Q2 c1=new EX4Q2();
.M

c1.setSize(400,400);
c1.setVisible(true);
c1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Ms

OUTPUT:
XIII. Exercise
1. Write Java program to display following output.

i
sar
CODE:
importjava.awt.*;
public class EX4Q1 extends Frame
An
ira
{
Button b1,b2,b3,b4,b5;
un

GridBagConstraints c;
EX4Q1()
.M

{
setLayout(new GridBagLayout());
c= new GridBagConstraints();
Ms

b1= new Button("Button One");


b2= new Button("Button Two");
b3= new Button("Button Three");
b4= new Button("Button Four");
b5= new Button("Button Five");
c.ipadx=70;
c.ipady=70;
c.fill=GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=0;
add(b1,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.gridx=1;
c.gridy=0;
add(b2,c);

i
sar
c.fill=GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=1;
add(b3,c);
An
c.fill=GridBagConstraints.HORIZONTAL;
ira
c.gridx=1;
c.gridy=1;
un

add(b4,c);
c.fill=GridBagConstraints.HORIZONTAL;
.M

c.gridx=0;
c.gridy=2;
c.gridwidth=2;
Ms

add(b5,c);
setVisible(true);
}
public static void main(String args[])
{
new EX4Q1();
}
}

2. Write Java Program to display following output.

i
sar
An
ira
un

CODE:
importjava.awt.*;
.M

public class EX4Q3 extends Frame


{
Ms

Button b1;
Label l1,l2;
TextField t1;
TextArea ta;
GridBagConstraints c;
EX4Q3()
{
l1=new Label("Name");
l2=new Label("Component");
t1=new TextField(5);
ta=new TextArea(10,16);
b1=new Button("Submit");
setLayout(new GridBagLayout());
c=new GridBagConstraints();
add(l1,c,0,0,1,1,0,0);
add(t1,c,1,0,1,1,0,20);

i
sar
add(l2,c,0,1,1,1,0,0);
add(ta,c,1,1,1,1,0,60);
add(b1,c,0,2,2,1,0,20);

}
setVisible(true);
An
ira
void add(Component
comp,GridBagConstraintsc,intx,inty,intw,inth,intwx,intwy)
{
un

c.gridx=x;
c.gridy=y;
.M

c.gridwidth=w;
c.gridheight=h;
Ms

c.weightx=wx;
c.weighty=wy;
add(comp,c);

}
public static void main(String args[])
{
new EX4Q3();
}
}

i
sar
Extrs’s:
An
ira
1. Write Java Program to display following output.
un
.M
Ms

CODE:
importjava.awt.*;
public class EX4Q4 extends Frame
{
Button b1,b2,b3;
GridBagConstraints c;
EX4Q4()
{
setLayout(new GridBagLayout());
c= new GridBagConstraints();
b1= new Button("b1");
b2= new Button("b2");
b3= new Button("b3");
c.ipadx=20;
c.ipady=20;

i
sar
c.fill=GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=0;
c.gridwidth=2;
add(b1,c);
An
ira
c.gridwidth=1;
c.fill=GridBagConstraints.HORIZONTAL;
un

c.gridx=0;
c.gridy=1;
.M

add(b2,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.gridx=1;
Ms

c.gridy=1;
add(b3,c);
setVisible(true);
}
public static void main(String args[])
{
new EX4Q4();
}
}

2. Develop a program to demonstrate Mix Layout


CODE:
importjava.awt.*;

public class EX4Q5 extends Frame


{
Panel p1,p2,p3,p4,p5;
Button b1,b2,b3,b4,b5,b6;

i
Label l1,l2,l3,l4;

sar
EX4Q5()
{
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
p5=new Panel();
b1=new Button("Button 1");
An
ira
b2=new Button("Button 2");
b3=new Button("Button 3");
b4=new Button("Button 4");
b5=new Button("Button 5");
un

b6=new Button("Button 6");


l1=new Label("Label 1");
l2=new Label("Label 2");
.M

l3=new Label("Label 3");


l4=new Label("Label 4");
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(b1);
Ms

p1.add(b2);
p1.add(b3);

p2.setLayout(new FlowLayout(FlowLayout.CENTER));
p2.add(l1);

p4.setLayout(new GridLayout(1,3,5,5));
p4.add(l2);
p4.add(l3);
p4.add(l4);

p5.setLayout(new GridLayout(3,1,5,5));
p5.add(b4);
p5.add(b5);
p5.add(b6);

p3.setLayout(new GridLayout(2,1));
p3.add(p4);
p3.add(p5);

this.setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
add(p3,BorderLayout.CENTER);
setVisible(true);
}

i
public static void main(String args[])

sar
{
new EX4Q5();
}
}
OUTPUT:
An
ira
un
.M
Ms

You might also like