SlideShare a Scribd company logo
Pune Vidyarthi Griha’s
COLLEGE OF ENGINEERING, NASHIK – 3.
“ Core Java”
By
Prof. Anand N. Gharu
(Assistant Professor)
PVGCOE, (Computer Dept.) NASIK-4
30th June 2017
Computer Dept.
Agenda
1. Abstract Windowing Toolkit (AWT)
2. Event Handling
3. Swing
4. Layout Manager
5. Applet
2
Abstract Windowing Toolkit (AWT)
 Abstract Windowing Toolkit (AWT) is used for GUI programming in java.
 AWT Container Hierarchy:
3
Container:
 The Container is a component in AWT that can contain another components like
buttons, textfields, labels etc. The classes that extends Container class are known
as container.
Window:
 The window is the container that have no borders and menubars. You must use
frame, dialog or another window for creating a window.
Panel:
 The Panel is the container that doesn't contain title bar and MenuBars. It can have
other components like button, textfield etc.
Frame:
 The Frame is the container that contain title bar and can have MenuBars. It can
have other components like button, textfield etc.
4
Creating a Frame
Commonly used Methods of Component class:
 1)public void add(Component c)
 2)public void setSize(int width,int height)
 3)public void setLayout(LayoutManager m)
 4)public void setVisible(boolean)
Creating a Frame:
There are two ways to create a frame:
 By extending Frame class (inheritance)
 By creating the object of Frame class (association)
5
Creating a Frame: Set Button
Simple example of AWT by inheritance:
import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout now bydefault BorderLayout
setVisible(true);//now frame willbe visible, bydefault not visible
} public static void main(String args[]){ First f=new First(); } }
6
Button
 public void setBounds(int xaxis, int yaxis, int width, int height); have
been used in the above example that sets the position of the button.
7
Event and Listener (Event Handling)
 Changing the state of an object is known as an event. For example, click
on button, dragging mouse etc. The java.awt.event package provides
many event classes and Listener interfaces for event handling.
 Event classes and Listener interfaces
Event Classes Listener Interfaces
 ActionEvent ActionListener
 MouseEvent MouseListener
MouseMotionListener
 MouseWheelEvent MouseWheelListener
 KeyEvent KeyListener
 ItemEvent ItemListener
8
Event classes and Listener interfaces
Event Classes Listener Interfaces
 TextEvent TextListener
 AdjustmentEvent AdjustmentListener
 WindowEvent WindowListener
 ComponentEvent ComponentListener
 ContainerEvent ContainerListener
 FocusEvent FocusListener
Steps to perform EventHandling:
 Implement the Listener interface and overrides its methods
 Register the component with the Listener
9
Steps to perform EventHandling
 For registering the component with the Listener, many classes provide methods. F
 Button
public void addActionListener(ActionListener a){}
 MenuItem
public void addActionListener(ActionListener a){}
 TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
 TextArea
public void addTextListener(TextListener a){}
 Checkbox
public void addItemListener(ItemListener a){}
10
EventHandling Codes
 Choice
public void addItemListener(ItemListener a){}
 List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
EventHandling Codes:
 We can put the event handling code into one of the following places:
 Same class
 Other class
 Annonymous class
11
Example of event handling within class
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){ tf=new TextField(); tf.setBounds(60,50,170,20);
Button b=new Button("click me"); b.setBounds(100,120,80,30);
b.addActionListener(this); add(b);add(tf); setSize(300,300); setLayout(null);
setVisible(true); }
public void actionPerformed(ActionEvent e){ tf.setText("Welcome"); }
public static void main(String args[]){
new AEvent();
} }
12
Output
 public void setBounds(int xaxis, int yaxis, int width, int height); have been used in
the above example that sets the position of the component it may be button,
textfield etc.
13
Example of event handling by Outer class
import java.awt.*;
import java.awt.event.*;
class AEvent2 extends Frame implements ActionListener{
TextField tf;
AEvent2(){ tf=new TextField(); tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30); b.addActio nListener(this);
add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); }
public void actionPerformed(ActionEvent e){
tf.setText("Welcome"); }
public static void main(String args[]){ new AEvent2(); } }
14
Outer class
import java.awt.event.*;
class Outer implements ActionListener{
AEvent2 obj;
Outer(AEvent2 obj){
this.obj=obj;
}
public void actionPerformed(ActionEvent e){
obj.tf.setText("welcome");
} }
15
Example of event handling by Annonymous
class
import java.awt.*;
import java.awt.event.*;
class AEvent3 extends Frame{ TextField tf; AEvent3(){
tf=new TextField(); tf.setBounds(60,50,170,20);
Button b=new Button("click me"); b.setBounds(50,120,80,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(){ tf.setText("hello"); } });
add(b);
add(tf); setSize(300,300); setLayout(null); setVisible(true); }
public static void main(String args[])
{
new AEvent3(); } }
16
Swing (Graphics Programming in java)
 Swing is a part of JFC (Java Foundation Classes) that is used to create GUI
application. It is built on the top of Swing is a part of JFC (Java Foundation Classes)
that is used to create GUI application. It is built on the top of AWT and entirely
written in java.
Advantage of Swing over AWT:
 There are many advantages of Swing over AWT. They are as follows:
 Swing components are Plateform independent & It is lightweight.
 It supports pluggable look and feel.
 It has more powerful componenets like tables, lists, scroll panes, color chooser,
tabbed pane etc.
 It follows MVC (Model View Controller) architecture.
 What is JFC ? - The Java Foundation Classes (JFC) are a set of GUI components
which simplify the development of desktop applications.
17
Hierarchy of swing
18
Commonly used Methods of Component class
Commonly used Methods of Component class:
 1)public void add(Component c)
 2)public void setSize(int width,int height)
 3)public void setLayout(LayoutManager m)
 4)public void setVisible(boolean)
Creating a Frame:
 There are two ways to create a frame:
 By creating the object of Frame class (association)
 By extending Frame class (inheritance)
19
Simple example of Swing by Association
import javax.swing.*;
public class Simple {
JFrame f;
Simple(){
f=new JFrame();
JButton b=new JButton("click");
b.setBounds(130,100,100, 40);
f.add(b); f.setSize(400,500);
f.setLayout(null); f.setVisible(true); }
public static void main(String[] args) {
new Simple(); } }
20
Output
 public void setBounds(int xaxis, int yaxis, int width, int height); have
been used in the above example that sets the position of the button.
21
Button class
 The JButton class is used to create a button that have plateform-independent
implementation.
Commonly used Constructors:
 JButton(): creates a button with no text and icon.
 JButton(String s): creates a button with the specified text.
 JButton(Icon i): creates a button with the specified icon object.
Commonly used Methods of AbstractButton class:
 1) public void setText(String s): is used to set specified text on button.
 2) public String getText(): is used to return the text of the button.
 3) public void setEnabled(boolean b): is used to enable or disable the button.
 4) public void setIcon(Icon b): is used to set the specified Icon on the button.
 5) public Icon getIcon(): is used to get the Icon of the button.
 6) public void setMnemonic(int a): is used to set the mnemonic on the button.
22
Example of displaying image on the button
7) public void addActionListener(ActionListener a): is used to add the action listener
to this object.
Note: The JButton class extends AbstractButton class.
import java.awt.event.*;
import javax.swing.*;
public class ImageButton{
ImageButton(){
JFrame f=new JFrame();
JButton b=new JButton(new ImageIcon("b.jpg"));
b.setBounds(130,100,100, 40); f.add(b); f.setSize(300,400); f.setLayout(null);
f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public static void main(String[] args) { new ImageButton(); } }
23
JRadioButton class
 The JRadioButton class is used to create a radio button.
Commonly used Constructors of JRadioButton class:
 JRadioButton(): creates an unselected radio button with no text.
 JRadioButton(String s): creates an unselected radio button with specified text.
 JRadioButton(String s, boolean selected): creates a radio button with the
specified text and selected status.
Commonly used Methods of AbstractButton class:
 1) public void setText(String s): is used to set specified text on button.
 2) public String getText(): is used to return the text of the button.
 3) public void setEnabled(boolean b): is used to enable or disable the button.
 4) public void setIcon(Icon b): is used to set the specified Icon on the button.
 5) public Icon getIcon(): is used to get the Icon of the button.
 6) public void setMnemonic(int a): is used to set the mnemonic on the button.
24
Example of JRadioButton class
7) public void addActionListener(ActionListener a): is used to add the action listener to this
object. // Note: The JRadioButton class extends the JToggleButton class that extends
AbstractButton class.
import javax.swing.*;
public class Radio { JFrame f;
Radio(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) FeMale");
r1.setBounds(50,100,70,30); r2.setBounds(50,150,70,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2); f.add(r1);f.add(r2);
f.setSize(300,300); f.setLayout(null); f.setVisible(true); }
public static void main(String[] args) { new Radio(); } }
25
JTextArea class
 The JTextArea class is used to create a text area. It is a multiline area that displays
the plain text only.
Commonly used Constructors:
 JTextArea(): creates a text area that displays no text initally.
 JTextArea(String s): creates a text area that displays specified text initally.
 JTextArea(int row, int column): creates a text area with the specified number of
rows and columns that displays no text initally..
 JTextArea(String s, int row, int column): creates a text area with the specified
number of rows and columns that displays specified text.
Commonly used methods of JTextArea class:
 1) public void setRows(int rows): is used to set specified number of rows.
 2) public void setColumns(int cols):: is used to set specified number of columns.
26
Example of JTextField class
3) public void setFont(Font f): is used to set the specified font.
4) public void insert(String s, int position): is used to insert the specified text on the
specified position.
5) public void append(String s): to append the given text to the end of the document.
import java.awt.Color;
import javax.swing.*;
public class TArea { JTextArea area; JFrame f;
TArea(){ f=new JFrame();
area=new JTextArea(300,300); area.setBounds(10,30,300,300);
area.setBackground(Color.black); area.setForeground(Color.white);
f.add(area); f.setSize(400,400); f.setLayout(null); f.setVisible(true); }
public static void main(String[] args) { new TArea(); } }
27
JComboBox class
 The JComboBox class is used to create the combobox (drop-down list). At a time
only one item can be selected from the item list.
Commonly used Constructors of JComboBox class:
 JComboBox()
 JComboBox(Object[] items)
 JComboBox(Vector<?> items)
Commonly used methods of JComboBox class:
 1) public void addItem(Object anObject): is used to add an item to the item list.
 2) public void removeItem(Object anObject): is used to delete an item to the item
list.
 3) public void removeAllItems(): is used to remove all the items from the list.
 4) public void setEditable(boolean b): is used to determine whether the
JComboBox is editable.
28
Example of JComboBox class
5) public void addActionListener(ActionListener a): is used to add the ActionListener.
6) public void addItemListener(ItemListener i): is used to add the ItemListener.
import javax.swing.*;
public class Combo { JFrame f;
Combo(){
f=new JFrame("Combo ex");
String country[]={"India","Aus","U.S.A","England","Newzeland"};
JComboBox cb=new JComboBox(country);
cb.setBounds(50, 50,90,20); f.add(cb); f.setLayout(null); f.setSize(400,500);
f.setVisible(true); }
public static void main(String[] args) {
new Combo(); } }
29
JTable class
The JTable class is used to display the data on two dimentional tables of cells.
Commonly used Constructors of JTable class:
• JTable(): creates a table with empty cells.
• JTable(Object[][] rows, Object[] columns): creates a table with the specified data.
Example of JTable class:
import javax.swing.*;
public class MyTable { JFrame f; MyTable(){ f=new JFrame();
String data[][]={ {"101","Amit","670000"},
{"102","Jai","780000"},
{"101","Sachin","700000"}};
String column[]={"ID","NAME","SALARY"};
JTable jt=new JTable(data,column);
30
Example
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
f.add(sp);
f.setSize(300,400);
// f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new MyTable();
}
}
31
JColorChooser class
 The JColorChooser class is used to create a color chooser dialog box so that user
can select any color.
Commonly used Constructors of JColorChooser class:
 JColorChooser(): is used to create a color chooser pane with white color initially.
 JColorChooser(Color initialColor): is used to create a color chooser pane with the
specified color initially.
Commonly used methods of JColorChooser class:
 public static Color showDialog(Component c, String title, Color initialColor): is
used to show the color-chooser dialog box.
32
Example of JColorChooser class
33
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class JColorChooserExample extends JFrame implements ActionListener{
JButton b; Container c;
JColorChooserExample(){ c=getContentPane(); c.setLayout(new FlowLayout());
b=new JButton("color"); b.addActionListener(this); c.add(b); }
public void actionPerformed(ActionEvent e) {
Color initialcolor=Color.RED;
Color color=JColorChooser.showDialog(this,"Select a color",initialcolor);
c.setBackground(color);
}
34
Example of JColorChooser class
public static void main(String[] args) {
JColorChooserExample ch=new JColorChooserExample();
ch.setSize(400,400);
ch.setVisible(true);
ch.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
35
JProgressBar class
 The JProgressBar class is used to display the progress of the task.
Commonly used Constructors of JProgressBar class:
 JProgressBar(): is used to create a horizontal progress bar but no string
text.
 JProgressBar(int min, int max): is used to create a horizontal progress bar
with the specified minimum and maximum value.
 JProgressBar(int orient): is used to create a progress bar with the
specified orientation, it can be either Vertical or Horizontal by using
SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants.
 JProgressBar(int orient, int min, int max): is used to create a progress bar
with the specified orientation, minimum and maximum value.
36
Example of JProgressBar class
Commonly used methods of JProgressBar class:
1) public void setStringPainted(boolean b): is used to determine whether string
should be displayed.
2) public void setString(String s): is used to set value to the progress string.
3) public void setOrientation(int orientation): is used to set the orientation, it may be
either vertical or horizontal by using SwingConstants.VERTICAL and
SwingConstants.HORIZONTAL constants..
4) public void setValue(int value): is used to set the current value on the progress bar.
import javax.swing.*;
public class MyProgress extends JFrame{
JProgressBar jb; int i=0,num=0;
MyProgress(){ jb=new JProgressBar(0,2000); jb.setBounds(40,40,200,30);
37
Example of JProgressBar class
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(400,400); setLayout(null); }
public void iterate(){
while(i<=2000){
jb.setValue(i);
i=i+20;
try{Thread.sleep(150);}catch(Exception e){} } }
public static void main(String[] args) {
MyProgress m=new MyProgress();
m.setVisible(true); m.iterate(); } }
38
JSlider class
 The JSlider is used to create the slider. By using JSlider a user can select a value from
a specific range.
Commonly used Constructors of JSlider class:
 JSlider(): creates a slider with the initial value of 50 and range of 0 to 100.
 JSlider(int orientation): creates a slider with the specified orientaion set by
either JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to 100 and
intial value 50.
 JSlider(int min, int max): creates a horizontal slider using the given min and
max.
 JSlider(int min, int max, int value): creates a horizontal slider using the
given min, max and value.
39
JSlider class
JSlider(int orientation, int min, int max, int value): creates a slider using the given
orientation, min, max and value.
Commonly used Methods of JSlider class:
 1) public void setMinorTickSpacing(int n): is used to set the minor tick spacing
to the slider.
 2) public void setMajorTickSpacing(int n): is used to set the major tick spacing
to the slider.
 3) public void setPaintTicks(boolean b): is used to determine whether tick
marks are painted.
 4) public void setPaintLabels(boolean b): is used to determine whether labels
are painted.
 5) public void setPaintTracks(boolean b): is used to determine whether track is painted.
40
Simple example of JSlider class
import javax.swing.*;
public class SliderExample1 extends JFrame{
public SliderExample1() {
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
JPanel panel=new JPanel();
panel.add(slider);
add(panel); }
public static void main(String s[]) {
SliderExample1 frame=new SliderExample1();
frame.pack();
frame.setVisible(true);
} }
41
Example of JSlider class that paints ticks
import javax.swing.*;
public class SliderExample extends JFrame{
public SliderExample() {
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
slider.setMinorTickSpacing(2);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true); slider.setPaintLabels(true);
JPanel panel=new JPanel();
panel.add(slider); add(panel); }
public static void main(String s[]) {
SliderExample frame=new SliderExample();
frame.pack(); frame.setVisible(true); } }
42
BorderLayout (LayoutManagers)
The LayoutManagers are used to arrange components in a particular manner.
LayoutManager is an interface that is implemented by all the classes of layout
managers. There are following classes that represents the layout managers:
 java.awt.BorderLayout
 java.awt.FlowLayout
 java.awt.GridLayout
 java.awt.CardLayout
 java.awt.GridBagLayout
 javax.swing.BoxLayout
 javax.swing.GroupLayout
 javax.swing.ScrollPaneLayout
 javax.swing.SpringLayout etc.
43
BorderLayout
 The BorderLayout is used to arrange the components in five regions: north, south,
east, west and center. Each region (area) may contain one component only. It is
the default layout of frame or window. The BorderLayout provides five constants
for each region:
 1. public static final int NORTH
 2. public static final int SOUTH
 3. public static final int EAST
 4. public static final int WEST
 5. public static final int CENTER
Constructors of BorderLayout class:
 BorderLayout(): creates a border layout but with no gaps between the
components. JBorderLayout(int hgap, int vgap): creates a border layout with the
given horizontal and vertical gaps between the components.
44
Example of BorderLayout class
45
Example of BorderLayout class
import java.awt.*;
import javax.swing.*;
public class Border { JFrame f; Border(){ f=new JFrame();
JButton b1=new JButton("NORTH");; JButton b2=new JButton("SOUTH");;
JButton b3=new JButton("EAST");; JButton b4=new JButton("WEST");;
JButton b5=new JButton("CENTER");;
f.add(b1,BorderLayout.NORTH); f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST); f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER); f.setSize(300,300);
f.setVisible(true); }
public static void main(String[] args) { new Border(); } }
46
GridLayout
 The GridLayout is used to arrange the components in rectangular grid.
One component is dispalyed in each rectangle.
 Constructors of GridLayout class:
 GridLayout(): creates a grid layout with one column per component in a
row.
 GridLayout(int rows, int columns): creates a grid layout with the given
rows and columns but no gaps between the components.
 GridLayout(int rows, int columns, int hgap, int vgap): creates a grid
layout with the given rows and columns alongwith given horizontal and
vertical gaps.
47
Example of GridLayout class
48
Example of GridLayout class
import java.awt.*;
import javax.swing.*;
public class MyGridLayout{ JFrame f; MyGridLayout(){ f=new JFrame();
JButton b1=new JButton("1"); JButton b2=new JButton("2");
JButton b3=new JButton("3"); JButton b4=new JButton("4");
JButton b5=new JButton("5"); JButton b6=new JButton("6");
JButton b7=new JButton("7"); JButton b8=new JButton("8");
JButton b9=new JButton("9");
f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5); f.add(b6); f.add(b7
f.add(b8); f.add(b9); f.setLayout(new GridLayout(3,3));
f.setSize(300,300); f.setVisible(true); } //set grid layout of 3 rows and 3 columns
public static void main(String[] args) { new MyGridLayout(); } }
49
FlowLayout
 The FlowLayout is used to arrange the components in a line, one after another (in a flow). It
