SlideShare a Scribd company logo
1 of 3
Create a Frame in Java
Introduction
The frame in java works like the main window where your
components (controls) are added to develop a application.
In the Java AWT, top-level windows are represented by
the Frame class. Java supports the look and feel and
decoration for the frame.
The most common method of creating a frame is by using
single argument constructor of the Frame class that
contains the single string argument which is the title of the
window or frame. Then you can add user interface by
constructing and adding different components to the
container one by one.
In this program we are constructing a label to display
"Welcome to RK University" message on the frame. The
center alignment of the label has been defined by the
Label.CENTER. The frame initially invisible, so after
creating the frame it need to visualize the frame by
setVisible(true) method.
add(lbl):
This method has been used to add the label to the frame.
Method add() adds a component to it's container.
setSize                    (width,                height):
This is the method of the Frame class that sets the size of
the frame or window. This method takes two arguments
width (int), height (int).
setVisible(boolean):
This is also a method of the Frame class sets the visibility
of the frame. The frame will be invisible if you pass the
boolean value false otherwise frame will be visible.
Here is the code of the progam :
import java.awt.*;

public class AwtFrame
{
     public static void main(String[] args)
     {
             Frame frm = new Frame("Java AWT Frame");
             Label lbl = new Label("Welcome to RK
                                University",Label.CENTER);
             frm.add(lbl);
             frm.setSize(400,400);
             frm.setVisible(true);
     }
}
Close Frame Window: Program
import java.awt.*;
import java.awt.event.*;

public class AwtCloseButtonEvent
{
         public static void main(String[] args)
     {
              Frame frame = new Frame("Close Operation Frame");
Label lbl = new Label("Welcome to   RK
Univeristy",Label.CENTER);
 frame.add(lbl);
 frame.setSize(400,400);
 frame.setVisible(true);
 frame.addWindowListener(new Windo
wAdapter(){
  public void windowClosing(WindowE
vent we){
  System.exit(0);
  }
  });
    }
}

More Related Content

Similar to Lecture7 oopj

Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3Naga Muruga
 
jframe, jtextarea and jscrollpane
  jframe, jtextarea and jscrollpane  jframe, jtextarea and jscrollpane
jframe, jtextarea and jscrollpaneMr. Akaash
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managersswapnac12
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1PRN USM
 
CodeMash - Building Rich Apps with Groovy SwingBuilder
CodeMash - Building Rich Apps with Groovy SwingBuilderCodeMash - Building Rich Apps with Groovy SwingBuilder
CodeMash - Building Rich Apps with Groovy SwingBuilderAndres Almiray
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعةشرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعةجامعة القدس المفتوحة
 
Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window ToolkitRutvaThakkar1
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 
Creational pattern 2
Creational pattern 2Creational pattern 2
Creational pattern 2Naga Muruga
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1sotlsoc
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpointRicardo Garcia
 

Similar to Lecture7 oopj (20)

Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
jframe, jtextarea and jscrollpane
  jframe, jtextarea and jscrollpane  jframe, jtextarea and jscrollpane
jframe, jtextarea and jscrollpane
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
From Swing to JavaFX
From Swing to JavaFXFrom Swing to JavaFX
From Swing to JavaFX
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
CodeMash - Building Rich Apps with Groovy SwingBuilder
CodeMash - Building Rich Apps with Groovy SwingBuilderCodeMash - Building Rich Apps with Groovy SwingBuilder
CodeMash - Building Rich Apps with Groovy SwingBuilder
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
 
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdfSmart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdf
 
Smart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdfSmart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdf
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعةشرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعة
 
Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window Toolkit
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Creational pattern 2
Creational pattern 2Creational pattern 2
Creational pattern 2
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpoint
 

Lecture7 oopj

  • 1. Create a Frame in Java Introduction The frame in java works like the main window where your components (controls) are added to develop a application. In the Java AWT, top-level windows are represented by the Frame class. Java supports the look and feel and decoration for the frame. The most common method of creating a frame is by using single argument constructor of the Frame class that contains the single string argument which is the title of the window or frame. Then you can add user interface by constructing and adding different components to the container one by one. In this program we are constructing a label to display "Welcome to RK University" message on the frame. The center alignment of the label has been defined by the Label.CENTER. The frame initially invisible, so after creating the frame it need to visualize the frame by setVisible(true) method. add(lbl): This method has been used to add the label to the frame. Method add() adds a component to it's container. setSize (width, height): This is the method of the Frame class that sets the size of the frame or window. This method takes two arguments width (int), height (int). setVisible(boolean): This is also a method of the Frame class sets the visibility
  • 2. of the frame. The frame will be invisible if you pass the boolean value false otherwise frame will be visible. Here is the code of the progam : import java.awt.*; public class AwtFrame { public static void main(String[] args) { Frame frm = new Frame("Java AWT Frame"); Label lbl = new Label("Welcome to RK University",Label.CENTER); frm.add(lbl); frm.setSize(400,400); frm.setVisible(true); } } Close Frame Window: Program import java.awt.*; import java.awt.event.*; public class AwtCloseButtonEvent { public static void main(String[] args) { Frame frame = new Frame("Close Operation Frame");
  • 3. Label lbl = new Label("Welcome to RK Univeristy",Label.CENTER); frame.add(lbl); frame.setSize(400,400); frame.setVisible(true); frame.addWindowListener(new Windo wAdapter(){ public void windowClosing(WindowE vent we){ System.exit(0); } }); } }