Java: GUI basics
Tuesday, January 22, 2013

Turns out I'm pretty good at this Java thing, there's a test on this Friday and I don't particularly want my friends to fail so here's something for you guys to read. If you managed to understand everything, you will pass with flying colours without a doubt.

The files that I'm using can be downloaded here. I'm using the Shops.java file mostly for this. Follow this post carefully, if there are sections of code that seems alien, don't worry, they are probably not tested and are written as a challenge for me.

I'm basing this tutorial on Practical 8 Part 2 GUI.



First off, the Layout Manager.
In order to use it, you need to import the necessary libraries, in this case it's
import java.awt.*;
Which has to be declared at the very beginning.

public class Shops extends JFrame implements ActionListener {

private JButton Shop1 = new JButton("Shop1");
private JButton Shop2 = new JButton("Shop2");
private JButton Shop3 = new JButton("Shop3");
private JButton Shop4 = new JButton("Shop4");
private JButton shopcart = new JButton("Shopping Cart");
private JButton quit = new JButton("Quit");

public Shops() {
this.setLayout(new BorderLayout());

JPanel panelA = new JPanel();
this.add(panelA, BorderLayout.CENTER);
panelA.setLayout(new GridLayout(2,2));
panelA.add(Shop1);

panelA.add(Shop2);
panelA.add(Shop3);
panelA.add(Shop4);

JPanel panelQ = new JPanel();
this.add(panelQ, BorderLayout.SOUTH);
panelQ.setLayout(new GridLayout(1,2));
panelQ.add(shopcart);
panelQ.add(quit);


The first line declares that Shops is a child member of JFrame, and implements the Interface called ActionListener. This allows you to use methods and functions from JFrame and ActionListener.

JButton is declared to create a Button.
JLabel is declared to create a Label that you can assign text to.
JTextField is declared to create 1 Text Line you can write to.
JTextArea is declared to create multiple Text lines you can write to.

public Shops() is a constructor for the class Shops. You have to write this or you wouldn't be able to construct the object.

this.setLayout sets the LayoutManager used for the Frame you're creating. So far we've only learned BorderLayout and GridLayout.

this was a pain to draw

Assuming a GridLayout(2,2), it'll create a layout on the left.
Using a BorderLayout will look like the one on the right. Yes, the space distribution is that weird.

A panel is a space you use to enter your content.
JPanel panelA = new JPanel();this.add(panelA, BorderLayout.CENTER);panelA.setLayout(new GridLayout(2,2));
panelA.add(Shop1);
Creating a panel is really straight forward as you can see. You can then use the name of your panel to add items into it.

Now, the most important piece of code to look out for.

//library to import import java.awt.event.*;
Shop1.addActionListener(this); Shop2.addActionListener(this); Shop3.addActionListener(this); Shop4.addActionListener(this); shopcart.addActionListener(this); quit.addActionListener(this);

You must put this in. This tells the program to listen/watch out for your Shop1 button. So that it will know if you press it or something. (if you're confused where all these goes, refer to the downloaded files)

public void actionPerformed(ActionEvent e) { 
// TODO Auto-generated method stub 
if(e.getSource()==Shop1) { 
item1 = 1.00; 
item2 = 2.00; 
item3 = 3.00; 
Shopping shop1Form = new Shopping(total[0], tempno1, 1); 
shop1Form.setTitle("Shop 1"); 
}
else if(e.getSource()==Shop2) { 
item1 = 3.00; 
item2 = 4.00; 
item3 = 5.00; 
Shopping shop2Form = new Shopping(total[1], tempno2, 2); 
shop2Form.setTitle("Shop 2"); 
}

This section of code is where the magic happens. This is the method implemented by the ActionListener. Remember, when you implement an Interface, your class must have all the methods of the Interface. (which can be imported when you hover over you class that has a red underline, then add unimplemented methods, like this)

Look at e.getSource()

e is simply any variable you assign to the ActionEvent. It could even be (ActionEvent Fuck), in which case your code would be
if(Fuck.getSource()==Shop1)
That means that if you click on Shop1, something will happen, whatever code that you put to it. Then I simply create another Object from the class Shopping, passed some variables and voilĂ , a new window pops up like this.



The idea is the same for the other Classes that I've created.

Okay, the class is all good and done, how do you actually make it come out?
You write a test program to calls upon the object. Refer to TestGUI.java

Shops newShop = new Shops(); newShop.setVisible(true); newShop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); newShop.setTitle("Shops"); newShop.setSize(400,350); newShop.setLocationRelativeTo(null);

Here, I'm creating a new Object from the class Shops, with the name of newShop. You have to setVisible to true, or you can't see it, obviously.

setSize determines the size of the frame.
setLocationRelativeTo determines where the window will pop up. null will center the application.

Remember that you have to create the Shops object in a main method, which in something like this

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

You need a main method for the program to run after all. The other classes are pretty much constructed in the same way that I've explained so far.

Finally, catching error exceptions. The code works like this

try { 
some function you're working on 
} 
catch(some kind of error) { 
what to display if this error happens 
} 
catch(another kind of error) { 
display something else for another kind of error 
} 
finally { 
do something at the end of everything 
}

This is rather straight forward. Really, just read up on Chapter 9 and I promise you will understand it. Think of it as an if-else statement that captures certain errors. I do not remember the types of errors, but I'm sure it'll be provided in the test.


And this is pretty much it.
If there's a large chunk of stuffs that I didn't cover, that's because I implemented a cache system for each object. That is why I could store the quantity of items, along with the total cost of each shop.

Again, this is something you could understand as I merely used all the things that has been taught from Chapter 1 to 9. As long as you understand 90% of my code, you can score 100 for the test. I swear.

I took my precious time to write this. If anyone of you read this and still failed, I will personally shove my fist up your butthole. Be warned.

Labels: , , , , , , , ,