is the default layout of applet or panel.
Fields of FlowLayout class:
 1. public static final int LEFT
 2. public static final int RIGHT
 3. public static final int CENTER
 4. public static final int LEADING
 5. public static final int TRAILING
Constructors of FlowLayout class:
 FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal
and vertical gap.
 FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit
horizontal and vertical gap.
 FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and
the given horizontal and vertical gap.
50
Example of FlowLayout class
51
Example
import java.awt.*;
import javax.swing.*;
public class MyFlowLayout{ JFrame f; MyFlowLayout(){ f=new JFrame();
JButton b1=new JButton("1"); JButton b2=new JButton("2");
JButton b3=new JButton("3"); JButton b4=new JButton("4");
JButton b5=new JButton("5");
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//setting flow layout of right alignment
f.setSize(300,300); f.setVisible(true); }
public static void main(String[] args) { new MyFlowLayout(); } }
52
BoxLayout class
 The BoxLayout is used to arrange the components either vertically or horizontally.
For this purpose, BoxLayout provides four constants. They are as follows:
 Note: BoxLayout class is found in javax.swing package.
Fields of BoxLayout class:
 1. public static final int X_AXIS
 2. public static final int Y_AXIS
 3. public static final int LINE_AXIS
 4. public static final int PAGE_AXIS
Constructor of BoxLayout class:
 1. BoxLayout(Container c, int axis): creates a box layout that arranges the
components with the given axis.
53
Example of BoxLayout class with Y-AXIS
54
Example of BoxLayout class with Y-AXIS
import java.awt.*;
import javax.swing.*;
public class BoxLayoutExample1 extends Frame { Button buttons[];
public BoxLayoutExample1 () {
buttons = new Button [5];
for (int i = 0;i<5;i++) {
buttons[i] = new Button ("Button " + (i + 1));
add (buttons[i]); }
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(400,400); setVisible(true); }
public static void main(String args[]){ BoxLayoutExample1 b=new
BoxLayoutExample1(); } }
55
Example of BoxLayout class with X-AXIS
56
Example of BoxLayout class with X-AXIS
import java.awt.*;
import javax.swing.*;
public class BoxLayoutExample2 extends Frame { Button buttons[];
public BoxLayoutExample2() {
buttons = new Button [5];
for (int i = 0;i<5;i++) {
buttons[i] = new Button ("Button " + (i + 1));
add (buttons[i]); }
setLayout (new BoxLayout(this, BoxLayout.X_AXIS));
setSize(400,400); setVisible(true); }
public static void main(String args[]){ BoxLayoutExample2 b=new
BoxLayoutExample2(); } }
57
CardLayout class
 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 that is why it is
known as CardLayout.
Constructors of CardLayout class:
 CardLayout(): creates a card layout with zero horizontal and vertical gap.
 CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and
vertical gap.
 Commonly used methods of CardLayout class:
 public void next(Container parent): is used to flip to the next card of the given
container.
 public void previous(Container parent): is used to flip to the previous card of the
given container.
58
CardLayout class
 public void first(Container parent): is used to flip to the first card of the given
container.
 public void last(Container parent): is used to flip to the last card of the given
container.
 public void show(Container parent, String name): is used to flip to the specified
card with the given name.
59
Example of CardLayout class
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);
60
Example of CardLayout class
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);
} }
61
Questions ?
Feel Free to Contact: gharu.anand@gmail.com
62
Ad

More Related Content

What's hot (18)

Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
Shalabh Chaudhary
 
Migrating to JUnit 5
Migrating to JUnit 5Migrating to JUnit 5
Migrating to JUnit 5
Rafael Winterhalter
 
Java nio ( new io )
Java nio ( new io )Java nio ( new io )
Java nio ( new io )
Jemin Patel
 
Linux basics
Linux basicsLinux basics
Linux basics
sirmanohar
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
Aram Mohammed
 
Java Programming - 03 java control flow
Java Programming - 03 java control flowJava Programming - 03 java control flow
Java Programming - 03 java control flow
Danairat Thanabodithammachari
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
arnold 7490
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
Soumya Behera
 
Biopython
BiopythonBiopython
Biopython
Karin Lagesen
 
Io streams
Io streamsIo streams
Io streams
Elizabeth alexander
 
NIO and NIO2
NIO and NIO2NIO and NIO2
NIO and NIO2
Balamurugan Soundararajan
 
Java Threads
Java ThreadsJava Threads
Java Threads
Hamid Ghorbani
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntu
Khurshid Asghar
 
Unit testing concurrent code
Unit testing concurrent codeUnit testing concurrent code
Unit testing concurrent code
Rafael Winterhalter
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
Upender Upr
 

Similar to CORE JAVA-2 (20)

Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
swapnac12
 
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKDPPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
chessvashisth
 
it's about the swing programs in java language
it's about the swing programs in  java languageit's about the swing programs in  java language
it's about the swing programs in java language
arunkumarg271
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVERUnit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
javaprogramming framework-ppt frame.pptx
javaprogramming framework-ppt frame.pptxjavaprogramming framework-ppt frame.pptx
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
shanmuga rajan
 
Java awt
Java awtJava awt
Java awt
Arati Gadgil
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Java
suraj pandey
 
Java_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.pptJava_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
kirupasuchi1996
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
Creating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interfaceCreating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
Java swing
Java swingJava swing
Java swing
ssuser3a47cb
 
introduction to JAVA awt programmin .ppt
introduction to JAVA awt programmin .pptintroduction to JAVA awt programmin .ppt
introduction to JAVA awt programmin .ppt
bgvthm
 
Lecture8 oopj
Lecture8 oopjLecture8 oopj
Lecture8 oopj
Dhairya Joshi
 
