JavaProgramming_Lecture03
JavaProgramming_Lecture03
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
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.
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 {
• ………….
• …………. }
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.