Showing posts with label Overriding. Show all posts
Showing posts with label Overriding. Show all posts

GUI Basics

A GUI is an acronym of Graphical User Interface, and is sometimes pronounced as Gooey. Visual Basic has a very good example of GUI wherein you can draw your objects freely with the use of the mouse. However, in Java, you have to plot it point by point.

The first thing in creating a GUI in Java is to use everything in the swing package and to override swing's JFrame by using the extend keyword.

Our main goal in this lesson is to create a simple GUI of an ATM Machine with two labels and textboxes (Card Number and Pin), two buttons (OK and Cancel) and name it ATM.





Below is the code with explanations...


import javax.swing.*;  //use everything within the swing package
public class CodeGalaxy extends JFrame  //overrides JFrame
{

JTextField txt1 = new JTextField();  //JTextField is equivalent to a textbox
JLabel CardN = new JLabel("Card Number: ");  //JLabel is equivalent to a label

JTextField txt2 = new JTextField();
JLabel PIN = new JLabel("Pin: ");

JButton OKbtn = new JButton("OK");  //JButton is equivalent to a command button
JButton Can = new JButton("Cancel");


public CodeGalaxy()   //constructor method with the same name of your class
{

//shows the output of the Card Number


getContentPane().setLayout(null);   //set to nothing / null 
CardN.setBounds(70,10,90,10); //setBounds defines the boundary of your object
getContentPane().add(CardN);

txt1.setBounds(150,10,150,18);
getContentPane().add(txt1);

//same flow of idea as to the Card Number//

//shows the output of the PIN
PIN.setBounds(70,30,80,10);
getContentPane().add(PIN);

txt2.setBounds(150,30,150,18);
getContentPane().add(txt2);


//shows Ok button
OKbtn.setBounds(230,60,70,30);
getContentPane().add(OKbtn);


//shows Cancel button
Can.setBounds(70,60,90,30);
getContentPane().add(Can);


setTitle("ATM");   //sets the title as ATM
setSize(400,130);  //sets the size
setVisible(true);  //makes our ATM visible

}

public static void main (String args[])
{
CodeGalaxy x = new CodeGalaxy();
}
}



Further explanation:

setBounds syntax:
variable.setBounds(x-axis,y-axis,width,height);

setSize syntax:
setSize(width, height);


public static void main (String args[])
{
Lab3 x = new Lab3();
}
without the public static void main, our program will run invisibly.

JTextField txt1 = new JTextField();
JTextField is a textbox with the variable name txt1


JLabel PIN = new JLabel("Pin: ");
JLabel is a label with the variable name PIN with the caption as Pin:


JButton Can = new JButton("Cancel");
JButton is a command button with the variable name Can with the caption as Cancel

The 'extend' keyword


class first

{
int number;


void Add()
{
number=number+1;
}


void Subtract()
{
number=number-1;
}
}
class second extends first
{
void Add()
{
number=number*1;
}
void Divide()
{
number=number/1;
}
}
public class MClass
{
public static void main (String args[])
{
first one = new first();
one.Add();
one.Subtract();


second two = new second();
two.Add();
two.Divide()
}
}

Given the code above, second extends first where second inherits first's methods and variable. As you can see the variable in class first  and class second is the same, which is number, though number is only declared in class first. Class Second overrides the Add method and added a Divide method.

When class second extends to first, we call second as a subclass of first and first is the superclass of second. In java, two or more classes can inherit from the same parent class but a class can only have one parent.

Method Overriding

Let's start with an example. If Aphrodite Keys is a famous celebrity, her mother, Hera Keys is also a famous celebrity. Whatever the parent is doing is inherited by the child.

In programming, method overriding means that whatever the parent class is doing, it is inherited by the child class. Given the code below:


class CodeGalaxy

{
public void planet()
{
System.out.print("Pluto is not considered a planet");
}
}


class MilyWay extends CodeGalaxy
{
public void planet()
{
System.out.print("How many planets do we have now?");
}
}


public class RunningCode
{
public static void main(String args[])
{
CodeGalaxy output1 = new CodeGalaxy();
CodeGalaxy output2 = new Milkyway();


output1.planet();
output2.planet()
}
}


Class MilkyWay extends CodeGalaxy is better understood as MilkyWay overrides CodeGalaxy.  The output will be:

Pluto is not considered a planet.How many planets do we have now?

For more on Overriding, refer to next post about the extend keyword.

Extended Search

Custom Search
Hello World