Lesson 01 – simpleGUI

I decided to write a blog and what better than to writing about what I love doing and learning and be able to share it as some sort of lessons for us all.

BEFORE YOU START: I am sorry if the language / English is not up to scratch it’s not my first language or even my second language to please be patient if you find English grammar or spelling mistakes.

  • Is this suitable for everyone? Nope not at all and I don’t claim it to be either,
  • Will this teach you how to learn to code most likely not as those examples, lessons, blogs are just how I see it and how I am learning it.
  • Is the code free to use? It sure is the code has been written how I understand it and written by me truly.
  • Has this code been copied from else where? I have taken bits of code so I could understand how it work and then modified to my liking.

For day one I will start showing you the first bit of code how you are going to go from a simpleGUI to a usable application but all in good time as this is like a transformation from caterpillar to butterfly it will take time and many blogs,lesson,how-to(s) later you will see the result, I am learning also and I will be sharing my view on learning by example programming in Java.

Those how-to(s) will transform a simple #java GUI all the way into a useful application, not sure what yet but we could end up with another calculator 😉 as I already made one that can be downloaded from the Application link above or maybe something else I have not decided yet.

In this blog, learning by example I will be making some assumptions as the examples are based on how I am doing and learning things, I am working on Linux system and I am using Eclipse as my Java Integrated Development Environment (IDE)

I am also assuming you like Java language & the coffee as that is the reason I am doing this and I am hoping that is the reason you are reading this.

Here is a example of the completed code of a simple java Graphical User Interface will deliver today.

blog-lesson01

Java SimpleGUI – Lesson 1

1) Launch your EDI (Eclipse Juno)
2) File –> New –> Java Project –> Name = “SimpleGUI”
3) File –> New –> Class
a. Package: gcclinux.co.uk
b. Name: SimpleGUI
c. Modifiers: public

Now the basic package has been created in your project called gcclinux.co.uk and all your classes & resources will be located inside this package when finished.

You also start with the basic code in your class, it’s nice of eclipse doing some of the work for us but unfortunately that is not enough and you will need a little more that to open a Java GUI so let’s take a look at this one bit at the time.


public class SimpleGUI {
}

Lets create a method from which we will store all that is required to call by the main method in the end.

private = means that the method is not visible and can not be called by any different objects
static = means that the method is associated with the class, not a specific instance of that class.
void = means that the method has no return value
buildGUI = well that is the name I chosen for my method.


private static void buildGUI() {
}

You have now created a class, a method but we have not added a object in the method yet so right now it’s not going to do anything as there is nothing to do, so lets create a frame, give the frame a title, give it a size.


JFrame frame = new JFrame("SimpleGUI – Lesson 01");
frame.setPreferredSize(new Dimension(300, 200));

Another thing you will need to do import a couple of java class provided by Java to create the Frame & set the dimensions, and you have two choices, you can type it up or you can press “CRTL,SHIFT,O” and that will import All packages.


import java.awt.Dimension;
import javax.swing.JFrame;

You will also want to close the frame when you are finish with it so let’s add a function to the frame that will close it when you click “x”


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

We now have enough to display a small GUI, I am going to pack it up and set it visible so that when we call it it’s instructions are simple, take you pack and do what you need. (setVisible)


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

We are almost there, all we need to do now is create the main method that will call the frame.pack we just created.


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

So this was a breakdown of the entire and yet small amount of code to create a simple #java GUI.

Here is the full code:

———— START CODE ————

package gcclinux.co.uk;

import java.awt.Dimension;
import javax.swing.JFrame;

public class SimpleGUI {
private static void buildGUI() {
JFrame frame = new JFrame(“SimpleGUI – Lesson 01”);
frame.setPreferredSize(new Dimension(300, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

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

———— END FULL CODE ————

So last thing to do is launch your new SimpleGUI…..

Menu –> Run –> Run or click on the GREEN run button.

Next time for lesson 02 I’ll add some personality to this GUI.

btn_donate

Leave a Reply