Frame in Java AWT


The class Frame is a top level window with border and title. It uses BorderLayout as default layout manager.

The size of the frame includes any area designated for the border. The dimensions of the border area may be obtained using the getInsets method, however, since these dimensions are platform-dependent, a valid insets value cannot be obtained until the frame is made displayable by either calling pack or show.

Source Code

01_method.java

This is a basic Java program that uses the Abstract Window Toolkit (AWT) to create a graphical user interface (GUI). The program creates a window with a title "Tutor Joes" and sets its size to 1000x600 pixels using the setSize() method. The layout of the window is set to null, which means that the components added to the window will be manually positioned using absolute coordinates.

The program then creates a button labeled "Click Me" using the Button class, and sets its position and size using the setBounds() method. The button is positioned at (75,75) with a width of 200 pixels and a height of 50 pixels.

Finally, the button is added to the frame using the add() method, and the frame is set to be visible using the setVisible() method. When the program is run, a window will be displayed with the button in it, and the user can click the button to perform some action.

package awtDemo;
 
import java.awt.*;
public class app {
 
	public static void main(String[] args) {
		Frame frm =new Frame("Tutor Joes");
		frm.setSize(1000,600);//w,h
		frm.setLayout(null);
 
		Button btn=new Button("Click Me");
		btn.setBounds(75,75,200,50);//X,Y,W,H
 
		frm.add(btn);
		frm.setVisible(true);
	}
}
 

Output

Java AWT

To download raw file Click Here

02_method.java

This is another Java program that uses AWT to create a GUI. This program extends the Frame class and overrides its constructor to create a window with the title "Tutor Joes". The setLayout(null) method sets the layout of the window to null, which means that the components added to the window will be manually positioned using absolute coordinates.

The program creates a button labeled "Click Me" using the Button class, sets its position and size using the setBounds() method, and adds it to the window using the add() method. The setSize(1000,600) method sets the size of the window to 1000x600 pixels, and setVisible(true) makes the window visible on the screen.

The main() method creates an instance of the app class and shows the window on the screen. This program is similar to the previous one, but it uses inheritance to create the window instead of creating a Frame object and setting its properties.

package awtDemo;
import java.awt.*;
 
public class app extends Frame
{
	Button btn;
	public app() 
	{
		super("Tutor Joes");
		setLayout(null);
		btn=new Button("Click Me");
		btn.setBounds(75,75,200,50);//X,Y,W,H
		add(btn);
		setSize(1000,600);
		setVisible(true);
	}
	public static void main(String args[])
	{
		app frm =new app();
	}
}
 

Output

Java AWT

To download raw file Click Here

03_method.java

This is another Java program that uses AWT to create a GUI. This program creates a separate MyApp class that extends the Frame class to create a window with the title "Tutor Joes". The setLayout(null) method sets the layout of the window to null, which means that the components added to the window will be manually positioned using absolute coordinates.

The program creates a button labeled "Click Me" using the Button class, sets its position and size using the setBounds() method, and adds it to the window using the add() method.

The setSize(1000,600) method sets the size of the window to 1000x600 pixels, and setVisible(true) makes the window visible on the screen. The main() method creates an instance of the MyApp class and shows the window on the screen.

package awtDemo;
import java.awt.*;
class MyApp extends Frame
{
	Button btn;
 
	public MyApp()
	{
		super("Tutor Joes");
		setLayout(null);
 
		btn=new Button("Click Me");
		btn.setBounds(75,75,200,50);
		add(btn);
		setSize(1000,600);
		setVisible(true);
 
	}
 
}
 
 
public class app
{
	public static void main(String args[])
	{
		MyApp frm =new MyApp();
	}
To download raw file Click Here

Output

Java AWT


Basic Programs