

To keep it simple, it’s filled with the text “I’m a label in the window.” and its size has been set to a width of 300 pixels and height of 100 pixels. JLabel textLabel = new JLabel("I'm a label in the window",SwingConstants.CENTER) tPreferredSize(new Dimension(300, 100)) Ī JLabel is a graphical component that can contain an image or text.
#Gui java compiler code#
Add the following lines of code to the createWindow function to create a new JLabel object Since an empty window has little use, let's now put a graphical component inside it. The JFrame.EXIT_ON_CLOSE constant sets our Java application to terminate when the window is closed.

Type in:įtDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) We can do this by setting its attributes, such as its general appearance, its size, what it contains, and more.įor starters, let's make sure that when the window is closed, the application also stops.

#Gui java compiler how to#
It handles the complex task of telling the computer how to draw the window to the screen, and leaves us the fun part of deciding how it's going to look. The JFrame class will do most of the work of creating the window for us. You can think of "frame" as the window for our Java application. What this line does is create a new instance of a JFrame object called "frame". Type in the following code, remembering to place it between the curly brackets of the createWindow function: Now, let's look at creating the window using a JFrame object. Anytime the createWindow function is called, the Java application will create and display a window using this code. With this in mind, I'm grouping all the Java code that deals with creating the window into one function.Įnter the createWindow function definition:Īll the code to create the window goes between the function’s curly brackets. This design makes the program more readable, and if you want to run the same set of instructions again, all you have to do is run the function. It's good programming style to group sets of similar commands into functions. Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
