Download presentation
Presentation is loading. Please wait.
1
Constructors, GUI’s(Using Swing) and ActionListner
2
Constructors A block of code similar to a method that’s called when an instance of an object is created. Here are the key differences between a constructor and a method: A constructor doesn’t have a return type. The name of the constructor must be the same as the name of the class. Unlike methods, constructors are not considered members of a class. A constructor is called automatically when a new instance of an object is created.
3
Constructor has same name as the class and looks like this in a java code:
public class MyClass{ //This is the constructor MyClass(){ } ..statements..
4
Constructor Example Program
In the program below we have created an object “obj” of class Hello and then we have displayed the instance variable name of the object. this keyword Refers to the current object, object obj in this example. public class Hello { String name; //Constructor Hello(){ this.name = “Constructors are easy"; } public static void main(String[] args) { Hello obj = new Hello(); System.out.println(obj.name); } new keyword Creates the object of the Hello class and invokes the constructor to initialize it
5
Default Constructor Every class has a constructor
If you don’t set one up Java will do it for you at run time You will not find it in your source code(the java file) It would be inserted into the code during compilation and exists in .class file. //Constructor inserted public class Hello { public static void main(String[] args) { Hello obj = new Hello(); } public class Hello { Hello(){ } public static void main(String[] args) { Hello obj = new Hello(); Compiler
6
Difference between Constructor and Method
The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code. Constructors cannot be abstract (without braces, and followed by a semicolon), final, static while methods can be. Constructors do not have return types while methods do. Remember the name of the constructor must be the same as the name of the class
7
Creating GUI’s in Java – Swing Package
Java Swing is a lightweight Graphical User Interface (GUI) toolkit that includes a rich set of widgets. It includes a package which lets you make GUI components for your Java applications, and it is platform independent.
8
Java Swing class Hierarchy Diagram
What is a container class? Container classes are classes that can have other components on it. Panel: The sole purpose of a Panel is to organize the components on to a window. Frame: A fully functioning window with its title and icons. Dialog: Like a pop-up window, displays a message. Not a fully functioning window like the Frame. More info on the Swing package on Oracle’s website: 7/docs/api/javax/swing/package- summary.html All components in swing are JComponent which can be added to container classes.
9
Action Listener You implement an action listener to define what should be done when an user performs certain operation. An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field. The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component.
10
To write an Action Listener, follow the steps given below:
Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example: public class MyClass implements ActionListener { Register an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass); Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.