Bonus: Tetrominos User Interface with Swing

What you need to know about the built-in Java user interface library to write Tetrominos.

User interface coding is an involved task. You will not need to write any user interface code yourself for Tetrominos, since I will provide this portion for you. However, it’s nice to know what types of things you can do with a user interface library, and this section will give a very brief introduction.

There are several user interface libraries for Java. We will use Swing, which is provided with Java. The user interface library handles putting windows, buttons, menus, and other elements on the screen, and allows for user input such as mouse motion, clicking, or key presses.

Let’s start with an example. First, create a new project with Eclipse. Call it whatever you like; for example, SwingExamples. Create a new class, called SwingTest. Then copy-paste the following code into SwingTest. (This code is based on Oracle’s HelloWorldSwing example.) Do not worry too much about the details of how the code works yet.

Run the code. If everything is working properly, a small window should appear somewhere on your screen containing the words “Hello, World”. You can click the close button on that window to stop the program.

Get hands-on with 1200+ tech skills courses.