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
Full Source code to create a simple window in Java: using swings
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:
View Comments
Discussion Forum Thread:
https://technotip.com/forums/topic/creating-a-window-in-java-swings/