Lesson 02 – simpleGUI

In order to start given your SimpleGUI some personality without making much changes to the exiting code you only have to add a couple lines more and that can be achieved by creating a container,  getting the current details from the pack we created earlier and add a REG background colour to it, obviously it’s not going to stay RED forever but for this example it’s fine.

We are also going to take the existing frame a few notches away from the top conner of the screen by adding 100 pixels from the top and 300 pixels from the left, remember that measurement always starts from “0” and zero is always the TOP/LEFT of your screen.

Here is a example of the completed code for a SimpleGUI – Lesson 02

blog-lesson02

Java SimpleGUI – Lesson 02

First things first as this is now lesson 02 lets update the title to reflect what we are doing so we know that we go this far when picking this up the next day.

JFrame frame = new JFrame("SimpleGUI – Lesson 02"); // Lesson 02

While this is changing the existing code we done before it’s actually just a String of text so we are not actually changing much at all just displaying a different title.

I am going to now create a couple of integer variables that we can use to assign to the frame location so it does not display on the top left hand conner of your screen always.

int y = 100; // Lesson 02 - Distance from the top
int x = 300; // Lesson 02 - Distance from the left

With that created we can now add a single line of code to your application by using the newly created variables to set the location.

frame.setLocation(x, y); // Lesson 02 - Setlocation of the FRAME

So now your SimpleGUI will no longer show top left hand conner of your screen, go on try it, if you have added the code already just run it..

In Eclispe you do Menu –> Run –> Run or click on the GREEN run button.

You have updated the “title” minor change and made the frame open in a specific location on your screen, lets take a look at giving this frame some character and add a bit of a colour to it’s pale cheek 🙂

To do that you start by creating a container that will hold the colour in it’s place for the frame, you also get the current details of the pack we created and add it to the container using the getContentPane method that retrieves the content from the pane layer and store it in memory, you then can add the background object to it.

Container c = frame.getContentPane(); // Lesson 02 - Create container and get existing frame content

The container is now created and ready to receive the Background object and define a colour, what you do is you take the Container “c” just a name and it could be anything you like as long as you call it the same when using it, the only thing you can not use are pre-defined name, objectives, string or variables that Java has already used in the construction classes but when using Eclipse it will always tell you if you did that and that you need to change it.

Take your container “c” with all it’s goodies and setBackground() the Color.RED is a pre-define java class that you can call and apply some pre-defined colours that are available, I normally would type in Color. (dot) and wait for Eclipse to display my options 🙂 easy and laze way to do but I like it.


c.setBackground(Color.red); // Lesson 02 - apply colour to the frame

Now that you have done that you can either import the Color and Container classes into your code by typing it in or you can press “CRTL,SHIFT,O” in Eclipse and that will import required classes.

import java.awt.Color; // Lesson 02
import java.awt.Container; // Lesson 02

We have set location now, created a container and added colour to the frame and if you want to insure that the code changes today are correct, here is the full code to compare with, unfortunately I have not found out how to keep the code indentation in wordpress yet but in #Eclipse you can always use CRTL-SHIFT-F and that will put all your code inline.

————- START CODE —————-

package gcclinux.co.uk;

import java.awt.Color; // Lesson 02
import java.awt.Container; // Lesson 02
import java.awt.Dimension;
import javax.swing.JFrame;

public class SimpleGUI {
private static void buildGUI() {

JFrame frame = new JFrame(“SimpleGUI – Lesson 02”); // Updated Lesson 02
frame.setPreferredSize(new Dimension(300, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

int y = 100; // Lesson 02 – Distance from the top
int x = 300; // Lesson 02 – Distance from the left

frame.setLocation(x, y); // Lesson 02 – Set the location of the FRAME

Container c = frame.getContentPane(); // Lesson 02 – Create container
// and get existing frame
// content
c.setBackground(Color.red); // Lesson 02 – apply colour to the frame

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
buildGUI();
}
}

————- END CODE —————-

We are at the end of another blog try it out Menu –> Run –> Run or click on the GREEN run button.

Next time for lesson 03 I’ll add a function button to close the FRAME when pressed.

Leave a Reply