Clojure Swing JFrame Introduction

(See my full list of Clojure Swing examples and tutorials at Clojure Swing Interop)

JFrame s are the root windows of all modern Clojure Swing applications. In the old days, there were other options, but polite society does not speak of them.

The visible components that you use in your JFrame  are added to the content pane of the JFrame . You request the content pane and then add your components to that content frame.

Clojure JFrame Example
Clojure JFrame Example

Before adding components to a content pane, you need to decide one which of several layouts you will use for arranging your components. BorderLayout  is the default, so we’ll stick with that one for this simple example.

Border Layouts are divided into NORTH , SOUTH , EAST , WEST , and CENTER . We are going to use one label for each of the five locations in the content pane’s BorderLayout . We center the text and change the colors as needed so it is easy to see the locations of each label in the BorderLayout .

We set the size of the JFrame  to 600  pixels wide by 400  pixels tall, then set the JFrame  to visible equals true .

Note that we do all our layout in the event dispatch thread by using invokeLater .

Leave a Reply

Your email address will not be published. Required fields are marked *