Member-only story

Programming Basic Java GUIs With Swing

Scott Cosentino
4 min readDec 8, 2019

--

When you first start learning programming, it is understood that when a program executes, it runs through a set of instructions, and terminates execution after the last instruction is run. Event driven programmer is an alternative to this idea, in which code is executed by the program in response to some sort of event being triggered. This type of programming is typically used when developing Graphic User Interfaces (GUI). GUIs appear in many ways, including webpages, phone apps, and desktop applications. The driving idea behind GUI programming is that a user is presented with some sort of interface they can interact with. When they do something on the interface, code is executed to react to the event.

When we design an interface, we create components, which are objects that a user can interact with. Components are then registered with listeners. Listeners wait for some event to happen and execute code when the event occurs. To understand this, let’s look at a simple Java GUI application written in Swing.

When we create a GUI in Java, we start by creating what is referred to as a JFrame. A JFrame can be thought of as the main window of our application, which will hold all of the other components the user can interact with. There are a number of different properties we can set on JFrames to make them behave as required. For example, the code below defines a JFrame with a title text “Title of the window”, sets the default close operation to exit the program, sets the size of the frame to 300 by 300, and makes the frame…

--

--

Scott Cosentino
Scott Cosentino

No responses yet