Basic JAVA GUI Tutorial

Graphical user interfaces are the quintessential part of any commercially available software. Could you imagine buying Microsoft Word if you had to input via command line?

I certainly couldn’t. But, for many, making a GUI is tricky. Especially in Java. So, I’m going to shed the light on how to use Java’s Swing to create a basic Graphical User Interface for your program.

What You Will Learn About GUIs

  • Creating Windows.
  • Adding components; buttons, labels, fields.
  • Layout of Components in your GUI.

Firstly, How Do You Make A Blank Window in Java?

The answer is quite simple. JFrame. Of course, there are other ways, but for the purposes of this tutorial and general ease, we will start with JFrame.

  • JFrame is a “new window”.
  • You can declare various instances of JFrame.
  • JFrame is customizable.

Example of a Blank JFrame Window (for GUI)

This code will produce a blank window with the title that we’ve set. You can see this depicted below.

Related:  HackThis: Basic+ Level 1 Solution

blank jframe window in java

Make a Blank Window using Inheritance (Extends) from JFrame

How To Add Components to JFrame , JPanel

Now that we’ve got blank window using JFrame, we want to add some components. So add some components we need a JPanel. Is panel sits on JFrame. We add components to the panel.

This above code will produce a simple window, with a simple button on it.

Related:  eBay, Retail Arbitrage Tutorial: Making BIG Profits

jframe with jbutton

Using FlowLayout on your GUI, Adding Components & Aligning!

After using the above code you’ll end with something like the image below!

flowlayout java gui alignment

You should also note while the window is resizable, the components will collapse underneath each other. Left staying at the top, right at the bottom.

resizing jframe