Lesson 1 - Java GUI
This tutorial is for anyone who wants to learn the basics of the graphical user interface in Java and thus create interesting programs. Prerequisites for this tutorial include knowledge of at least basic language constructs and basics of OOP.
What is a GUI?
The Graphical User Interface is everything the ordinary user of our program interacts with - a window containing various graphic components (buttons, icons, scrollbars ..). Using these components, the user communicates with the program. Since the 1980s, the GUI has become standard and today, we practically don't encounter a program that runs only in the command line. That's why it's a good idea to learn it
GUI and Java
In the early days of Java, designing the GUI was quite lousy, but over time
it's been constantly improved. Today, Java is a fully-featured tool for creating
nice window apps. Java contains two popular graphic libraries. The older AWT
(Abstract Windowing Toolkit) and since JDK 1.2, there's also JFC (Java
Foundation Classes) - known as Swing. In Java, all buttons, windows, text areas,
etc. are called components. We place components in containers such as
JFrame
or JPanel
.
First window
In Java, of course, there are more ways to create a basic window. In my
opinion, the best one is as follows: we'll create a new project in NetBeans or
Eclipse. It'll generate a class named after our project with the
main()
method.
We'll add a new class named Gui
to our project. The
Gui
class will inherit from the JFrame
class. Of
course, we must import the library. JFrame
is located in the
javax.Swing.JFrame
package. Just to be sure, we'll import all Swing
packages using an asterisk.
In the Gui
class, we'll create a parameter-less constructor.
Since Java doesn't inherit constructors, we'll use the super
keyword which calls the JFrame
class constructor and takes the
title of our window as a parameter. The Gui
class looks like
this:
import javax.swing.*; public class Gui extends JFrame { public Gui() { super("My first window"); // the parameter is the window title } }
Let's get back to the main method. We'll create a Gui
class
instance in it, which is our window. We'll call the following basic methods on
our object:
setVisible(true)
- This method with thetrue
parameter makes our window visiblesetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
- Makes the program to close when the "X" window button is pressedwindow.setSize(300, 200)
- A method that sets the window size (in pixels), the first parameter is the width, the second is the height.
The main class will look like this:
import javax.swing.JFrame; public class GuiTutorial { public static void main(String[] args) { Gui window = new Gui(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); window.setSize(300, 200); } }
If we run the program now, a window will appear in the left corner. It'd be
more elegant if we centered it on the screen. To do this, we'll use the
following method with a null
parameter:
setLocationRelativeTo(null)
So the main method looks like this:
public static void main(String[] args) { Gui window = new Gui(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); window.setSize(300, 200); window.setLocationRelativeTo(null); }
Great, we have our first window! It should look something like this:
You can normally resize, minimize and close the window. Next time, in the lesson Java GUI - Basic Components, we'll look at adding some components.