Skip to main content

JAVA AWT: Panel, Dialog and ToolKit


Java AWT Panel

The Panel is a simplest container class. It provides space in which an application can attach any other component. It inherits the Container class.

It doesn't have title bar.

AWT Panel class declaration

  1. public class Panel extends Container implements Accessible  

Java AWT Panel Example

  1. import java.awt.*;  
  2. public class PanelExample {  
  3.      PanelExample()  
  4.         {  
  5.         Frame f= new Frame("Panel Example");    
  6.         Panel panel=new Panel();  
  7.         panel.setBounds(40,80,200,200);    
  8.         panel.setBackground(Color.gray);  
  9.         Button b1=new Button("Button 1");     
  10.         b1.setBounds(50,100,80,30);    
  11.         b1.setBackground(Color.yellow);   
  12.         Button b2=new Button("Button 2");   
  13.         b2.setBounds(100,100,80,30);    
  14.         b2.setBackground(Color.green);   
  15.         panel.add(b1); panel.add(b2);  
  16.         f.add(panel);  
  17.         f.setSize(400,400);    
  18.         f.setLayout(null);    
  19.         f.setVisible(true);    
  20.         }  
  21.         public static void main(String args[])  
  22.         {  
  23.         new PanelExample();  
  24.         }  
  25. }  

Output:

java awt Panel example 1

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.

Frame vs Dialog

Frame and Dialog both inherits Window class. Frame has maximize and minimize buttons but Dialog doesn't have.

AWT Dialog class declaration

  1. public class Dialog extends Window  

Java AWT Dialog Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class DialogExample {  
  4.     private static Dialog d;  
  5.     DialogExample() {  
  6.         Frame f= new Frame();  
  7.         d = new Dialog(f , "Dialog Example"true);  
  8.         d.setLayout( new FlowLayout() );  
  9.         Button b = new Button ("OK");  
  10.         b.addActionListener ( new ActionListener()  
  11.         {  
  12.             public void actionPerformed( ActionEvent e )  
  13.             {  
  14.                 DialogExample.d.setVisible(false);  
  15.             }  
  16.         });  
  17.         d.add( new Label ("Click button to continue."));  
  18.         d.add(b);   
  19.         d.setSize(300,300);    
  20.         d.setVisible(true);  
  21.     }  
  22.     public static void main(String args[])  
  23.     {  
  24.         new DialogExample();  
  25.     }  
  26. }  

Output:

java awt Dialog example 1


Java AWT Toolkit

Toolkit class is the abstract superclass of every implementation in the Abstract Window Toolkit. Subclasses of Toolkit are used to bind various components. It inherits Object class.

AWT Toolkit class declaration

  1. public abstract class Toolkit extends Object  

Java AWT Toolkit Example

  1. import java.awt.*;  
  2. public class ToolkitExample {  
  3.   public static void main(String[] args) {  
  4.       Toolkit t = Toolkit.getDefaultToolkit();  
  5.       System.out.println("Screen resolution = " + t.getScreenResolution());  
  6.       Dimension d = t.getScreenSize();  
  7.       System.out.println("Screen width = " + d.width);  
  8.       System.out.println("Screen height = " + d.height);  
  9.   }  
  10. }  

Output:

Screen resolution = 96
Screen width = 1366
Screen height = 768

Java AWT Toolkit Example: beep()

  1. import java.awt.event.*;  
  2. public class ToolkitExample {  
  3. public static void main(String[] args) {  
  4.     Frame f=new Frame("ToolkitExample");  
  5.     Button b=new Button("beep");  
  6.     b.setBounds(50,100,60,30);  
  7.     f.add(b);     
  8.     f.setSize(300,300);  
  9.     f.setLayout(null);  
  10.     f.setVisible(true);  
  11.     b.addActionListener(new ActionListener(){  
  12.         public void actionPerformed(ActionEvent e){  
  13.             Toolkit.getDefaultToolkit().beep();  
  14.         }  
  15.     });       
  16. }  
  17. }  

Output:

java awt toolkit example 1

Java AWT Toolkit Example: Change TitleBar Icon

  1. import java.awt.*;   
  2. class ToolkitExample {   
  3. ToolkitExample(){   
  4. Frame f=new Frame();   
  5. Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");  
  6. f.setIconImage(icon);  
  7. f.setLayout(null);   
  8. f.setSize(400,400);   
  9. f.setVisible(true);   
  10. }   
  11. public static void main(String args[]){   
  12. new ToolkitExample();   
  13. }   
  14. }  

Output:

java awt toolkit example 2

Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&