KEMBAR78
Abstract Window Toolkit_Event Handling_python | PPTX
AWT Classes
AWT Classes
• The classes and interfaces of the AWT are
used to develop stand alone applications and
to implement the GUI controls used by
Applets.
Window Fundamentals
• The AWT defines windows according to a class
hierarchy that adds functionality and
specificity with each level.
• The two most common windows are:
1. Panel, which is used by applets
2. Derived from Frame, which creates a
standard application window.
Frame
• Subclass of window and has a title bar,
menubar, borders and resizing corners.
• Creating Frame:
Frame f1=new Frame();
Frame f2=new Frame(“New Example”);
Frame: Setting Window’s Dimensions
void setSize(int width, int height)
void setSize(Dimension size)
Frame: Hiding and Showing
Syntax: void setVisible(boolean)
Example: void setVisible(true);
Frame: Setting Windows Title
Syntax:void setTitle(String title)
Example: void setTitle(New Example);
Closing the Frame Window
void windowClosing();
Write a program to display a frame with a button. The caption of the button should be “Change Color”. For every click of the button, the background color of the
frame should change randomly.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class ButtonFrame extends Frame
implements ActionListener
{
Button b;
Random rand;
Color c;
public ButtonFrame()
{
addWindowListener(new MyWindowAdapter());
b=new Button("Change Color");
b.addActionListener(this);
add(b);
rand=new Random();
}
public void paint(Graphics g)
{ }
public static void main(String args[])
{
ButtonFrame ob= new ButtonFrame();
ob.setSize(new Dimension(300, 200));
ob.setTitle("An AWT-Based Application--Buttons in
frame");
ob.setLayout(new FlowLayout());
ob.setVisible(true);
}
public void actionPerformed(ActionEvent ie)
{
//repaint();
float r=rand.nextFloat();
float gr=rand.nextFloat();
float b=rand.nextFloat();
c=new Color(r,gr,b);
setBackground(c);
}
}
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
Layout Manager
• The Layout Managers are used to arrange
controls in a particular manner.
• Layout Manager is an interface that is
implemented by all the classes of layout
managers.
Types of Layout Manager
• Flow Layout
• Grid Layout
• Border Layout
• CardLayout
Types of Layout Manager
• Flow Layout
• Grid Layout
• Border Layout
• CardLayout
Fields of FlowLayout
• public static final int LEFT
• public static final int RIGHT
• public static final int CENTER
• public static final int LEADING
• public static final int TRAILING
Flow Layout
• 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.
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.
Flow Layout - Example
import java.awt.*;
public class MyFlowLayout
{
Frame f=new Frame();
MyFlowLayout()
{
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("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)
{
MyFlowLayout= new MyFlowLayout();
} }
Flow Layout - Example
GridLayout
• The GridLayout is used to arrange the components in
rectangular grid.
• 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.
GridLayout
import java.awt.*;
public class MyGridLayout
{
Frame ff=new Frame();;
MyGridLayout(){
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9=new Button("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));
//setting grid layout of 3 rows and 3 columns
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
MyGridLayout= new MyGridLayout();
} }
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
Constructors - BorderLayout
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER
Constructors of BorderLayout class:
BorderLayout(): creates a border layout but with no gaps
between the components.
BorderLayout(int hgap, int vgap): creates a border
layout with the given horizontal and vertical gaps
between the components.
Example - BorderLayout
import java.awt.*;
import javax.swing.*;
public class Border {
Frame f;
Border()
{
f=new Frame();
Button b1=new Button("NORTH");;
Button b2=new Button("SOUTH");;
Button b3=new Button("EAST");;
Button b4=new Button("WEST");;
Button b5=new Button("CENTER");;
BorderLayout
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();
}
}
BorderLayout
Control Fundamentals
• Button
• Label
• CheckBox
• List
• MenuBar
AWT Button Example with ActionListener
import java.awt.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f=new Frame("Button Example");
TextField tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400); > javac ButtonExample.java
>java ButtonExample
f.setLayout(null);
f.setVisible(true);
}
}
AWT TextField Example with ActionListener
import java.awt.*;
import java.awt.event.*;
public class TextFieldExample extends Frame implements ActionListener{
TextField tf1,tf2,tf3;
Button b1,b2;
TextFieldExample(){
tf1=new TextField();
tf1.setBounds(50,50,150,20);
tf2=new TextField();
tf2.setBounds(50,100,150,20);
tf3=new TextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new Button("+");
b1.setBounds(50,200,50,50);
b2=new Button("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText(); s1=“10” =10
String s2=tf2.getText(); s2=“5” =5
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
}
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args) {
new TextFieldExample();
}
}
TextArea
import java.awt.*;
import java.awt.event.*;
public class TextAreaExample extends Frame implements ActionListener{
Label l1,l2;
TextArea area;
Button b;
TextAreaExample(){
l1=new Label();
l1.setBounds(50,50,100,30);
l2=new Label();
l2.setBounds(160,50,100,30);
area=new TextArea();
area.setBounds(20,100,300,300);
b=new Button("Count Words");
b.setBounds(100,400,100,30);
b.addActionListener(this);
add(l1);add(l2);add(area);add(b);
setSize(400,450);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String text=area.getText();
String words[]=text.split("s");
l1.setText("Words: "+words.length);
l2.setText("Characters: "+text.length());
}
public static void main(String[] args) {
new TextAreaExample();
}
}
Choice Example with ActionListener
import java.awt.*;
import java.awt.event.*;
public class ChoiceExample
{ ChoiceExample(){
Frame f= new Frame();
Label label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
Button b=new Button("Show");
b.setBounds(200,100,50,20);
Choice c=new Choice();
c.setBounds(100,100, 75,75);
c.add("C");
c.add("C++");
c.add("Java");
c.add("PHP");
c.add("Android");
f.add(c);f.add(label); f.add(b);
Choice Example with ActionListener
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
String data = "Programming language Selected: "+
c.getItem(c.getSelectedIndex());
label.setText(data);
}
});
}
public static void main(String args[])
{
new ChoiceExample();
}
}
Java AWT List Example with ActionListener
import java.awt.*;
import java.awt.event.*;
public class ListExample
{ ListExample(){
Frame f= new Frame();
Label label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(500,100);
Button b=new Button("Show");
b.setBounds(200,150,80,30);
List l1=new List(4, false);
l1.setBounds(100,100, 70,70);
l1.add("C");
l1.add("C++");
l1.add("Java");
l1.add("PHP");
final List l2=new List(4, true);
l2.setBounds(100,200, 70,70);
l2.add("Turbo C++");
l2.add("Spring");
l2.add("Hibernate");
Java AWT List Example with ActionListener
l2.add("CodeIgniter");
f.add(l1); f.add(l2); f.add(label); f.add(b);
f.setSize(450,450);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "Programming language Selected: "+l1.getItem(l1.getSelectedIndex());
data += ", Framework Selected:";
for(String frame:l2.getSelectedItems()){
data += frame + " ";
}
label.setText(data);
}
});
}
public static void main(String args[])
{
new ListExample();
} }
Java AWT MenuItem and Menu
import java.awt.*;
class MenuExample
{
MenuExample(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu m=new Menu("Menu");
Menu sm=new Menu("Sub Menu");
MenuItem i1=new MenuItem("Item 1");
MenuItem i2=new MenuItem("Item 2");
MenuItem i3=new MenuItem("Item 3");
MenuItem i4=new MenuItem("Item 4");
MenuItem i5=new MenuItem("Item 5");
m.add(i1);
m.add(i2);
m.add(i3);
sm.add(i4);
sm.add(i5);
Java AWT MenuItem and Menu
m.add(sm);
mb.add(m);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
MenuExample me= new MenuExample();
}
}
Java AWT Dialog
• The Dialog control represents a top level
window with a border and a title used to take
some form of input from the user. It inherits
the Window class.
• Unlike Frame, it doesn't have maximize and
minimize buttons.
Java AWT Dialog
import java.awt.*;
import java.awt.event.*;
public class DialogExample {
private static Dialog d;
DialogExample() {
Frame f= new Frame();
d = new Dialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
Button b = new Button ("OK");
b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogExample.d.setVisible(false);
}
});
Java AWT Dialog
d.add( new Label ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}
FileNameFilter - Interface
import java.io.*;
• FileNameFilter interface has method
• boolean accept(File dir, String name)
• Should be implemented and every file is
tested for this method to be included in the
file list.
Java Swings
 Java Swing is a lightweight Graphical User
