AJP Prac4-6
AJP Prac4-6
Aim:
Write a program to create two-level card desk that allow the user to
select component of panel using CardLayout
Theory:
1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout with the given
horizontal and vertical gap.
Commonly used methods of CardLayout class:
1. public void next (Container parent): is used to flip to the next card of
the given container
2. public void previous (Container parent): is used to flip to the previous
card of the given container.
3. public void first (Container parent): is used to flip to the first card of
the given container.
4. public void last (Container parent): is used to flip to the last card of
the given container.
5. public void show (Container parent, String name): is used to flip to
the specified card with the given name
Program Code:
import java.awt.*;
import
java.awt.event.*;
import
javax.swing.JFrame;
import javax.swing.*;
CardLayoutDemo()
{
c = getContentPane();
card = new CardLayout(40,
30); c.setLayout(card);
b1.addActionListener(t
his);
b2.addActionListener(t
his);
c.add("a", b1);
c.add("b", b2);
}
cl.setSize(400,
400);
cl.setVisible(true)
;
}
}
Output:
Aim:
Menu Bar
Instantiate a MenuBar.
Create Menu objects (like "File", "Edit", etc.).
Add Menu objects to the MenuBar.
Set the MenuBar to your Frame or Dialog.
Menu
Creating a Menu:
Menu Item
Instantiate a MenuItem.
Optionally, set properties like labels or shortcuts.
Add an ActionListener to handle actions.
Program Code:
import java.awt.*;
public class
MenuDialog
{
public static void main(String[] args)
{
Frame f=new Frame("Welcome in Practical
5"); MenuBar mBar = new MenuBar();
File.add(New)
New.add(i9);
New.add(i10);
File.add(i2);
File.add(i3);
File.add(i4);
mBar.add(File);
Edit.add(i5);
Edit.add(i6);
Edit.add(i7);
Edit.add(i8);
View.add(Zoom);
Zoom.add(i14);
Zoom.add(i15);
View.add(i12);
View.add(i13);
mBar.add(Edit);
mBar.add(View);
f.setMenuBar(mBar);
f.setVisible(true);
f.setSize(400,400);
f.setLayout(null);
}
Output:
Aim:
JComboBox
Key Points:
JScrollPane:
Primary Function:
import java.awt.*;
import
javax.swing.*;
public class Practical6 extends JFrame
{
public Practical6()
{
Container ct=
getContentPane();
ct.setLayout(null);
JLabel jl = new JLabel("Select Branch :");
JComboBox<String> jc = new
JComboBox<>();
jc.addItem("Computer Engineering");
jc.addItem("Mechanical Engineering");
jc.addItem("Civil Engineering");
jc.addItem("Electronics & Tele.
Engineering"); jc.addItem("Electrical
Engineering"); jc.addItem("Chemical
Engineering"); ct.add(jl);
ct.add(jc);
jl.setBounds(60,50,100,30);
jc.setBounds(170,50,200,30);
}
public static void main(String ar[])
{
Practical6 fr = new
Practical6();
fr.setTitle("Practical 6");
fr.setSize(300,400);
fr.setVisible(true);
}
}
Output: