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

Java Units

The document provides information about key concepts in Java Swing GUI programming, including: 1) The difference between AWT and Swing is that AWT uses platform-dependent "peer components" while Swing uses lightweight platform-independent components with a consistent look and feel across platforms. 2) Layout managers like FlowLayout, BorderLayout, GridLayout, CardLayout, GridBagLayout and BoxLayout are used to arrange and position components within containers in different ways. 3) Event handling in Swing follows the delegation model where component events are delegated to listeners registered with the component.

Uploaded by

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

Java Units

The document provides information about key concepts in Java Swing GUI programming, including: 1) The difference between AWT and Swing is that AWT uses platform-dependent "peer components" while Swing uses lightweight platform-independent components with a consistent look and feel across platforms. 2) Layout managers like FlowLayout, BorderLayout, GridLayout, CardLayout, GridBagLayout and BoxLayout are used to arrange and position components within containers in different ways. 3) Event handling in Swing follows the delegation model where component events are delegated to listeners registered with the component.

Uploaded by

bajagovindam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

UNIT-5

EXAM REVIEW
Important concepts
Give the difference between awt and swing? Or give the
features of swing?
Explain the following?
Japplet
Jframe
Explain different layout managers in detail?
What is event delegation model or event handling explain
it?
Write short notes on inner classes and adapter classes?
All the dialogs of joptionpane, jdialog?
Give the table of event, its listener, its event, its event
source? 8.Handle mouse events?
difference between awt and swing
AWT
SWING
AWT is called 'peer component Swing components do not
based' model. depend on native methods. SO
no portability issues.
The look-and-feel change
depending on the platform. same look-and-feel'on all
platforms.
heavy-weight. light-weight
user interface designs tended Model-View-Controller is a
to lump objects together. model used in swing
wont provide any pluggable components.
look and feel feature. Swing offers 'pluggable look-
and-feel' feature
Japplet is equivalent to applet already studied.
JFrame
Frames : A frame is a container which is used for displaying the
components in separate window.
A frame containing the following features :
i. The frame contains title bar, minimize, maximum, buttons.
ii. Contains resizable borders.
iii. Contains a menu bar.
To create a Jframe, create a class that extends the Jframe class.
Constructors :
Jframe ( ) : Creates a frame.
Jframe ( String title ) :
Methods :
void setTitle( String newTitle ) :
String getTitle( ) :
example
class MyFrame extends Frame
{
MyFrame()
{
setTitle("Sample Frame 4");
setSize(300,200);
setBackground(Color.pink);
addWindowListener(new MyWindowAdapter());
}
class frametest4
{
public static void main(String args[])
{
MyFrame f = new MyFrame();
f.setVisible(true); // show the frame
}
}
output
different layout managers
useful to arrange components in a particular
manner in a frame or container.
Different layouts
FlowLayout
BorderLayout
CardLayout
GridLayout
GridBagLayout
BoxLayout
Flow layout
arrange the components in a line one after
the other.
Syntax:
FlowLayout obj = new FlowLayout();
FlowLayout obj = new FlowLayout(int
alignment);//alignment is LEFT,
RIGHT,CENTER,LEADING,TRAILING
FlowLayout obj = new FlowLayout(int
alignment,int hgap, int vgap);
example
setLayout(new FlowLayout(FlowLayout.LEFT));
w = new JButton(one);
a = new JButton(Two");
s = new JButton(Three");
m = new JButton(Four");
j = new JButton(Five");

add(w);
add(a);
add(s);
add(m);
add(j);
output
BorderLayout

arrange the components in the 4 borders


of the frame and in the center.
syntax
BorderLayout obj=new BorderLayout();
BorderLayout obj = new BorderLayout(int
hgap, int vgap);
c.add(North,component)
c.add(component, BorderLayout.NORTH);
Where c is a container.
setLayout(new BorderLayout());
add(new Button(North"),
BorderLayout.NORTH);
add(new Label(South"),BorderLayout.SOUTH);
add(new Button(East"), BorderLayout.EAST);
add(new Button(West"), BorderLayout.WEST);
add(new Button(Center"),
BorderLayout.CENTER);
CardLayout

treats each component as a card.


container acts as a stack of cards
Only one card is visible at a time.
syntax
CardLayout obj = new CardLayout();
CardLayout obj = new CardLayout(int
hgap, int vgap);
c.add(cardname,component)
example
Win = new Button("Windows");
Other = new Button("Other");
add(Win);
add(Other);
cardLO = new CardLayout();
osCards = new Panel();
osCards.setLayout(cardLO);
winPan.add(windowsXP);
winPan.add(windows7);
winPan.add(windows8);
otherPan.add(android);
otherPan.add(solaris);
otherPan.add(mac);
osCards.add(winPan, "Windows");
osCards.add(otherPan, "Other");
output
GridLayout

divide the container into a two-dimensional


grid form that contains several rows and
columns.
syntax
GridLayout obj = new GridLayout();
GridLayout obj = new GridLayout(int rows
, int cols);
GridLayout obj = new GridLayout(int
rows, int eols, int hgap, int vgap);
example
setLayout(new GridLayout(4, 4));
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
int k = i * n + j;
if(k > 0)
add(new Button("" + k));
output
GridBagLayout

represents grid bag layout manager where


the components are arranged in rows and
columns.
the components can span more than one
row or column and the size of the
components can be adjusted to fit the
display area.
uses a helper class ( that is GridBag
constraints ) for placing the components.
GridBag constraint is containing some
variables.
They are i. int grdix;
ii. int gridy;
iii.int gridwidth;
iv. int gridheight;
syntax
GridbagLayout obj=new GridBagLayout();
To apply constraints use
GridBagConstraints cons=new
GridBagConstraints();
output
BoxLayout
Allows multiple components to be laid out either vertically or
horizontally.
Syntax
BoxLayout box=new BoxLayout(JPanel object, axis-orientation)
This 'axis-orientation' can be:
BoxLayout.X_AXIS: Here, components are arranged along x-axis
BoxLayout.Y_AXIS: Here, components are arranged along y-axis
BoxLayout.LINE_AXIS: Here, components are arranged like lines in a
text.
BoxLayout.PAGE_AXIS: Here, components are arranged like lines in
several pages.
event delegation model or event
handling

You might also like