Creating Simple Window in Java: using swings

This Java video tutorial demonstrates the creation of a simple window by importing Swings package and extending JFrame class.

import javax.swing.*;

javax.swing provides a set of lightweight components that, to the maximum degree possible, works the same on all platforms.

myWindow Class

class myWindow extends JFrame
{
 
}

myWindow is user the name given to the defined class. extends is the keyword. JFrame is a built-in class of swing package.

Constructor

 myWindow()
 { 
setTitle("My window program, using Swings");
setSize(400, 200);
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 }

myWindow() is the constructor, and thus it’s name matches with that of its class name.
setTitle() : To set the string present on the title bar of the window we are creating.
setSize() : To set the size of the window. It takes two parameters, width and height of the window.
setVisible() : It’s very important for all the programs which involves visual components. passing a value of true makes the window visible.

Main method

public static void main(String args[])
{
 // new myWindow();
myWindow m1 = new myWindow();
}

main method just calls the constructor. We can simply write new myWindow() since we are not at all taking the help of any object here.

Video Tutorial: Creating Simple Window in Java: using swings


[youtube https://www.youtube.com/watch?v=7ThtNST8gKE]

YouTube Link: https://www.youtube.com/watch?v=7ThtNST8gKE [Watch the Video In Full Screen.]



Full Source code to create a simple window in Java: using swings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import javax.swing.*;
 
class myWindow extends JFrame
{
myWindow()
{
setTitle("My window program, using Swings");
setSize(400, 200);
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
 
public static void main(String args[])
{
 // new myWindow();
myWindow m1 = new myWindow();
}
}

Output:
window-swing-java