Unit 5 Notes
Unit 5 Notes
BorderLayout (LayoutManagers)
Java 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.
Java 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:
For Example :
Pressing a button
Entering a character in Textbox
Clicking or Dragging a mouse, etc.
Components of Event Handling
Event handling has three main components,
import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdapter{
Frame f;
MouseAdapterExample(){
f=new Frame("Mouse Adapter");
f.addMouseListener(this);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
Graphics g=f.getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
import java.awt.*;
import java.awt.event.*;
public class MyApplet extends JApplet
{
JLabel label;
public void init()
{
setSize(600,300);
setLayout(new FlowLayout());
label = new JLabel();
add(label);
addMouseListener(new MyAdapter());
}
class MyAdapter extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
label.setText("Mouse is clicked");
}
}
}
Anonymous Inner Classes
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcome",150,150);
}
}
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
import java.awt.*;
import java.applet.*;
public class AppletTest extends Applet
{
public void init()
{
//initialization
}
public void start ()
{
//start or resume execution
}
public void stop()
{
//suspend execution
}
public void destroy()
{
//perform shutdown activity
}
public void paint (Graphics g)
{
//display the content of window
}
}
Parameter in Applet
• We can get any information from the HTML file as a
parameter. For this purpose, Applet class provides a
method named getParameter().
Syntax:
public String getParameter(String parameterName)
import java.applet.Applet;
import java.awt.Graphics;
public class UseParam extends Applet
{
public void paint(Graphics g)
{
String str=getParameter("msg");
g.drawString(str,50, 50);
}
}
/*
<applet code="UseParam.class" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
*/
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class JAppletDemo extends JApplet implements
ActionListener
{
JButton b;
JTextField t;
public void init()
{
t=new JTextField();
t.setBounds(30,40,220,20);
b=new JButton("Click");
b.setBounds(80,150,70,40);
add(b);
add(t);
b.addActionListener(this);
setLayout(null);
public void actionPerformed(ActionEvent e)
{
t.setText("Welcome to MRCE");
}
}
}
public static void main(String[] args) {
DisplayGraphics m=new DisplayGraphics();
JFrame f=new JFrame();
f.add(m);
f.setSize(400,400);
//f.setLayout(null);
f.setVisible(true);
}
}
import javax.swing.*;
class LabelExample
{
public static void main(String args[])
{
JFrame f= new JFrame("Label Example");
JLabel l1,l2;
l1=new JLabel("Enter your name");
l1.setBounds(50,50, 100,30);
JTextField tf =new JTextField();
tf.setBounds(50,100, 150,20);
f.add(l1); f.add(tf);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
UNIT-5
The Swing Buttons
The Swing Buttons
JButton,
JToggle Button,
JCheck Box,
JRadio Button,
JTabbed Pane,
JScroll Pane,
JList,
JCombo Box,
Swing Menus,
Dialogs.
Java JButton
• The JButton class is used to create a labeled button. The
application result in some action when the button is
pushed. It inherits AbstractButton class.
JButton class declaration
javax.swing.JButton class.
public class JButton extends AbstractButton implem
ents Accessible
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Java JCheckBox
• The JCheckBox class is used to create a checkbox. It is
used to turn an option on (true) or off (false). Clicking on a
CheckBox changes its state from "on" to "off" or from
"off" to "on ".It inherits JToggleButton class.
JCheckBox class declaration
javax.swing.JCheckBox
public class JCheckBox extends JToggleButton implement
s Accessible
Constructor Description
Constructor Description
Constructor Description
l1.addElement("Item1");
l1.addElement("Item2");
l1.addElement("Item3");
l1.addElement("Item4");
JList<String> list = new JList<>(l1);
list.setBounds(100,100, 75,75);
f.add(list);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ListExample();
}}
Java JScrollBar
• The object of JScrollbar class is used to add
horizontal and vertical scrollbar. It is an
implementation of a scrollbar. It inherits
JComponent class.
Constructor Description