Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

JavaProgramming_Lecture03

Uploaded by

stacy russo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JavaProgramming_Lecture03

Uploaded by

stacy russo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

AP/ITEC 2610 D

Object Oriented
Programming
Lecture 3
22 Sept 2022
nd
GUI: Learning outcomes
• Understand the GUI API in Java
• API means Application Programming Interface
• Implementing a GUI app with Java AWT
• AWT is a Java Library
• AWT means Abstract Windows Toolkit
• It is a platform dependent API for creating GUI for Java programs.
• AWT is old and outdated, it produces code that is
• not compact
• Not efficient

• Implementing a GUI app with Java SWING


• SWING is also a Java GUI Library, but more modern
• also more complicated

• Writing a simple GUI Java program


• display a simple GUI with a button and a message on it.

• Implementing GUI user input


• Using Event Listeners
Dr Sultana
****

Text Field
JavaX.Swing:
• JavaX is a library extension to the AWT Toolkit
• It is built on the top of AWT (Abstract Windowing Toolkit) API and is entirely written in Java.
• Swing is a part of the javax Library
• The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
• The JavaX Swing library needs to be imported which is used by a Java app.

import javax.swing.Jframe ,
import javax.Jpanel,
import javax.SwingUtilities

Note: The Swing Layout Extension Library is included in the Java Platform JDK 6 or higher, so no additional steps are needed if you develop the application with JDK 6
or 7 and deploy in environments that have JRE version 6 or higher.
Main Differences b/w Java awt and
swing:
Components and containers

• All the elements like buttons, text fields, scroll bars etc.. are known as
components.
• In AWT we have classes for each component as shown in the diagram
in previous slide (slide 3).
• To have everything placed on a screen to a particular position, we
have to add them to a container.
Types of containers:
There are four types of containers available in AWT: Window, Frame,
Dialog and Panel. As shown in the hierarchy diagram above, Frame
and Dialog are subclasses of Window class.

Window: An instance of the Window class has no border and no


title

Dialog: Dialog class has border and title. An instance of the Dialog
class cannot exist without an associated instance of the Frame
class.

Panel: Panel does not contain title bar, menu bar or border. It is a
generic container for holding components. An instance of the Panel
class provides a container to which we add components.

Frame: A frame has title, border and menu bars. It can contain
several components like buttons, text fields, scrollbars etc. This is
most widely used container while developing an application in AWT.
Java Swing Simple Example:
import javax.swing.JFrame;
GUI - Output
public class HelloFrame
{
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setSize(300,400);
frame.setTitle(“An empty frame”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Compile this code (HelloFrame.java) and get the GUI o/p fired up from your cmd prompt

Note: Be very wary of non ASCII characters when you copy & paste code.
Always practice safe texting.
import javax.swing.*;
import java.awt.*; Advanced Code Example
import java.awt.event.*;
public class first_GUI implements ActionListener { Command Line Code Execution:
public static void main(String args[]) {
JFrame frame = new JFrame("My First GUI");
JPanel panel = new JPanel();
frame.setSize(600,300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
// Creating an object called button
JButton button = new JButton("Close");
button.setBounds(20,80,100,125);
button.addActionListener(new first_GUI()); GUI - OUTPUT
panel.add(button);
// Creating an object called label
JLabel label = new JLabel("Button Object is created Yayy!!!");
label.setBounds(40,100,150,200);
panel.add(label);
// Setting frame to visible
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) { Compile this code (first_GUI.java) and get the GUI o/p
System.out.println("Button clicked"); }
}
fired up from your cmd prompt
Event Listeners:
• In an event-driven user interface, the program receives an event whenever
the user manipulates an input component.
• User interface events include key presses, mouse moves, button clicks, and
so on.
• A program indicates which events it needs to receive by installing event listener objects
• Belongs to a class provided by the application programmer
• Its methods describe the actions to be taken when an event occurs
• Notified when event happens

Event source:
• User interface component that generates a particular event
• Add an event listener object to the appropriate event source
• When an event occurs, the event source notifies all event listeners
Event listeners syntax:
• At the beginning – Implement the action:
• class SimpleGui extends JFrame implements ActionListener {
• ………….
• …………. }

• At the end - Call the action:


• public void actionPerformed(ActionEvent ae) {
• ………….
• …………. }
JAVA awt and swing Example:
• HelloGUI.java
• This code implements a class called HelloGUI. It creates a frame and a
panel layout to show User ID and Password fields with a Button
object, saying “Login”.
• On matching the pre-defined user Id and password, the window
shows a message of “Successful Login”.
Seems Fun!
• It uses a Call back ActionListener Function and all major swing
libraries
HelloGUI.java
• DOWNLOAD & RUN THE FILE FROM E-CLASS under WEEK 3:

Ref : https://beginnersbook.com/2015/07/java-swing-tutorial/
Listening to the Mouse:
• HelloMousePoint.java
• This code creates a grid of squares and shows the black dots when
mouse is pressed showing the (x,y) coordinate of the mouse.

DOWNLOAD & RUN THE FILE FROM E-CLASS under WEEK 3:


Displaying images and GIF
animation in Java

• Entire image or a GIF (graphic Interfacing image =Toolkit.getDefaultToolkit().createImage("oQ0tF.gif");
image2 = Toolkit.getDefaultToolkit().createImage("mario-pipe.gif");
File) can easily be displayed in your panel ….
with the graphics drawImage command
public void paintComponent(Graphics g) {
• This requires the re-painting of the super.paintComponent(g);
Window Panel to be self-managed g.drawImage(image, 200, 150, 50,50,this);
g.drawImage(image2, 10, 25, 100,100,this); }
• which you do by overriding the command
}
Exercises:
• https://www.javatpoint.com/java-keylistener
• https://www.javatpoint.com/java-jcombobox
• Explain a difference between frame , panel and window in Java GUI

• Do practice the online free tutorials to create Java GUI applications.

You might also like