10 Best Java Training Book
Updated on: September 2023
Best Java Training Book in 2023
Core Java Volume I--Fundamentals (11th Edition) (Core Series)
Murach's Java Servlets and JSP, 3rd Edition (Murach: Training & Reference)
Effective Java
Sql Guide (Quick Study: SQL)
Java for Aliens - Volume 1: LEARN JAVA FROM SCRATCH AND BECOME A PRO
Java for Aliens - Volume 2: LEARN JAVA FROM SCRATCH AND BECOME A PRO
Introduction to Programming in Java: An Interdisciplinary Approach (2nd Edition)
Core Java Volume I--Fundamentals (10th Edition) (Core Series)
Head First Android Development: A Brain-Friendly Guide
Introduction to Java Programming and Data Structures, Comprehensive Version, Loose Leaf Edition (12th Edition)
Java Tutorial: Creating Your First Window in Java
Windows are the building block of all graphical desktop programs, but programming them may seem difficult. Never fear, using just a few lines of Java code, you can make your own and start on the path that leads to programming like a pro!
A window is an essential part to pretty much every program you use on your computer, regardless of operating system. The web browser you are using to read this article is contained in a window, as your favorite word processing and calendar applications are as well. There are the basic building blocks of creating GUIs, as they are the container for everything else. They are to the programmer as a canvas is to a painter. Before you can create a program with buttons, menus, text boxes, and other useful components.
To begin, you must first import the package that contains the code necessary for creating a panel. There is a lot of code hidden in the package that communicates with your computer screen directly. We don't need to know how it all works, the same way you don't need to know how your car works to drive it. The code to import this is simple:
import javax.swing.JFrame;
This code should be placed at the very top part of your source file. Next, lets move on to creating a window object:
JFrame window = new JFrame();
This code creates a new JFrame object, which is a window. If you ran the program now you would find that there was no window displayed. This happens because the makers of Java decided to make every JFrame invisible once it is created. The programmer must explicitly tell the JFrame to be visible, this code will do so:
window.setVisible(true);
Now you can run the program and you should see a tiny window. To make the window any size you like put this code above the line that contains setVisible:
window.setSize(100,200);
This will make the window 100 pixels wide and 200 pixels tall. You can change these numbers around to make the window whatever size you wish.
Another problem you may find with this window is that when you close it, the java application contiinues to run. You can fix this by adding:
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
before you make the window visible. This will tell the actual thread that the window is running on to exit when the window closes. Otherwise the application continues running invisibly until the computer shuts down, or the virtual machine is terminated.
Here is the complete code. Remember to save this code as "WindowPractice.java" if you are not changing the class name:
import javax.swing.JFrame;
public class WindowPractice{
public static void main(String[] args){
JFrame window = new JFrame();
window.setSize(2220,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
Well, that is all there is to creating a simple window in Java. The wonderful flexibility and cross-platform aspects of Java make it a great language to learn for both pre-professionals or hobbyists. Have fun coding!