UNIT-5
Java AWT Toolkit:
The Abstract Window Toolkit (AWT) is a Java package that provides a platform-
independent set of tools for creating graphical usеr intеrfacеs (GUIs).
AWT is part of thе Java Foundation Classеs (JFC), which also includes Swing for morе
advancеd GUI dеvеlopmеnt. In this rеsponsе, I'll providе an ovеrviеw of thе kеy
componеnts and concеpts in AWT, along with somе samplе codе to dеmonstratе thеir
usagе.
"Toolkit class" is thе abstract supеrclass of еvеry implеmеntation in thе "Abstract
Window Toolkit". Subclassеs of Toolkit arе usеd to bind various componеnts.
Syntax for AWT Tool Kit
public abstract class Toolkit extends Object
Key Components of AWT:
1. Components: AWT provides various GUI components, such as buttons, labels, text
fields, and more, which are used to build the user interface.
2. Layout Managers: AWT includes layout managers like FlowLayout, BorderLayout,
and GridLayout that help in arranging components within containers.
3. Event Handling: AWT supports event handling for user interactions. You can use
event listeners and adapters to handle events like button clicks, mouse movements, and
keypresses.
4. Containers: AWT provides container classes like Frame, Panel, and Window for
organizing and grouping components.
5. Graphics: You can draw shapes, text, and images using the Graphics and
Graphics2D classes.
6. Fonts and Colors: AWT allows you to work with fonts and colors for customizing the
appearance of your components.
Main types of containers in Java AWT:
1. Frame:
A Frame is a top-level container and represents a graphical window or dialog box.
It can have a title bar, border, and menu bars.
Typically used as the primary window for an AWT application.
You can add various components to a Frame, and it often serves as the main
container for creating a complete application.
2.Panel:
A Panel is a lightweight container that is used for grouping other components
together within a window or a frame.
It does not have a title bar, border, or menu bar, making it suitable for organizing
components.
Panels are often used to group related UI elements within a Frame or another
container.
3. Dialog:
A Dialog is a temporary window that an application creates to retrieve user input or
provide information.
It has a title bar and border and can contain buttons for user interaction.
Dialogs are commonly used for tasks like displaying error messages, confirmation
dialogs, or input forms.
4. Window:
A Window is a top-lеvеl containеr likе a Framе, but it has no titlе bar or bordеr.
It is mainly usеd whеn you nееd to crеatе additional windows for your application that
don't rеquirе thе standard window dеcorations.
You can add othеr componеnts to a Window as wеll.
1. Event Handling Mechanisms in Java AWT
1. ActionListener: Handles action events, like button clicks.
2. MouseListener: Manages mouse events, such as clicks and movements.
3. KeyListener: Deals with keyboard events, like key presses and releases.
4. WindowListener: Listens to window-related events like opening, closing, or resizing.
5. ComponentListener: Handles component-specific events, e.g., resizing
components.
Classes of AWT package:
• Label
• Button -
• TextField -
• Checkbox -
• CheckboxGroup -
• Choice -
• List -
• Canvas -
• Scrollbar -
• MenuItem & Menu -
• PopupMenu -
• Panel -
• Toolkit -
Event Handling Components - Java AWT
1. ActionListener
2. MouseListener
3. MouseMotionListener
4. ItemListener
5. KeyListener
6. WindowListener
We have the swing package in java for more advance classes :
1. JFrame
2. JPanel
3. JLAbel
4. JTextField
5. JTextArea
6. JButton
7. JList
8. JCheckBox
9. ButtonGroup
10. JRadioButton
11. JComboBox
12. JDialog
Example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JFrame_Demo implements ActionListener
JFrame f;
JLabel l1,l2,l3,l4,l5;
JTextField t1;
JPasswordField t2;
JButton b1,b2,b3;
JComboBox jb1;
JRadioButton male;
JRadioButton female;
ButtonGroup btg;
JDialog d;
public JFrame_Demo()
f=new JFrame("Registration Form");
l1=new JLabel("Login");
l2=new JLabel("Password");
l3=new JLabel("Gender");
l4=new JLabel("City");
l5=new JLabel("on the dialog box");
t1=new JTextField(20);
t2=new JPasswordField(20);
b1=new JButton("Click Me");
b2=new JButton("Reset");
b3=new JButton("Close The Form");
male=new JRadioButton("Male");
female=new JRadioButton("Female");
btg=new ButtonGroup();
String s1[] = { "Mathura", "Mumbai", "Noida", "Agra", "New Delhi" };
jb1=new JComboBox(s1);
f.getContentPane().setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.getContentPane().setBackground(Color.cyan);
l1.setFont(new Font("Arial",Font.BOLD,20));
l1.setSize(100,20);
l1.setLocation(100, 100);
f.add(l1);
t1.setFont(new Font("Times New Roman",Font.ITALIC,20));
t1.setSize(300,30);
t1.setLocation(200, 100);
f.add(t1);
l2.setFont(new Font("Arial",Font.BOLD,20));
l2.setSize(100,20);
l2.setLocation(100, 150);
f.add(l2);
t2.setSize(300,30);
t2.setLocation(200, 150);
f.add(t2);
l3.setFont(new Font("Arial",Font.BOLD,20));
l3.setSize(100, 20);
l3.setLocation(100, 200);
f.add(l3);
male.setFont(new Font("Arial",Font.BOLD,20));
male.setSelected(true);
male.setSize(80, 20);
male.setLocation(200, 200);
f.add(male);
female.setFont(new Font("Arial",Font.BOLD,20));
female.setSelected(false);
female.setSize(100, 20);
female.setLocation(300 ,200);
f.add(female);
btg.add(male);
btg.add(female);
b1.setFont(new Font("Arial",Font.BOLD,20));
b1.setSize(100,20);
b1.setLocation(100, 250);
f.add(b1);
b2.setFont(new Font("Arial",Font.BOLD,20));
b2.setSize(100 ,20);
b2.setLocation(290, 250);
f.add(b2);
b3.setFont(new Font("Arial",Font.BOLD,20));
b3.setSize(250,20);
b3.setLocation(450, 250);
f.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
l4.setFont(new Font("Arial",Font.BOLD,20));
l4.setSize(100,20);
l4.setLocation(100, 300);
f.add(l4);
jb1.setFont(new Font("Arial",Font.BOLD,20));
jb1.setSize(150,50);
jb1.setLocation(200, 300);
f.add(jb1);
f.setVisible(true);
public void actionPerformed(ActionEvent e)
try
if(e.getSource()==b1)
System.out.println("welcome");
d = new JDialog(f, "dialog Box appearing");
d.add(l5);
d.setSize(600, 600);
d.setVisible(true);
else
if(e.getSource()==b2)
t1.setText("");
t2.setText("");
if(e.getSource()==b3)
System.exit(0);
}catch(Exception ee){System.out.println(ee);}
public static void main(String[] aa)
JFrame_Demo fm=new JFrame_Demo();
Above example is showing all the controls ( which are above listed) .
JFrame : The class JFrame is an extended version of java.awt.Frame that adds support for
the JFC/Swing component architecture.
Some Methods From JFrame Class:
1.Container getContentPane() :Returns the contentPane object for this frame.
2.int getDefaultCloseOperation() :Returns the operation that occurs when the user
initiates a "close" on this frame.
3.void setDefaultCloseOperation(int operation) :Sets the operation that will happen
by default when the user initiates a "close" on this frame.
4.void setIconImage(Image image):Sets the image to be displayed as the icon for this
window.
5.void setJMenuBar(JMenuBar menubar):Sets the menubar for this frame.
6.void setLayout(LayoutManager manager):Sets the LayoutManager.
import java.awt.*;
import javax.swing.*;
class JFrame_Methods
JFrame f;
public JFrame_Methods()
f=new JFrame("Various Methods of JFrame Class");
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// f.setSize(700,700); works when setExtended is not applied
f.getContentPane().setBackground(Color.red);
f.setLayout(new FlowLayout()); // setting the layout of the controls
f.setVisible(true);
public static void main(String[] aaa)
JFrame_Methods j=new JFrame_Methods();
} }
Creating a 2D Shape in Java
Here is information about drawing shapes in Java.
Java provides several ways to draw shapes, primarily using the java.awt and javax.swing packages. Here's a breakdown
of the common approaches:
1. Using Graphics and Graphics2D
The Graphics class provides basic drawing capabilities, while Graphics2D extends it with more advanced features.
To draw shapes, you typically override the paintComponent method of a JPanel or JFrame.
You obtain a Graphics object within this method and use it to draw shapes.
2. Common Drawing Methods
fillRect(x, y, width, height): Draws a filled rectangle.
drawRect(x, y, width, height): Draws a rectangle outline.
fillOval(x, y, width, height): Draws a filled circle or oval.
drawOval(x, y, width, height): Draws a circle or oval outline.
drawLine(x1, y1, x2, y2): Draws a line.
drawPolygon(xPoints, yPoints, nPoints): Draws a polygon outline.
fillPolygon(xPoints, yPoints, nPoints): Draws a filled polygon.
drawString(text, x, y): Draws text.
3. Using Shape Objects
The java.awt.geom package provides classes like Rectangle2D, Ellipse2D, Line2D, and GeneralPath to represent shapes
as objects.
This can be useful for more complex shapes or when you need to manipulate shapes programmatically.
You can then use Graphics2D.draw(Shape) or Graphics2D.fill(Shape) to render them.
import java.awt.*;
import javax.swing.*;
class GraphicsDemo extends JFrame
{
public GraphicsDemo()
{
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
}
public void paint(Graphics g)
{
super.paint(g);
g.fillOval(100,100,100,50);
g.drawRect(200,200,200,50);
g.drawLine(300,300,400,400);
}
public static void main(String[] aaa)
{
new GraphicsDemo();
}
Image Demo:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Image_Demo extends Frame
{
static Image img;
Image_Demo()
{
img =
Toolkit.getDefaultToolkit().getImage("C:\\Users\\Rajani\\Pictures\\Photo.jpg");
MediaTracker track = new MediaTracker(this);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//Use a unique id for the image object
track.addImage(img,0);
try {
track.waitForID(0);
}
catch(InterruptedException ae){
}
//The JVM waits until the image is completely loaded
public void paint(Graphics g){
g.drawImage(img,150,200,200,200,null);
}
public static void main(String args[]){
Image_Demo g = new Image_Demo();
g.setTitle("Image Showing");
g.setSize(500,500);
//Setting the IconImage in the Frame
g.setIconImage(img);
g.setVisible(true);
Event Handling : Event handling in Java is the procedure that controls an event
and performs appropriate action if it occurs. The code or set of instructions used
to implement it is known as the Event handler.
It consists of two major components:
The event source and
The event listener.
The source is the object where the event occurs, and the event listener is
responsible for taking appropriate actions when an event occurs. These Listeners
must be registered with the source object in order for the listener to receive event
notifications.
What is Event Handling in Java?
Many event listeners are frequently used; recall the click of a button that takes
you to another website or the mouse scroll? All of this is accomplished through
the use of various event handlers, and the mechanism is known as event handling.
Events in Java
Events in Java represent the change in the state of any object. Events occur when
the user interacts with the interface. Clicking a button, moving the mouse, typing a
character, selecting an item from a list, and scrolling the page are all examples of
behaviors that cause an event to occur.
Types of Events in Java:
1. Foreground Events: These events necessitate the user's direct
participation. They are produced as a result of a user interacting with
graphical components in a Graphical User Interface.
2. Background Events: Background events are those that require end-user
interaction. Operating system interrupts and hardware or software
failures are examples of background events.
The image below shows the flow chart of the event delegation model.
Event Classes and Listener Interfaces in Java
Event Classes Description Listener Interface
When a button is clicked or a list item is double-clicked,
ActionEvent ActionListener
an ActionEvent is triggered.
MouseEvent This event indicates a mouse action occurred in a component MouseListener
The Key event is triggered when the character is entered using
KeyEvent KeyListener
the keyboard.
ItemEvent An event that indicates whether an item was selected or not. ItemListener
TextEvent when the value of a textarea or text field is changed TextListener
MouseWheelEvent generated when the mouse wheel is rotated MouseWheelListener
The object of this class represents the change in the state of a
WindowEvent window and are generated when the window is activated, WindowListener
deactivated, deiconified, iconified, opened or closed
ComponentEvent when a component is hidden, moved, resized, or made visible ComponentEventListener
ContainerEvent when a component is added or removed from a container ContainerListener
AdjustmentEvent when the scroll bar is manipulated AdjustmentListener
FocusEvent when a component gains or loses keyboard focus FocusListener
ActionListener : Eample
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;
class Menu_Demo implements ActionListener, ItemListener
{
JFrame f;
JLabel l1,l2,l3,l4,l5;
JTextField t1;
JPasswordField t2;
JButton b1,b2,b3;
JComboBox jb1;
JToggleButton tb;
JRadioButton male;
JRadioButton female;
ButtonGroup btg;
JDialog d;
JMenuBar mb;
JMenu m1,m2,m3;
JMenuItem mi1,mi2,mi3,mi4;
public Menu_Demo()
{
f=new JFrame("Registration Form");
l1=new JLabel("Login");
l2=new JLabel("Password");
l3=new JLabel("Gender");
l4=new JLabel("City");
l5=new JLabel("on the dialog box");
t1=new JTextField(20);
t2=new JPasswordField(20);
b1=new JButton("Click Me");
b2=new JButton("Reset");
b3=new JButton("Close The Form");
male=new JRadioButton("Male");
female=new JRadioButton("Female");
btg=new ButtonGroup();
tb=new JToggleButton("Its Toggle Button");
String s1[] = { "Mathura", "Mumbai", "Noida", "Agra", "New Delhi" };
jb1=new JComboBox(s1);
f.getContentPane().setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.getContentPane().setBackground(Color.cyan);
l1.setFont(new Font("Arial",Font.BOLD,20));
l1.setSize(100,20);
l1.setLocation(100, 100);
f.add(l1);
t1.setFont(new Font("Times New Roman",Font.ITALIC,20));
t1.setSize(300,30);
t1.setLocation(200, 100);
f.add(t1);
l2.setFont(new Font("Arial",Font.BOLD,20));
l2.setSize(100,20);
l2.setLocation(100, 150);
f.add(l2);
t2.setSize(300,30);
t2.setLocation(200, 150);
f.add(t2);
l3.setFont(new Font("Arial",Font.BOLD,20));
l3.setSize(100, 20);
l3.setLocation(100, 200);
f.add(l3);
male.setFont(new Font("Arial",Font.BOLD,20));
male.setSelected(true);
male.setSize(80, 20);
male.setLocation(200, 200);
f.add(male);
female.setFont(new Font("Arial",Font.BOLD,20));
female.setSelected(false);
female.setSize(100, 20);
female.setLocation(300 ,200);
f.add(female);
btg.add(male);
btg.add(female);
b1.setFont(new Font("Arial",Font.BOLD,20));
b1.setSize(100,20);
b1.setLocation(100, 250);
f.add(b1);
b2.setFont(new Font("Arial",Font.BOLD,20));
b2.setSize(100 ,20);
b2.setLocation(290, 250);
f.add(b2);
tb.setFont(new Font("Arial",Font.BOLD,20));
tb.setSize(250,20);
tb.setLocation(450, 250);
f.add(tb);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
l4.setFont(new Font("Arial",Font.BOLD,20));
l4.setSize(100,20);
l4.setLocation(100, 300);
f.add(l4);
jb1.setFont(new Font("Arial",Font.BOLD,20));
jb1.setSize(150,50);
jb1.setLocation(200, 300);
f.add(jb1);
mb=new JMenuBar();
m1=new JMenu("File");
m2=new JMenu("Edit");
m3=new JMenu("Format");
mi1=new JMenuItem("New");
mi2=new JMenuItem("Open");
mi3=new JMenuItem("Print");
mi4=new JMenuItem("Close");
mb.add(m1);
mb.add(m2);
mb.add(m3);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
mi4.addActionListener(this);
mi3.addActionListener(this);
tb.addItemListener(this);
f.setJMenuBar(mb);
f.setVisible(true);
}
public void itemStateChanged(ItemEvent itemEvent)
{
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED)
{
System.out.println("Selected");
}
else
{
// else print deselected in console
System.out.println("Deselected");
}
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==b1)
{
System.out.println("welcome");
d = new JDialog(f, "dialog Box appearing");
d.add(l5);
d.setSize(600, 600);
d.setVisible(true);
}
else
if(e.getSource()==b2)
{
t1.setText("");
t2.setText("");
}
if(e.getSource()==b3)
{
System.exit(0);
}
if(e.getSource()==mi4)
{
System.exit(0);
}
if(e.getSource()==mi3)
{
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(t1.getPrintable(null,null));
if(pj.printDialog())
{
try
{
pj.print();
}catch(Exception ee){System.out.println(ee);}
}
}
}catch(Exception ee){System.out.println(ee);}
}
public static void main(String[] aa)
{
Menu_Demo fm=new Menu_Demo();
}
}
MouseListener & MouseMotionListener
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MouseListener_Demo implements ActionListener ,MouseListener,
MouseMotionListener
{
JFrame f;
JLabel l1,l2,l3,l4,l5, label1;
JTextField t1;
JPasswordField t2;
JButton b1,b2,b3;
JComboBox jb1;
JRadioButton male;
JRadioButton female;
ButtonGroup btg;
JDialog d;
public MouseListener_Demo()
{
f=new JFrame("Registration Form");
l1=new JLabel("Login");
l2=new JLabel("Password");
l3=new JLabel("Gender");
l4=new JLabel("City");
l5=new JLabel("on the dialog box");
label1=new JLabel("fjfjfhfhfh");
t1=new JTextField(20);
t2=new JPasswordField(20);
b1=new JButton("Click Me");
b2=new JButton("Reset");
b3=new JButton("Close The Form");
male=new JRadioButton("Male");
female=new JRadioButton("Female");
btg=new ButtonGroup();
String s1[] = { "Mathura", "Mumbai", "Noida", "Agra", "New Delhi" };
jb1=new JComboBox(s1);
f.getContentPane().setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.getContentPane().setBackground(Color.cyan);
l1.setFont(new Font("Arial",Font.BOLD,20));
l1.setSize(100,20);
l1.setLocation(100, 100);
f.add(l1);
t1.setFont(new Font("Times New Roman",Font.ITALIC,20));
t1.setSize(300,30);
t1.setLocation(200, 100);
f.add(t1);
l2.setFont(new Font("Arial",Font.BOLD,20));
l2.setSize(100,20);
l2.setLocation(100, 150);
f.add(l2);
t2.setSize(300,30);
t2.setLocation(200, 150);
f.add(t2);
l3.setFont(new Font("Arial",Font.BOLD,20));
l3.setSize(100, 20);
l3.setLocation(100, 200);
f.add(l3);
male.setFont(new Font("Arial",Font.BOLD,20));
male.setSelected(true);
male.setSize(80, 20);
male.setLocation(200, 200);
f.add(male);
female.setFont(new Font("Arial",Font.BOLD,20));
female.setSelected(false);
female.setSize(100, 20);
female.setLocation(300 ,200);
f.add(female);
btg.add(male);
btg.add(female);
b1.setFont(new Font("Arial",Font.BOLD,20));
b1.setSize(100,20);
b1.setLocation(100, 250);
f.add(b1);
b2.setFont(new Font("Arial",Font.BOLD,20));
b2.setSize(100 ,20);
b2.setLocation(290, 250);
f.add(b2);
b3.setFont(new Font("Arial",Font.BOLD,20));
b3.setSize(250,20);
b3.setLocation(450, 250);
f.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
t1.addMouseListener(this);
t1.addMouseMotionListener(this);
l4.setFont(new Font("Arial",Font.BOLD,20));
l4.setSize(100,20);
l4.setLocation(100, 300);
f.add(l4);
jb1.setFont(new Font("Arial",Font.BOLD,20));
jb1.setSize(150,50);
jb1.setLocation(200, 300);
f.add(jb1);
label1.setFont(new Font("Arial",Font.BOLD,20));
label1.setSize(200,100);
label1.setLocation(300, 400);
f.add(label1);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==b1)
{
System.out.println("welcome");
d = new JDialog(f, "dialog Box appearing");
d.add(l5);
d.setSize(600, 600);
d.setVisible(true);
}
else
if(e.getSource()==b2)
{
t1.setText("");
t2.setText("");
}
if(e.getSource()==b3)
{
System.exit(0);
}
}catch(Exception ee){System.out.println(ee);}
}
public void mousePressed(MouseEvent e)
{
t1.setText("mouse pressed at point:"+ e.getX() + " " + e.getY());
}
public void mouseReleased(MouseEvent e)
{
t1.setText("mouse released at point:"+ e.getX() + " " + e.getY());
}
public void mouseEntered(MouseEvent e)
{
t1.setText("mouse entered at point:"+ e.getX() + " " + e.getY());
}
public void mouseExited(MouseEvent e)
{
t1.setText("mouse exited at point:"+ e.getX() + " " + e.getY());
}
public void mouseClicked(MouseEvent e)
{
t1.setText("mouse clicked at point:"+ e.getX() + " " + e.getY());
}
public void mouseMoved(MouseEvent e)
{
t1.setText("mouse moved at point:"+ e.getX() + " " + e.getY());
}
public void mouseDragged(MouseEvent e)
{
t1.setText("mouse dragged at point:"+ e.getX() + " " + e.getY());
}
public static void main(String[] aa)
{
MouseListener_Demo fm=new MouseListener_Demo();
}
Example contains Dialog Boxes, Scroll Bars.
menus
Think of a menu as a way to arrange buttons. There are several types.
Dropdown menus hang down from the menu bar at the top of a window.
Popup menus appear when the user clicks, eg with the right mouse button, on a
component that can handle a popup request.
Combo boxes are like menus that always shows one item. Similary, there are lists and
radio buttons.
Dropdown menus: JMenuBar, JMenu, and JMenuItem
A menu bar can be added to the top of any top-level containers, eg, JFrame, JApplet, or JDialog.
Note that a menu bar can not be added to JPanel.
Dropdown menus have three parts:
1. JMenuBar is positioned across the top of a container (eg a JFrame, JPanel, or JApplet).
It's placed above the content pane, so does not use the container's layout. Add menus to
the menubar.
2. JMenu is a vertical list of menu items.
3. JMenuItem and Separators are added to each menu. Menu items are usually strings,
but can also have icons, checkboxes, or hierarchical submenus.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;
class Menu_Demo implements ActionListener, ItemListener
JFrame f;
JLabel l1,l2,l3,l4,l5;
JTextField t1;
JPasswordField t2;
JButton b1,b2,b3;
JComboBox jb1;
JToggleButton tb;
JRadioButton male;
JRadioButton female;
ButtonGroup btg;
JDialog d;
JMenuBar mb;
JMenu m1,m2,m3;
JMenuItem mi1,mi2,mi3,mi4;
public Menu_Demo()
{
f=new JFrame("Registration Form");
l1=new JLabel("Login");
l2=new JLabel("Password");
l3=new JLabel("Gender");
l4=new JLabel("City");
l5=new JLabel("on the dialog box");
t1=new JTextField(20);
t2=new JPasswordField(20);
b1=new JButton("Click Me");
b2=new JButton("Reset");
b3=new JButton("Close The Form");
male=new JRadioButton("Male");
female=new JRadioButton("Female");
btg=new ButtonGroup();
tb=new JToggleButton("Its Toggle Button");
String s1[] = { "Mathura", "Mumbai", "Noida", "Agra", "New Delhi" };
jb1=new JComboBox(s1);
f.getContentPane().setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.getContentPane().setBackground(Color.cyan);
l1.setFont(new Font("Arial",Font.BOLD,20));
l1.setSize(100,20);
l1.setLocation(100, 100);
f.add(l1);
t1.setFont(new Font("Times New Roman",Font.ITALIC,20));
t1.setSize(300,30);
t1.setLocation(200, 100);
f.add(t1);
l2.setFont(new Font("Arial",Font.BOLD,20));
l2.setSize(100,20);
l2.setLocation(100, 150);
f.add(l2);
t2.setSize(300,30);
t2.setLocation(200, 150);
f.add(t2);
l3.setFont(new Font("Arial",Font.BOLD,20));
l3.setSize(100, 20);
l3.setLocation(100, 200);
f.add(l3);
male.setFont(new Font("Arial",Font.BOLD,20));
male.setSelected(true);
male.setSize(80, 20);
male.setLocation(200, 200);
f.add(male);
female.setFont(new Font("Arial",Font.BOLD,20));
female.setSelected(false);
female.setSize(100, 20);
female.setLocation(300 ,200);
f.add(female);
btg.add(male);
btg.add(female);
b1.setFont(new Font("Arial",Font.BOLD,20));
b1.setSize(100,20);
b1.setLocation(100, 250);
f.add(b1);
b2.setFont(new Font("Arial",Font.BOLD,20));
b2.setSize(100 ,20);
b2.setLocation(290, 250);
f.add(b2);
tb.setFont(new Font("Arial",Font.BOLD,20));
tb.setSize(250,20);
tb.setLocation(450, 250);
f.add(tb);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
l4.setFont(new Font("Arial",Font.BOLD,20));
l4.setSize(100,20);
l4.setLocation(100, 300);
f.add(l4);
jb1.setFont(new Font("Arial",Font.BOLD,20));
jb1.setSize(150,50);
jb1.setLocation(200, 300);
f.add(jb1);
mb=new JMenuBar();
m1=new JMenu("File");
m2=new JMenu("Edit");
m3=new JMenu("Format");
mi1=new JMenuItem("New");
mi2=new JMenuItem("Open");
mi3=new JMenuItem("Print");
mi4=new JMenuItem("Close");
mb.add(m1);
mb.add(m2);
mb.add(m3);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
mi4.addActionListener(this);
mi3.addActionListener(this);
tb.addItemListener(this);
f.setJMenuBar(mb);
f.setVisible(true);
public void itemStateChanged(ItemEvent itemEvent)
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED)
System.out.println("Selected");
else
// else print deselected in console
System.out.println("Deselected");
public void actionPerformed(ActionEvent e)
try
if(e.getSource()==b1)
{
System.out.println("welcome");
d = new JDialog(f, "dialog Box appearing");
d.add(l5);
d.setSize(600, 600);
d.setVisible(true);
else
if(e.getSource()==b2)
t1.setText("");
t2.setText("");
if(e.getSource()==b3)
System.exit(0);
if(e.getSource()==mi4)
System.exit(0);
if(e.getSource()==mi3)
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(t1.getPrintable(null,null));
if(pj.printDialog())
try
pj.print();
}catch(Exception ee){System.out.println(ee);}
}catch(Exception ee){System.out.println(ee);}
public static void main(String[] aa)
Menu_Demo fm=new Menu_Demo();