Posted By:
21 Feb, 2014 8:47 am PST

Creating Frames in Java

How to create a frame by creating an object to frame class?

// creating a frame

import java.awt.*;

class MyFrame{

       public static void main (String args[])

       {

              //create a frame

              Frame f = new Frame("My AWT frame");

              //set the size of the frame

              f.setSize(300,250);

              //display the frame

              f.setVisible(true);

       }

}

OUTPUT:

Note:The frame above can be maximised/minimised but cannot be closed. Therefore to close the Frame window, we need to use the WindowListener Interface.

How to create a frame which is closed by clicking on the close button?

import java.awt.*;

import java.awt.event.*;

public class CloseFrame {

       public static void main(String []args)

       {

       Frame s = new Frame("My AWT frame");

       s.setSize(300,250);

       s.setVisible(true);

       //close the frame

       s.addWindowListener(new MyClass());

       }

}

class MyClass implements WindowListener{

       public void windowActivated(WindowEvent e){}

       public void windowClosed(WindowEvent e){}

       public void windowClosing(WindowEvent e)

       {

              //closing the application

              System.exit(0);

       }

       public void windowDeactivated(WindowEvent e){}

       public void windowDeiconified(WindowEvent e){}

       public void windowIconified(WindowEvent e){}

       public void windowOpened(WindowEvent e){}

}

Develop the code to close the frame using an anonymous inner class.

import java.awt.*;

import java.awt.event.*;

public class CloseFrame2 {

       public static void main(String[] args) {

Frame f = new Frame("Title of the Frame");

f.setSize(600,400);;

f.setVisible(true);

f.addWindowListener(new WindowAdapter(){

       publicvoid windowClosing(WindowEvent e){

              System.exit(0);

       }

});

        }

}

OUTPUT:

 

 

Attachment: workspace.zip


Description:

Java.awt : Contains all of the classes for creating user interfaces and for painting graphics and images.

Frame class : A Frame is a top-level window with a title and a border.

Syntax:
public class Frame

extends Window

implements MenuContainer

WindowListener Interface: The listener interface for receiving window events. The class that is interested in processing a window event either implements this interface (and all the methods it contains) or extends the abstract WindowAdapter class (overriding only the methods of interest). The listener object created from that class is then registered with a Window using the window's addWindowListener method. When the window's status changes by virtue of being opened, closed, activated or deactivated, iconified or deiconified, the relevant method in the listener object is invoked, and the WindowEvent is passed to it.

 

Attachment: Frames.zip
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud