frame program in java code example

Example 1: java awt tutorial

import java.awt.*;
public class Example2 {
   Example2()
   {
      //Creating Frame    
      Frame fr=new Frame();       
      
      //Creating a label
      Label lb = new Label("UserId: "); 
      
      //adding label to the frame
      fr.add(lb);           
      
      //Creating Text Field
      TextField t = new TextField();
      
      //adding text field to the frame
      fr.add(t);
      
      //setting frame size
      fr.setSize(500, 300);  
      
      //Setting the layout for the Frame
      fr.setLayout(new FlowLayout());
            
      fr.setVisible(true);                
   }
   public static void main(String args[])
   {
       Example2 ex = new Example2(); 
   }
}

Example 2: java awt tutorial

import java.awt.*;
/* We have extended the Frame class here,
 * thus our class "SimpleExample" would behave
 * like a Frame
 */
public class SimpleExample extends Frame{
    SimpleExample(){  
        Button b=new Button("Button!!"); 
        
        // setting button position on screen
        b.setBounds(50,50,50,50);  
        
        //adding button into frame 
        add(b); 
        
        //Setting Frame width and height
        setSize(500,300); 
        
        //Setting the title of Frame
        setTitle("This is my First AWT example"); 
        
        //Setting the layout for the Frame
        setLayout(new FlowLayout());
        
        /* By default frame is not visible so 
         * we are setting the visibility to true 
         * to make it visible.
         */
        setVisible(true);  
    }  
    public static void main(String args[]){  
         // Creating the instance of Frame
         SimpleExample fr=new SimpleExample();  
    }
}

Tags:

Java Example