0% found this document useful (0 votes)
13 views11 pages

AJP Prac4-6

Uploaded by

mayurkove428
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)
13 views11 pages

AJP Prac4-6

Uploaded by

mayurkove428
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/ 11

Practical 4

Aim:

Write a program to create two-level card desk that allow the user to
select component of panel using CardLayout
Theory:

The CardLayout class manages the components in such a manner that


only one component is visible at a time. It treats each component as a
card hence known as CardLayout.

Constructors of CardLayout class :

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.*;

public class CardLayoutDemo extends JFrame implements

ActionListener { CardLayout card;


JButton b1, b2,
b3;
Container c;

CardLayoutDemo()
{
c = getContentPane();
card = new CardLayout(40,
30); c.setLayout(card);

b1 = new JButton("Welcome in First Level");


b2 = new JButton("Welcome in Second Level");

b1.addActionListener(t
his);
b2.addActionListener(t
his);

c.add("a", b1);
c.add("b", b2);
}

public void actionPerformed(ActionEvent e)


{
card.next(c);
}

public static void main(String[] args)


{
CardLayoutDemo cl = new CardLayoutDemo();

cl.setSize(400,
400);
cl.setVisible(true)
;

}
}
Output:

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 5

Aim:

Write a program using AWT to create a menubar where menubar


contains menu items such as File, Edit, View and create a submenu
under the File Menu: New and Open.
Theory:

 Menu Bar

In AWT, a MenuBar is used to hold one or more Menu objects, and it is


typically added to a Frame or Dialog.

Creating a 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

A Menu is a drop-down list that can contain multiple MenuItem objects. It


is added to a MenuBar and represents a category of commands or
options.

Creating a Menu:

 Instantiate a Menu with a name.


 Add MenuItem objects to the Menu.

 Menu Item

A MenuItem represents a single option within a Menu. It can be a simple


menu item or a check box item.

Creating a 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();

Menu File = new Menu("File");


Menu Edit = new Menu("Edit");
Menu View = new Menu("View");
Menu New = new Menu("New");
Menu Zoom = new
Menu("Zoom");
MenuItem i2 = new
MenuItem("OpenCtrl+O"); MenuItem i3 =
new MenuItem("SaveCtrl+S"); MenuItem i4 =
new MenuItem("Exit"); MenuItem i5 = new
MenuItem("UndoCtrl+Z"); MenuItem i6 = new
MenuItem("CutCtrl+X"); MenuItem i7 = new
MenuItem("CopyCtrl+C"); MenuItem i8 = new
MenuItem("PasteCtrl+V");
MenuItem i9 = new MenuItem("New TabCtrl+N");
MenuItem i10 = new MenuItem("New WindowCtrl+Shift+N");
MenuItem i12 = new MenuItem("Status Bar");
MenuItem i13 = new MenuItem("Word wrap");
MenuItem i14 = new MenuItem("Zoom In");
MenuItem i15 = new MenuItem("Zoom Out");

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:

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 6

Aim:

Write a program using swing to display a scrollpane and JCombobox in an


Japplet with the items: English, Marathi, Hindi, Sanskrit.
Theory:

 JComboBox

 JComboBox is a component that provides a drop-down list of


items for the user to choose from. It’s useful for selecting from a
limited set of options.

Key Points:

 Model: JComboBox uses a model to manage its list of items. The


default model is DefaultComboBoxModel, but you can use custom
models if needed.

 Editable vs. Non-Editable: By default, a JComboBox is non-


editable, meaning users can only select from the list. You can
make it editable by calling setEditable(true), allowing users to
type their own entries.

 JScrollPane:

JScrollPane is a key component in Java Swing used to provide scrolling


capabilities for other components when their content exceeds the visible
area.

Primary Function:

JScrollPane allows for the inclusion of a scrollable view for another


component, making it useful when the content is too large to fit within a
fixed-size area.
Program Code:

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:

Marks Obtained Dated Signature


of Teacher

Process Product Total (50)


Related(15) Related(35)

You might also like