Powered by Blogger.

Java-Using Abstract Windowing Toolkit (AWT) In Java Applications

Abstract Window Toolkit:

AWT is the basic GUI (Graphics User Interface) used for Java applications and applets. Every GUI component that appears on the screen is a subclass of the abstract class Component or Menu Component. The class Container is an abstract subclass of Component class, which permits other components to be nested inside it. There are two types of containers: Window and Panel. A Window is a free-standing native window on the display that is independent of other containers. There are two important types of Window containers: Frame and Dialog. A Frame is a window with a title and corners that you can resize. A Dialog is a simple window and cannot have a menu bar, you cannot resize it, but you can move it. A Panel must be contained within another Container, or inside a web browser’s window.

Abstract Window Toolkit Example_1:

 import java.awt.*;
 import java.awt.event.*;

 public class FrameWithPanel implements WindowListener{
    private Frame f;
    private Panel p;

    public FrameWithPanel(){
       f = new Frame(”Frame Title”);
       p = new Panel();
    }

    public void launchFrame(){
       f.addWindowListener(this);
       f.setSize(400, 400);
       f.setBackground(Color.red);
       f.setLayout(null);

       p.setSize(150, 150);
       p.setBackground(Color.green);
       f.add(p);
       f.setVisible(true);
   }

   public void windowClosing(WindowEvent e){
      System.exit(0);
   }

   public void windowOpened(WindowEvent e){}
   public void windowIconified(WindowEvent e){}
   public void windowDeiconified(WindowEvent e){}
   public void windowClosed(WindowEvent e){}
   public void windowActivated(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}

   public static void main(String[]args){
      FrameWithPanel fp = new FrameWithPanel();
      fp.launchFrame();
   }
 }

Example 2:

 import java.awt.*;
 import java.awt.event.*;

 public class TestMenuBar implements WindowListener, ActionListener{
    private Frame f;
    private MenuBar mb;
    private Menu m1, m2, m3;
    private MenuItem mi1, mi2, mi3, mi4;

    public TestMenuBar(){
       f = new Frame(”MenuBar Example”);
       mb = new MenuBar();
       m1 = new Menu(”File”);
       m2 = new Menu(”Edit”);
       m3 = new Menu(”Help”);
       mi1 = new MenuItem(”New”);
       mi2 = new MenuItem(”Save”);
       mi3 = new MenuItem(”Load”);
       mi4 = new MenuItem(”Quit”);
   }

   public void launchFrame(){
      mi4.addActionListener(this);
      m1.add(mi1);
      m1.add(mi2);
      m1.add(mi3);
      m1.addSeparator();
      m1.add(mi4);

      mb.add(m1);
      mb.add(m2);
      mb.setHelpMenu(m3);
      f.setMenuBar(mb);

      f.addWindowListener(this);
      f.setSize(400, 400);
      f.setBackground(Color.red);
      f.setLayout(null);
      f.setVisible(true);
   }

   public void actionPerformed(ActionEvent e){
      System.exit(0);
   }

   public void windowClosing(WindowEvent e){
      System.exit(0);
   }

   public void windowOpened(WindowEvent e){}
   public void windowIconified(WindowEvent e){}
   public void windowDeiconi fied(WindowEvent e){}
   public void windowClosed(WindowEvent e){}
   public void windowActivated(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}

   public static void main(String[]args){
      TestMenuBar tmb = new TestMenuBar();
      tmb.launchFrame();
   }
 }


References:

1. Fowler, Amy (1994). "Mixing heavy and light components". Sun Microsystems. Retrieved 17 December 2008.

2. Geertjan, Wielenga (16 December 2008). "A Farewell to Heavyweight/Lightweight Conflicts". Retrieved 17 December 2008.

3. "Bug/RFE fixed in current JDK 6u12 build". Sun Micro systems. 12 December 2008. Retrieved 17 December 2008.

4. Torre, Mario (2 March 2008). "FINAL PROPOSAL: Portable GUI backends". Retrieved 7 September 2008.

5. Kennke, Roman (18 December 2008). "Caciocavallo Architecture Overview". Retrieved 7 September 2008.

6. Kennke, Roman (3 September 2008). "Cacio Swing AWT peers". Retrieved 7 September 2008.

7. "How much has been pushed upstream?". openjdk.java.net. 20 September 2009. Retrieved 7 March 2010. "You don't need anymore those patches, with the latest FontManager push, everything is upstream now, so just use the Cacio repo, it's completely self contained."

8. a b Kennke, Roman (28 July 2011). "JDK7 and Cacio coolness". Retrieved 8 August 2011.

9. Eisserer, Clemens. "HTML5/Canvas backend for Caciocavallo (GNU-Classpath)". Archived from the original on 10 August 2011. Retrieved 8 August 2011.

1 comment: