Python Notes for mca i year students osmania university.docx
Logic and Coding of Java Interfaces & Swing Applications
1. Interfaces
• Lists a set of methods and their signatures
– A class that ‘implements’ the interface must implement
all of the methods of the interface
– It is similar to a class, but there are differences:
• All methods in an interface type are abstract
They have a name, parameters, and a return type, but they
don’t have an implementation
• All methods in an interface type are automatically public
• An interface type cannot have instance variables
• An interface type cannot have static methods
2. Interface Syntax
• An interface declaration and a class that
implements the interface.
3. FileHelper Interface for the Classroom
Project
public interface FileHelper {
public boolean doesAFileExist();
public ArrayList<?> readFile();
public boolean writeFile(ArrayList<?> list);
}
4. Generics
• Code that uses generics has many benefits over non-
generic code:
– Elimination of casts. The following code snippet
without generics requires casting:
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);
– When re-written to use generics, the code does not
require casting:
• List<String> list = new ArrayList<String>();
• list.add("hello");
• String s = list.get(0); // no cast
• Enables programmers to implement generic algorithms
– Implementing generic algorithms that work on
collections of different types, can be customized, and
are type safe and easier to read.
5. ClassroomFileHelper
public class ClassroomFileHelper implements FileHelper {
@Override
public boolean doesAFileExist(){
}
@Override
public ArrayList<?> readFile(){
}
@Override
public boolean writeFile(ArrayList<?> list){
}
}
6. InstructorFileHelper
public class InstructorFileHelper implements FileHelper {
@Override
public boolean doesAFileExist(){
}
@Override
public ArrayList<?> readFile(){
}
@Override
public boolean writeFile(ArrayList<?> list){
}
}
7. CourseFileHelper
public class CourseFileHelper implements FileHelper {
@Override
public boolean doesAFileExist(){
}
@Override
public ArrayList<?> readFile(){
}
@Override
public boolean writeFile(ArrayList<?> list){
}
}
8. Demo in Eclipse
• Implementing interfaces quickly
Moves
• heal( )
• attack( )
• jump( )
• defend( )
Good Guys
Bad Guys
implements
Items of notes
-return types
-parameters
-@Override
10. Frame Windows
Java provides classes to create graphical applications
that can run on any major graphical user interface
A graphical application shows information inside a
frame: a window with a title bar
Java’s JFrame class allows you to display a frame
It is part of the javax.swing package
11. Five steps to displaying a frame
1) Construct an object of the JFrame class in the main method
2) Set the size of the frame
3) Set the title of the frame
4) Set the “default close operation”
5) Make it visible
JFrame frame = new JFrame();
frame.setSize(300,400);
frame.setTitle(“An Empty Frame”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
12. Adding Components to the JFrame
You cannot draw directly on a JFrame object
Instead, construct an object and add it to the frame
A few examples objects to add are:
JComponent
JPanel
JTextComponent
Jlabel
If you have more than one component, put them into a
panel (a container for other user-interface components),
and then add the panel to the frame
13. Create the Components
public class FormPanel extends JPanel{
JButton button = new JButton("Click me!");
JLabel label = new JLabel("Hello, World!");
public FormPanel( ) {
add(button);
add(label);
}
}
• Design a subclass of JFrame
• Store the components as
instance variables
• Initialize them in the
constructor of your subclass
14. Add the Panel to the Frame
public class StartProgram {
public static void main(String[ ] args) {
JFrame frame = new JFrame();
JPanel panel = new FormPanel();
frame.add(panel);
frame.setSize(250, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
15. Project Notes
• Let Eclipse handle the imports
• Divide classes by function
– model
– view
– tests
– [controller]
• Always set the frame visible last
16. Your Book Favors…
private void createComponents() {
button = new JButton("Click me!");
label = new JLabel("Hello, World!");
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
add(panel);
}
17. Your Book Favors…
public class FilledFrame extends Jframe {
private JButton button;
private JLabel label;
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 100;
public FilledFrame()
{
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
}
18. Your Book Favors
public class FilledFrameViewer2 {
public static void main(String[] args)
{
JFrame frame = new FilledFrame();
frame.setTitle("A frame with two
components");
frame.setDefaultCloseOperation(JFrame.EXIT
_ON_CLOSE);
frame.setVisible(true);
}
}
19. Event Handling
A program must indicate which events it wants to receive
It does so by installing event listener objects
An event listener object belongs to a class that you declare
The methods of your event listener classes contain the
instructions that you want to have executed when the
events occur
To install a listener, you need to know the event source
You add an event listener object to selected event sources
OK Button clicked, Cancel Button clicked, Menu Choice
Whenever the event occurs, the event source calls the
appropriate methods of the attached event listeners
20. Implementation for Event Handling
• Create the class as an inner class inside the class
that contains the elements you want to listen
– Typically in your JPanel because your buttons are
created in it
• Implement the ActionListener interface
– Add the method
– Inside the method are the instructions to execute
once the action has been triggered
• Add the ActionListener to the Button
21. Inner Classes
Inner classes are often used for ActionListeners
An inner class is a class that is declared inside another class
It may be declared inside or outside a method of the class
Why inner classes? Two reasons:
1) It places the trivial listener class exactly where it is
needed, without cluttering up the remainder of the project
2) Their methods can access variables that are declared in
surrounding blocks
22. public class ButtonFrame2 extends JFrame
{
private JButton button;
private JLabel label;
. . .
class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent
event)
{
label.setText("I was clicked");
}
}
. . .
}
Can easily access methods of
the private instance of a label
object.
Outer
Block
Inner
Block
23. Class & Interface Implementation
public class ClearButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dollarField.setText("");
euroField.setText("");
gbpField.setText("");
}
24. Attaching to Button
//inside the JPanel constructor
ClearButtonListener clearlistener = new ClearButtonListener( );
clearButton.addActionListener(clearlistener);
add(clearButton);
In a way, these are the ‘controllers’ for
our Swing interfaces. They are
contained in another class though.
25. Demo in Eclipse
StartProgram.java
Main Method
Creates the JFrame
Adds the JPanel to it
ConverterPanel
extends JPanel
All the components
All the clicklisteners
CurrencyConverter
Contains the business logic
Actually does the conversion
26. Demo in Eclipse
StartProgram.java
Main Method
Creates the JFrame
Adds the JPanel to it
ConverterPanel
extends JPanel
All the components
All the clicklisteners
model
package
view
packag
e
default
package
CurrencyConverter
Contains the business logic
Actually does the conversion