Interface (GUI) toolkit that includes a rich set
of widgets.
It includes package to create GUI components
for your Java applications
It is platform independent.
Java Swings
 Swing API is a set of extensible GUI
Components to ease the developer's life to
create JAVA based Front End/GUI Applications.
Swing component follows a Model-View-
Controller architecture to fulfill the following
criteria.
 A single API is to be sufficient to support
multiple look and feel.
• Unlike AWT, Java Swing provides platform-
independent and lightweight components.
• The javax.swing package provides classes for java
swing API such as JButton, JTextField, JTextArea,
JRadioButton, JCheckbox, JMenu, JColorChooser
etc.
import javax.swing.*;
Java Swings
Java Swings- Features
Light Weight
Rich Controls - Swing provides a rich set of
advanced controls like Tree, TabbedPane, slider,
colorpicker, and table controls.
Highly Customizable - Swing controls can be
customized in a very easy way as visual
apperance is independent of internal
representation.
Pluggable look-and-feel - SWING based GUI
Application look and feel can be changed at run-
time, based on available values.
MVC - Model View Controller
Abstract Window Toolkit_Event Handling_python

Abstract Window Toolkit_Event Handling_python

  • 1.
  • 2.
    AWT Classes • Theclasses and interfaces of the AWT are used to develop stand alone applications and to implement the GUI controls used by Applets.
  • 3.
    Window Fundamentals • TheAWT defines windows according to a class hierarchy that adds functionality and specificity with each level. • The two most common windows are: 1. Panel, which is used by applets 2. Derived from Frame, which creates a standard application window.
  • 4.
    Frame • Subclass ofwindow and has a title bar, menubar, borders and resizing corners. • Creating Frame: Frame f1=new Frame(); Frame f2=new Frame(“New Example”);
  • 5.
    Frame: Setting Window’sDimensions void setSize(int width, int height) void setSize(Dimension size)
  • 6.
    Frame: Hiding andShowing Syntax: void setVisible(boolean) Example: void setVisible(true); Frame: Setting Windows Title Syntax:void setTitle(String title) Example: void setTitle(New Example);
  • 7.
    Closing the FrameWindow void windowClosing();
  • 8.
    Write a programto display a frame with a button. The caption of the button should be “Change Color”. For every click of the button, the background color of the frame should change randomly. import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; public class ButtonFrame extends Frame implements ActionListener { Button b; Random rand; Color c;
  • 9.
    public ButtonFrame() { addWindowListener(new MyWindowAdapter()); b=newButton("Change Color"); b.addActionListener(this); add(b); rand=new Random(); } public void paint(Graphics g) { }
  • 10.
    public static voidmain(String args[]) { ButtonFrame ob= new ButtonFrame(); ob.setSize(new Dimension(300, 200)); ob.setTitle("An AWT-Based Application--Buttons in frame"); ob.setLayout(new FlowLayout()); ob.setVisible(true); }
  • 11.
    public void actionPerformed(ActionEventie) { //repaint(); float r=rand.nextFloat(); float gr=rand.nextFloat(); float b=rand.nextFloat(); c=new Color(r,gr,b); setBackground(c); } }
  • 12.
    class MyWindowAdapter extendsWindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } }
  • 13.
    Layout Manager • TheLayout Managers are used to arrange controls in a particular manner. • Layout Manager is an interface that is implemented by all the classes of layout managers.
  • 14.
    Types of LayoutManager • Flow Layout • Grid Layout • Border Layout • CardLayout
  • 15.
    Types of LayoutManager • Flow Layout • Grid Layout • Border Layout • CardLayout
  • 16.
    Fields of FlowLayout •public static final int LEFT • public static final int RIGHT • public static final int CENTER • public static final int LEADING • public static final int TRAILING
  • 17.
    Flow Layout • TheFlowLayout is used to arrange the components in a line, one after another (in a flow). • It is the default layout of applet or panel. 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.
  • 18.
    Flow Layout -Example import java.awt.*; public class MyFlowLayout { Frame f=new Frame(); MyFlowLayout() { Button b1=new Button("1"); Button b2=new Button("2"); Button b3=new Button("3"); Button b4=new Button("4"); Button b5=new Button("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) { MyFlowLayout= new MyFlowLayout(); } }
  • 19.
  • 20.
    GridLayout • The GridLayoutis used to arrange the components in rectangular grid. • 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.
  • 21.
    GridLayout import java.awt.*; public classMyGridLayout { Frame ff=new Frame();; MyGridLayout(){ Button b1=new Button("1"); Button b2=new Button("2"); Button b3=new Button("3"); Button b4=new Button("4"); Button b5=new Button("5"); Button b6=new Button("6"); Button b7=new Button("7"); Button b8=new Button("8"); Button b9=new Button("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)); //setting grid layout of 3 rows and 3 columns f.setSize(300,300); f.setVisible(true); } public static void main(String[] args) { MyGridLayout= new MyGridLayout(); } }
  • 23.
    BorderLayout • The BorderLayoutis 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
  • 24.
    Constructors - BorderLayout publicstatic final int NORTH public static final int SOUTH public static final int EAST public static final int WEST public static final int CENTER Constructors of BorderLayout class: BorderLayout(): creates a border layout but with no gaps between the components. BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and vertical gaps between the components.
  • 25.
    Example - BorderLayout importjava.awt.*; import javax.swing.*; public class Border { Frame f; Border() { f=new Frame(); Button b1=new Button("NORTH");; Button b2=new Button("SOUTH");; Button b3=new Button("EAST");; Button b4=new Button("WEST");; Button b5=new Button("CENTER");;
  • 26.
  • 27.
  • 28.
    Control Fundamentals • Button •Label • CheckBox • List • MenuBar
  • 29.
    AWT Button Examplewith ActionListener import java.awt.*; import java.awt.event.*; public class ButtonExample { public static void main(String[] args) { Frame f=new Frame("Button Example"); TextField tf=new TextField(); tf.setBounds(50,50, 150,20); Button b=new Button("Click Here"); b.setBounds(50,100,60,30); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ tf.setText("Welcome to Javatpoint."); } }); f.add(b);f.add(tf); f.setSize(400,400); > javac ButtonExample.java >java ButtonExample f.setLayout(null); f.setVisible(true); } }
  • 30.
    AWT TextField Examplewith ActionListener import java.awt.*; import java.awt.event.*; public class TextFieldExample extends Frame implements ActionListener{ TextField tf1,tf2,tf3; Button b1,b2; TextFieldExample(){ tf1=new TextField(); tf1.setBounds(50,50,150,20); tf2=new TextField(); tf2.setBounds(50,100,150,20); tf3=new TextField(); tf3.setBounds(50,150,150,20); tf3.setEditable(false); b1=new Button("+"); b1.setBounds(50,200,50,50); b2=new Button("-"); b2.setBounds(120,200,50,50); b1.addActionListener(this); b2.addActionListener(this);
  • 31.
    add(tf1);add(tf2);add(tf3);add(b1);add(b2); setSize(300,300); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvente) { String s1=tf1.getText(); s1=“10” =10 String s2=tf2.getText(); s2=“5” =5 int a=Integer.parseInt(s1); int b=Integer.parseInt(s2); int c=0; if(e.getSource()==b1){ c=a+b; }else if(e.getSource()==b2){ c=a-b; } String result=String.valueOf(c); tf3.setText(result); } public static void main(String[] args) { new TextFieldExample(); } }
  • 32.
    TextArea import java.awt.*; import java.awt.event.*; publicclass TextAreaExample extends Frame implements ActionListener{ Label l1,l2; TextArea area; Button b; TextAreaExample(){ l1=new Label(); l1.setBounds(50,50,100,30); l2=new Label(); l2.setBounds(160,50,100,30); area=new TextArea(); area.setBounds(20,100,300,300); b=new Button("Count Words"); b.setBounds(100,400,100,30); b.addActionListener(this); add(l1);add(l2);add(area);add(b); setSize(400,450); setLayout(null); setVisible(true); }
  • 33.
    public void actionPerformed(ActionEvente){ String text=area.getText(); String words[]=text.split("s"); l1.setText("Words: "+words.length); l2.setText("Characters: "+text.length()); } public static void main(String[] args) { new TextAreaExample(); } }
  • 34.
    Choice Example withActionListener import java.awt.*; import java.awt.event.*; public class ChoiceExample { ChoiceExample(){ Frame f= new Frame(); Label label = new Label(); label.setAlignment(Label.CENTER); label.setSize(400,100); Button b=new Button("Show"); b.setBounds(200,100,50,20); Choice c=new Choice(); c.setBounds(100,100, 75,75); c.add("C"); c.add("C++"); c.add("Java"); c.add("PHP"); c.add("Android"); f.add(c);f.add(label); f.add(b);
  • 35.
    Choice Example withActionListener f.setSize(400,400); f.setLayout(null); f.setVisible(true); b.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex()); label.setText(data); } }); } public static void main(String args[]) { new ChoiceExample(); } }
  • 36.
    Java AWT ListExample with ActionListener import java.awt.*; import java.awt.event.*; public class ListExample { ListExample(){ Frame f= new Frame(); Label label = new Label(); label.setAlignment(Label.CENTER); label.setSize(500,100); Button b=new Button("Show"); b.setBounds(200,150,80,30); List l1=new List(4, false); l1.setBounds(100,100, 70,70); l1.add("C"); l1.add("C++"); l1.add("Java"); l1.add("PHP"); final List l2=new List(4, true); l2.setBounds(100,200, 70,70); l2.add("Turbo C++"); l2.add("Spring"); l2.add("Hibernate");
  • 37.
    Java AWT ListExample with ActionListener l2.add("CodeIgniter"); f.add(l1); f.add(l2); f.add(label); f.add(b); f.setSize(450,450); f.setLayout(null); f.setVisible(true); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String data = "Programming language Selected: "+l1.getItem(l1.getSelectedIndex()); data += ", Framework Selected:"; for(String frame:l2.getSelectedItems()){ data += frame + " "; } label.setText(data); } }); } public static void main(String args[]) { new ListExample(); } }
  • 38.
    Java AWT MenuItemand Menu import java.awt.*; class MenuExample { MenuExample(){ Frame f= new Frame("Menu and MenuItem Example"); MenuBar mb=new MenuBar(); Menu m=new Menu("Menu"); Menu sm=new Menu("Sub Menu"); MenuItem i1=new MenuItem("Item 1"); MenuItem i2=new MenuItem("Item 2"); MenuItem i3=new MenuItem("Item 3"); MenuItem i4=new MenuItem("Item 4"); MenuItem i5=new MenuItem("Item 5"); m.add(i1); m.add(i2); m.add(i3); sm.add(i4); sm.add(i5);
  • 39.
    Java AWT MenuItemand Menu m.add(sm); mb.add(m); f.setMenuBar(mb); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } public static void main(String args[]) { MenuExample me= new MenuExample(); } }
  • 40.
    Java AWT Dialog •The Dialog control represents a top level window with a border and a title used to take some form of input from the user. It inherits the Window class. • Unlike Frame, it doesn't have maximize and minimize buttons.
  • 41.
    Java AWT Dialog importjava.awt.*; import java.awt.event.*; public class DialogExample { private static Dialog d; DialogExample() { Frame f= new Frame(); d = new Dialog(f , "Dialog Example", true); d.setLayout( new FlowLayout() ); Button b = new Button ("OK"); b.addActionListener ( new ActionListener() { public void actionPerformed( ActionEvent e ) { DialogExample.d.setVisible(false); } });
  • 42.
    Java AWT Dialog d.add(new Label ("Click button to continue.")); d.add(b); d.setSize(300,300); d.setVisible(true); } public static void main(String args[]) { new DialogExample(); } }
  • 43.
    FileNameFilter - Interface importjava.io.*; • FileNameFilter interface has method • boolean accept(File dir, String name) • Should be implemented and every file is tested for this method to be included in the file list.
  • 44.
    Java Swings  JavaSwing is a lightweight Graphical User Interface (GUI) toolkit that includes a rich set of widgets. It includes package to create GUI components for your Java applications It is platform independent.
  • 45.
    Java Swings  SwingAPI is a set of extensible GUI Components to ease the developer's life to create JAVA based Front End/GUI Applications. Swing component follows a Model-View- Controller architecture to fulfill the following criteria.  A single API is to be sufficient to support multiple look and feel.
  • 46.
    • Unlike AWT,Java Swing provides platform- independent and lightweight components. • The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc. import javax.swing.*; Java Swings
  • 47.
    Java Swings- Features LightWeight Rich Controls - Swing provides a rich set of advanced controls like Tree, TabbedPane, slider, colorpicker, and table controls. Highly Customizable - Swing controls can be customized in a very easy way as visual apperance is independent of internal representation. Pluggable look-and-feel - SWING based GUI Application look and feel can be changed at run- time, based on available values.
  • 50.
    MVC - ModelView Controller