Unit 1- awt(Abstract Window Toolkit) .ppt
Unit 1- awt(Abstract Window Toolkit) .pptUnit 1- awt(Abstract Window Toolkit) .ppt
Unit 1- awt(Abstract Window Toolkit) .ppt
Deepgaichor1
 
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.pptfdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.pptawdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
1.Abstract windowing toolkit.ppt of AJP sub
1.Abstract windowing toolkit.ppt of AJP sub1.Abstract windowing toolkit.ppt of AJP sub
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
swapnac12
 
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKDPPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
chessvashisth
 
it's about the swing programs in java language
it's about the swing programs in  java languageit's about the swing programs in  java language
it's about the swing programs in java language
arunkumarg271
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVERUnit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
javaprogramming framework-ppt frame.pptx
javaprogramming framework-ppt frame.pptxjavaprogramming framework-ppt frame.pptx
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Java
suraj pandey
 
Java_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.pptJava_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
Creating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interfaceCreating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
introduction to JAVA awt programmin .ppt
introduction to JAVA awt programmin .pptintroduction to JAVA awt programmin .ppt
introduction to JAVA awt programmin .ppt
bgvthm
 
Unit 1- awt(Abstract Window Toolkit) .ppt
Unit 1- awt(Abstract Window Toolkit) .pptUnit 1- awt(Abstract Window Toolkit) .ppt
Unit 1- awt(Abstract Window Toolkit) .ppt
Deepgaichor1
 
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.pptfdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.pptawdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
1.Abstract windowing toolkit.ppt of AJP sub
1.Abstract windowing toolkit.ppt of AJP sub1.Abstract windowing toolkit.ppt of AJP sub
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
 
Ad

More from PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK (20)

BASICS OF COMPUTER
BASICS OF COMPUTERBASICS OF COMPUTER
BASICS OF COMPUTER
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 6 ppts web services
Wt unit 6 ppts web servicesWt unit 6 ppts web services
Wt unit 6 ppts web services
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side frameworkWt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side framework
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 4 server side technology-2
Wt unit 4 server side technology-2Wt unit 4 server side technology-2
Wt unit 4 server side technology-2
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technologyWt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
web development process WT
web development process WTweb development process WT
web development process WT
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Unit 5 dsa QUEUE
Unit 5 dsa QUEUEUnit 5 dsa QUEUE
Unit 5 dsa QUEUE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Unit 3 dsa LINKED LIST
Unit 3 dsa LINKED LISTUnit 3 dsa LINKED LIST
Unit 3 dsa LINKED LIST
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Unit 1 dsa
Unit 1 dsaUnit 1 dsa
Unit 1 dsa
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 2 ppts client side technology
Wt unit 2 ppts client side technologyWt unit 2 ppts client side technology
Wt unit 2 ppts client side technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 1 ppts web development process
Wt unit 1 ppts web development processWt unit 1 ppts web development process
Wt unit 1 ppts web development process
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
OPERATING SYSTEM
OPERATING SYSTEMOPERATING SYSTEM
OPERATING SYSTEM
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
LEX & YACC TOOL
LEX & YACC TOOLLEX & YACC TOOL
LEX & YACC TOOL
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PL-3 LAB MANUAL
PL-3 LAB MANUALPL-3 LAB MANUAL
PL-3 LAB MANUAL
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERINGCOMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Ad

Recently uploaded (20)

Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 

CORE JAVA-2

  • 1. Pune Vidyarthi Griha’s COLLEGE OF ENGINEERING, NASHIK – 3. “ Core Java” By Prof. Anand N. Gharu (Assistant Professor) PVGCOE, (Computer Dept.) NASIK-4 30th June 2017 Computer Dept.
  • 2. Agenda 1. Abstract Windowing Toolkit (AWT) 2. Event Handling 3. Swing 4. Layout Manager 5. Applet 2
  • 3. Abstract Windowing Toolkit (AWT)  Abstract Windowing Toolkit (AWT) is used for GUI programming in java.  AWT Container Hierarchy: 3
  • 4. Container:  The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container. Window:  The window is the container that have no borders and menubars. You must use frame, dialog or another window for creating a window. Panel:  The Panel is the container that doesn't contain title bar and MenuBars. It can have other components like button, textfield etc. Frame:  The Frame is the container that contain title bar and can have MenuBars. It can have other components like button, textfield etc. 4
  • 5. Creating a Frame Commonly used Methods of Component class:  1)public void add(Component c)  2)public void setSize(int width,int height)  3)public void setLayout(LayoutManager m)  4)public void setVisible(boolean) Creating a Frame: There are two ways to create a frame:  By extending Frame class (inheritance)  By creating the object of Frame class (association) 5
  • 6. Creating a Frame: Set Button Simple example of AWT by inheritance: import java.awt.*; class First extends Frame{ First(){ Button b=new Button("click me"); b.setBounds(30,100,80,30);// setting button position add(b);//adding button into frame setSize(300,300);//frame size 300 width and 300 height setLayout(null);//no layout now bydefault BorderLayout setVisible(true);//now frame willbe visible, bydefault not visible } public static void main(String args[]){ First f=new First(); } } 6
  • 7. Button  public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above example that sets the position of the button. 7
  • 8. Event and Listener (Event Handling)  Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling.  Event classes and Listener interfaces Event Classes Listener Interfaces  ActionEvent ActionListener  MouseEvent MouseListener MouseMotionListener  MouseWheelEvent MouseWheelListener  KeyEvent KeyListener  ItemEvent ItemListener 8
  • 9. Event classes and Listener interfaces Event Classes Listener Interfaces  TextEvent TextListener  AdjustmentEvent AdjustmentListener  WindowEvent WindowListener  ComponentEvent ComponentListener  ContainerEvent ContainerListener  FocusEvent FocusListener Steps to perform EventHandling:  Implement the Listener interface and overrides its methods  Register the component with the Listener 9
  • 10. Steps to perform EventHandling  For registering the component with the Listener, many classes provide methods. F  Button public void addActionListener(ActionListener a){}  MenuItem public void addActionListener(ActionListener a){}  TextField public void addActionListener(ActionListener a){} public void addTextListener(TextListener a){}  TextArea public void addTextListener(TextListener a){}  Checkbox public void addItemListener(ItemListener a){} 10
  • 11. EventHandling Codes  Choice public void addItemListener(ItemListener a){}  List public void addActionListener(ActionListener a){} public void addItemListener(ItemListener a){} EventHandling Codes:  We can put the event handling code into one of the following places:  Same class  Other class  Annonymous class 11
  • 12. Example of event handling within class import java.awt.*; import java.awt.event.*; class AEvent extends Frame implements ActionListener{ TextField tf; AEvent(){ tf=new TextField(); tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(100,120,80,30); b.addActionListener(this); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e){ tf.setText("Welcome"); } public static void main(String args[]){ new AEvent(); } } 12
  • 13. Output  public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above example that sets the position of the component it may be button, textfield etc. 13
  • 14. Example of event handling by Outer class import java.awt.*; import java.awt.event.*; class AEvent2 extends Frame implements ActionListener{ TextField tf; AEvent2(){ tf=new TextField(); tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(100,120,80,30); b.addActio nListener(this); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e){ tf.setText("Welcome"); } public static void main(String args[]){ new AEvent2(); } } 14
  • 15. Outer class import java.awt.event.*; class Outer implements ActionListener{ AEvent2 obj; Outer(AEvent2 obj){ this.obj=obj; } public void actionPerformed(ActionEvent e){ obj.tf.setText("welcome"); } } 15
  • 16. Example of event handling by Annonymous class import java.awt.*; import java.awt.event.*; class AEvent3 extends Frame{ TextField tf; AEvent3(){ tf=new TextField(); tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(50,120,80,30); b.addActionListener(new ActionListener(){ public void actionPerformed(){ tf.setText("hello"); } }); add(b); add(tf); setSize(300,300); setLayout(null); setVisible(true); } public static void main(String args[]) { new AEvent3(); } } 16
  • 17. Swing (Graphics Programming in java)  Swing is a part of JFC (Java Foundation Classes) that is used to create GUI application. It is built on the top of Swing is a part of JFC (Java Foundation Classes) that is used to create GUI application. It is built on the top of AWT and entirely written in java. Advantage of Swing over AWT:  There are many advantages of Swing over AWT. They are as follows:  Swing components are Plateform independent & It is lightweight.  It supports pluggable look and feel.  It has more powerful componenets like tables, lists, scroll panes, color chooser, tabbed pane etc.  It follows MVC (Model View Controller) architecture.  What is JFC ? - The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications. 17
  • 19. Commonly used Methods of Component class Commonly used Methods of Component class:  1)public void add(Component c)  2)public void setSize(int width,int height)  3)public void setLayout(LayoutManager m)  4)public void setVisible(boolean) Creating a Frame:  There are two ways to create a frame:  By creating the object of Frame class (association)  By extending Frame class (inheritance) 19
  • 20. Simple example of Swing by Association import javax.swing.*; public class Simple { JFrame f; Simple(){ f=new JFrame(); JButton b=new JButton("click"); b.setBounds(130,100,100, 40); f.add(b); f.setSize(400,500); f.setLayout(null); f.setVisible(true); } public static void main(String[] args) { new Simple(); } } 20
  • 21. Output  public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above example that sets the position of the button. 21
  • 22. Button class  The JButton class is used to create a button that have plateform-independent implementation. Commonly used Constructors:  JButton(): creates a button with no text and icon.  JButton(String s): creates a button with the specified text.  JButton(Icon i): creates a button with the specified icon object. Commonly used Methods of AbstractButton class:  1) public void setText(String s): is used to set specified text on button.  2) public String getText(): is used to return the text of the button.  3) public void setEnabled(boolean b): is used to enable or disable the button.  4) public void setIcon(Icon b): is used to set the specified Icon on the button.  5) public Icon getIcon(): is used to get the Icon of the button.  6) public void setMnemonic(int a): is used to set the mnemonic on the button. 22
  • 23. Example of displaying image on the button 7) public void addActionListener(ActionListener a): is used to add the action listener to this object. Note: The JButton class extends AbstractButton class. import java.awt.event.*; import javax.swing.*; public class ImageButton{ ImageButton(){ JFrame f=new JFrame(); JButton b=new JButton(new ImageIcon("b.jpg")); b.setBounds(130,100,100, 40); f.add(b); f.setSize(300,400); f.setLayout(null); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new ImageButton(); } } 23
  • 24. JRadioButton class  The JRadioButton class is used to create a radio button. Commonly used Constructors of JRadioButton class:  JRadioButton(): creates an unselected radio button with no text.  JRadioButton(String s): creates an unselected radio button with specified text.  JRadioButton(String s, boolean selected): creates a radio button with the specified text and selected status. Commonly used Methods of AbstractButton class:  1) public void setText(String s): is used to set specified text on button.  2) public String getText(): is used to return the text of the button.  3) public void setEnabled(boolean b): is used to enable or disable the button.  4) public void setIcon(Icon b): is used to set the specified Icon on the button.  5) public Icon getIcon(): is used to get the Icon of the button.  6) public void setMnemonic(int a): is used to set the mnemonic on the button. 24
  • 25. Example of JRadioButton class 7) public void addActionListener(ActionListener a): is used to add the action listener to this object. // Note: The JRadioButton class extends the JToggleButton class that extends AbstractButton class. import javax.swing.*; public class Radio { JFrame f; Radio(){ f=new JFrame(); JRadioButton r1=new JRadioButton("A) Male"); JRadioButton r2=new JRadioButton("B) FeMale"); r1.setBounds(50,100,70,30); r2.setBounds(50,150,70,30); ButtonGroup bg=new ButtonGroup(); bg.add(r1);bg.add(r2); f.add(r1);f.add(r2); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public static void main(String[] args) { new Radio(); } } 25
  • 26. JTextArea class  The JTextArea class is used to create a text area. It is a multiline area that displays the plain text only. Commonly used Constructors:  JTextArea(): creates a text area that displays no text initally.  JTextArea(String s): creates a text area that displays specified text initally.  JTextArea(int row, int column): creates a text area with the specified number of rows and columns that displays no text initally..  JTextArea(String s, int row, int column): creates a text area with the specified number of rows and columns that displays specified text. Commonly used methods of JTextArea class:  1) public void setRows(int rows): is used to set specified number of rows.  2) public void setColumns(int cols):: is used to set specified number of columns. 26
  • 27. Example of JTextField class 3) public void setFont(Font f): is used to set the specified font. 4) public void insert(String s, int position): is used to insert the specified text on the specified position. 5) public void append(String s): to append the given text to the end of the document. import java.awt.Color; import javax.swing.*; public class TArea { JTextArea area; JFrame f; TArea(){ f=new JFrame(); area=new JTextArea(300,300); area.setBounds(10,30,300,300); area.setBackground(Color.black); area.setForeground(Color.white); f.add(area); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } public static void main(String[] args) { new TArea(); } } 27
  • 28. JComboBox class  The JComboBox class is used to create the combobox (drop-down list). At a time only one item can be selected from the item list. Commonly used Constructors of JComboBox class:  JComboBox()  JComboBox(Object[] items)  JComboBox(Vector<?> items) Commonly used methods of JComboBox class:  1) public void addItem(Object anObject): is used to add an item to the item list.  2) public void removeItem(Object anObject): is used to delete an item to the item list.  3) public void removeAllItems(): is used to remove all the items from the list.  4) public void setEditable(boolean b): is used to determine whether the JComboBox is editable. 28
  • 29. Example of JComboBox class 5) public void addActionListener(ActionListener a): is used to add the ActionListener. 6) public void addItemListener(ItemListener i): is used to add the ItemListener. import javax.swing.*; public class Combo { JFrame f; Combo(){ f=new JFrame("Combo ex"); String country[]={"India","Aus","U.S.A","England","Newzeland"}; JComboBox cb=new JComboBox(country); cb.setBounds(50, 50,90,20); f.add(cb); f.setLayout(null); f.setSize(400,500); f.setVisible(true); } public static void main(String[] args) { new Combo(); } } 29
  • 30. JTable class The JTable class is used to display the data on two dimentional tables of cells. Commonly used Constructors of JTable class: • JTable(): creates a table with empty cells. • JTable(Object[][] rows, Object[] columns): creates a table with the specified data. Example of JTable class: import javax.swing.*; public class MyTable { JFrame f; MyTable(){ f=new JFrame(); String data[][]={ {"101","Amit","670000"}, {"102","Jai","780000"}, {"101","Sachin","700000"}}; String column[]={"ID","NAME","SALARY"}; JTable jt=new JTable(data,column); 30
  • 31. Example jt.setBounds(30,40,200,300); JScrollPane sp=new JScrollPane(jt); f.add(sp); f.setSize(300,400); // f.setLayout(null); f.setVisible(true); } public static void main(String[] args) { new MyTable(); } } 31
  • 32. JColorChooser class  The JColorChooser class is used to create a color chooser dialog box so that user can select any color. Commonly used Constructors of JColorChooser class:  JColorChooser(): is used to create a color chooser pane with white color initially.  JColorChooser(Color initialColor): is used to create a color chooser pane with the specified color initially. Commonly used methods of JColorChooser class:  public static Color showDialog(Component c, String title, Color initialColor): is used to show the color-chooser dialog box. 32
  • 34. import java.awt.event.*; import java.awt.*; import javax.swing.*; public class JColorChooserExample extends JFrame implements ActionListener{ JButton b; Container c; JColorChooserExample(){ c=getContentPane(); c.setLayout(new FlowLayout()); b=new JButton("color"); b.addActionListener(this); c.add(b); } public void actionPerformed(ActionEvent e) { Color initialcolor=Color.RED; Color color=JColorChooser.showDialog(this,"Select a color",initialcolor); c.setBackground(color); } 34
  • 35. Example of JColorChooser class public static void main(String[] args) { JColorChooserExample ch=new JColorChooserExample(); ch.setSize(400,400); ch.setVisible(true); ch.setDefaultCloseOperation(EXIT_ON_CLOSE); } } 35
  • 36. JProgressBar class  The JProgressBar class is used to display the progress of the task. Commonly used Constructors of JProgressBar class:  JProgressBar(): is used to create a horizontal progress bar but no string text.  JProgressBar(int min, int max): is used to create a horizontal progress bar with the specified minimum and maximum value.  JProgressBar(int orient): is used to create a progress bar with the specified orientation, it can be either Vertical or Horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants.  JProgressBar(int orient, int min, int max): is used to create a progress bar with the specified orientation, minimum and maximum value. 36
  • 37. Example of JProgressBar class Commonly used methods of JProgressBar class: 1) public void setStringPainted(boolean b): is used to determine whether string should be displayed. 2) public void setString(String s): is used to set value to the progress string. 3) public void setOrientation(int orientation): is used to set the orientation, it may be either vertical or horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants.. 4) public void setValue(int value): is used to set the current value on the progress bar. import javax.swing.*; public class MyProgress extends JFrame{ JProgressBar jb; int i=0,num=0; MyProgress(){ jb=new JProgressBar(0,2000); jb.setBounds(40,40,200,30); 37
  • 38. Example of JProgressBar class jb.setValue(0); jb.setStringPainted(true); add(jb); setSize(400,400); setLayout(null); } public void iterate(){ while(i<=2000){ jb.setValue(i); i=i+20; try{Thread.sleep(150);}catch(Exception e){} } } public static void main(String[] args) { MyProgress m=new MyProgress(); m.setVisible(true); m.iterate(); } } 38
  • 39. JSlider class  The JSlider is used to create the slider. By using JSlider a user can select a value from a specific range. Commonly used Constructors of JSlider class:  JSlider(): creates a slider with the initial value of 50 and range of 0 to 100.  JSlider(int orientation): creates a slider with the specified orientaion set by either JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to 100 and intial value 50.  JSlider(int min, int max): creates a horizontal slider using the given min and max.  JSlider(int min, int max, int value): creates a horizontal slider using the given min, max and value. 39
  • 40. JSlider class JSlider(int orientation, int min, int max, int value): creates a slider using the given orientation, min, max and value. Commonly used Methods of JSlider class:  1) public void setMinorTickSpacing(int n): is used to set the minor tick spacing to the slider.  2) public void setMajorTickSpacing(int n): is used to set the major tick spacing to the slider.  3) public void setPaintTicks(boolean b): is used to determine whether tick marks are painted.  4) public void setPaintLabels(boolean b): is used to determine whether labels are painted.  5) public void setPaintTracks(boolean b): is used to determine whether track is painted. 40
  • 41. Simple example of JSlider class import javax.swing.*; public class SliderExample1 extends JFrame{ public SliderExample1() { JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25); JPanel panel=new JPanel(); panel.add(slider); add(panel); } public static void main(String s[]) { SliderExample1 frame=new SliderExample1(); frame.pack(); frame.setVisible(true); } } 41
  • 42. Example of JSlider class that paints ticks import javax.swing.*; public class SliderExample extends JFrame{ public SliderExample() { JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25); slider.setMinorTickSpacing(2); slider.setMajorTickSpacing(10); slider.setPaintTicks(true); slider.setPaintLabels(true); JPanel panel=new JPanel(); panel.add(slider); add(panel); } public static void main(String s[]) { SliderExample frame=new SliderExample(); frame.pack(); frame.setVisible(true); } } 42
  • 43. BorderLayout (LayoutManagers) The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes of layout managers. There are following classes that represents the layout managers:  java.awt.BorderLayout  java.awt.FlowLayout  java.awt.GridLayout  java.awt.CardLayout  java.awt.GridBagLayout  javax.swing.BoxLayout  javax.swing.GroupLayout  javax.swing.ScrollPaneLayout  javax.swing.SpringLayout etc. 43
  • 44. BorderLayout  The BorderLayout is used to arrange the components in five regions: north, south, east, west and center. Each region (area) may contain one component only. It is the default layout of frame or window. The BorderLayout provides five constants for each region:  1. public static final int NORTH  2. public static final int SOUTH  3. public static final int EAST  4. public static final int WEST  5. public static final int CENTER Constructors of BorderLayout class:  BorderLayout(): creates a border layout but with no gaps between the components. JBorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and vertical gaps between the components. 44
  • 46. Example of BorderLayout class import java.awt.*; import javax.swing.*; public class Border { JFrame f; Border(){ f=new JFrame(); JButton b1=new JButton("NORTH");; JButton b2=new JButton("SOUTH");; JButton b3=new JButton("EAST");; JButton b4=new JButton("WEST");; JButton b5=new JButton("CENTER");; f.add(b1,BorderLayout.NORTH); f.add(b2,BorderLayout.SOUTH); f.add(b3,BorderLayout.EAST); f.add(b4,BorderLayout.WEST); f.add(b5,BorderLayout.CENTER); f.setSize(300,300); f.setVisible(true); } public static void main(String[] args) { new Border(); } } 46
  • 47. GridLayout  The GridLayout is used to arrange the components in rectangular grid. One component is dispalyed in each rectangle.  Constructors of GridLayout class:  GridLayout(): creates a grid layout with one column per component in a row.  GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components.  GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns alongwith given horizontal and vertical gaps. 47
  • 49. Example of GridLayout class import java.awt.*; import javax.swing.*; public class MyGridLayout{ JFrame f; MyGridLayout(){ f=new JFrame(); JButton b1=new JButton("1"); JButton b2=new JButton("2"); JButton b3=new JButton("3"); JButton b4=new JButton("4"); JButton b5=new JButton("5"); JButton b6=new JButton("6"); JButton b7=new JButton("7"); JButton b8=new JButton("8"); JButton b9=new JButton("9"); f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5); f.add(b6); f.add(b7 f.add(b8); f.add(b9); f.setLayout(new GridLayout(3,3)); f.setSize(300,300); f.setVisible(true); } //set grid layout of 3 rows and 3 columns public static void main(String[] args) { new MyGridLayout(); } } 49
  • 50. FlowLayout  The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel. Fields of FlowLayout class:  1. public static final int LEFT  2. public static final int RIGHT  3. public static final int CENTER  4. public static final int LEADING  5. public static final int TRAILING Constructors of FlowLayout class:  FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.  FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.  FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and the given horizontal and vertical gap. 50
  • 52. Example import java.awt.*; import javax.swing.*; public class MyFlowLayout{ JFrame f; MyFlowLayout(){ f=new JFrame(); JButton b1=new JButton("1"); JButton b2=new JButton("2"); JButton b3=new JButton("3"); JButton b4=new JButton("4"); JButton b5=new JButton("5"); f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5); f.setLayout(new FlowLayout(FlowLayout.RIGHT)); //setting flow layout of right alignment f.setSize(300,300); f.setVisible(true); } public static void main(String[] args) { new MyFlowLayout(); } } 52
  • 53. BoxLayout class  The BoxLayout is used to arrange the components either vertically or horizontally. For this purpose, BoxLayout provides four constants. They are as follows:  Note: BoxLayout class is found in javax.swing package. Fields of BoxLayout class:  1. public static final int X_AXIS  2. public static final int Y_AXIS  3. public static final int LINE_AXIS  4. public static final int PAGE_AXIS Constructor of BoxLayout class:  1. BoxLayout(Container c, int axis): creates a box layout that arranges the components with the given axis. 53
  • 54. Example of BoxLayout class with Y-AXIS 54
  • 55. Example of BoxLayout class with Y-AXIS import java.awt.*; import javax.swing.*; public class BoxLayoutExample1 extends Frame { Button buttons[]; public BoxLayoutExample1 () { buttons = new Button [5]; for (int i = 0;i<5;i++) { buttons[i] = new Button ("Button " + (i + 1)); add (buttons[i]); } setLayout (new BoxLayout (this, BoxLayout.Y_AXIS)); setSize(400,400); setVisible(true); } public static void main(String args[]){ BoxLayoutExample1 b=new BoxLayoutExample1(); } } 55
  • 56. Example of BoxLayout class with X-AXIS 56
  • 57. Example of BoxLayout class with X-AXIS import java.awt.*; import javax.swing.*; public class BoxLayoutExample2 extends Frame { Button buttons[]; public BoxLayoutExample2() { buttons = new Button [5]; for (int i = 0;i<5;i++) { buttons[i] = new Button ("Button " + (i + 1)); add (buttons[i]); } setLayout (new BoxLayout(this, BoxLayout.X_AXIS)); setSize(400,400); setVisible(true); } public static void main(String args[]){ BoxLayoutExample2 b=new BoxLayoutExample2(); } } 57
  • 58. CardLayout class  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 that is why it is known as CardLayout. Constructors of CardLayout class:  CardLayout(): creates a card layout with zero horizontal and vertical gap.  CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical gap.  Commonly used methods of CardLayout class:  public void next(Container parent): is used to flip to the next card of the given container.  public void previous(Container parent): is used to flip to the previous card of the given container. 58
  • 59. CardLayout class  public void first(Container parent): is used to flip to the first card of the given container.  public void last(Container parent): is used to flip to the last card of the given container.  public void show(Container parent, String name): is used to flip to the specified card with the given name. 59
  • 60. Example of CardLayout class 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); 60
  • 61. Example of CardLayout class 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); } } 61