Example : An AWT GUI program in java to close the AWT window.
import java.awt.*;
import java.awt.event.*;

class CloseWindow
{
  public static void main(String[] args)
  {
    Frame fr = new Frame("AWT Window Close Operation");
    Label lbl = new Label("Welcome U All in Codershelpline",Label.CENTER);
    fr.add(lbl);
    fr.setSize(400,400);
    fr.setVisible(true);
    fr.addWindowListener(new WindowAdapter()
     {
       public void windowClosing(WindowEvent we)
         {
           System.exit(0);
         }
     });
  }
}
-----------  OR  -----------

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

class AWTFrame extends Frame
{
	Button b1;
	
     AWTFrame()
      {
	setTitle("How to Close an AWT Window");
	setSize(400,400);
	setLayout(new FlowLayout());
	setVisible(true); 
 
	b1=new Button("Submit");  //Optional Button Creation
	add(b1);
	
	addWindowListener(new WindowAdapter()
          {
		public void windowClosing(WindowEvent we)
		{
		 System.exit(0);
		}
	  });
       }

    public static void main(String args[])
     {
	new AWTFrame();
     }
}
Example : An AWT GUI program in java to show the mouse click location(coordinates point) value from the frame.
import java.awt.*;
import java.awt.event.*;

class ClickLocation
{
	public static void main (String[] args)
	{
	   Frame frm = new Frame("Mouse Event Listener Program");
	   frm.setSize(500,500);   //Size of Frame.
	   frm.setVisible(true);   // To make Frame visible.
	   frm.addMouseListener(new MouseAdapter()
	      {
	         public void mouseClicked(MouseEvent e)
	          {
		    System.out.println("Mouse clicked at location : ("+e.getX()+","+e.getY()+")");
	          }
	       }
	    );
	   frm.addWindowListener(new WindowAdapter() 
	       {
                  public void windowClosing(WindowEvent we) 
		    {
                        System.exit(0);					
		    }
               });
	}	
}
Example : An AWT GUI program in java to create a button & text field and display button event on frame.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

class ButtonEffect implements ActionListener 
{    
    Button btn;
    TextField tf;
    Frame fr;

    ButtonEffect(String s) 
      {
           fr = new Frame(s);
           btn = new Button("OK");

           tf = new TextField(12);
           fr.setSize(400, 350);
           fr.setVisible(true);
           btn.addActionListener(this);

           fr.add(tf);
           fr.add(btn);

           fr.addWindowListener(new WindowAdapter() 
	   {
             public void windowClosing(WindowEvent we) 
	      {
                System.exit(0);		
              }
           });

           fr.setLayout(new FlowLayout());
       }

    public void actionPerformed(ActionEvent e) 
	{
        if (e.getSource() == btn) 
	    {
            tf.setText("Ok button is Pressed");
            }
        }
    public static void main(String args[]) 
    {
        new ButtonEffect("Button Action Performed");
    }
}

 501 total views,  1 views today

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.