AWT Toolkit

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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 Title Bar 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