Object-Orientated Analysis, Design and Programming

Slides:



Advertisements
Similar presentations
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 UFCE3T-15-M Object- oriented Design and Programming Block 3 GUI Programming Jin Sa.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Java Programming Chapter 10 Graphical User Interfaces.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
1 Chapter 10 Getting Started with Graphics Programming F Graphics Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames.
GUI Basics.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
MIT-AITI 2004 – Lecture 16 Introduction to Swing.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Introduction to Java Applet Programs Section 1.3Intro. to Java Applet Programs: a Greeter Applet Section 3.7Graphical/Internet Java: Applet: An Einstein.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Review_6 AWT, Swing, ActionListener, and Graphics.
Computer Science 209 GUIs Model/View/Controller Layouts.
Basics of GUI Programming Chapter 11 and Chapter 22.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
A Quick Java Swing Tutorial
Graphical User Interfaces
Graphical User Interfaces -- GUIs
Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout.
Swing JComponents.
GUIs Model/View/Controller Layouts
Java Swing.
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
Event-driven programming for GUI
CMSC 202 Swing.
Chapter 10 Getting Started with Graphics Programming
Window Interfaces Using Swing Objects Chapter 12
Introduction to Graphical Interfaces in Java: AWT and Swing
Programming Graphical User Interface (GUI)
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Object-Orientated Analysis, Design and Programming Presentation by Dr. Phil Legg Senior Lecturer Computer Science 7: Graphical User Interfaces Autumn 2016

Session Aims GUI components : To create user interfaces using frames, panels, and some simple UI components Layout Managers : Understand their role, and use FlowLayout, GridLayout and BorderLayout Graphics : Use of the Color class, paintComponent method, and Graphics class.

GUI Components

AWT and Swing Java provides many predefined classes for building GUI applications These classes are provided in two packages called: Abstract Windows Toolkit (AWT) and Swing In Java, to write a GUI program, we derive new classes from those provided in AWT or Swing

Structure of AWT and Swing Graphics Drawing, fonts, colour etc. Components buttons, text fields, menus and scroll bars etc Layout managers Arrangement of GUI components Event handlers Event such as clicking button and corresponding actions Image manipulation.

Creating a Frame import javax.swing.*; public class MyFrame1 extends JFrame{ MyFrame1(String title) { setTitle(title); setSize(400,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { MyFrame1 frame = new MyFrame1("MyFrame");

Adding components to frame Container contentPane = getContentPane(); //obtain the content pane of the frame JButton ok = new JButton("first_ok_button"); //create a button object contentPane.add(ok);

Adding JButton to frame import javax.swing.*; import java.awt.Container; public class MyFrame2 extends Jframe{ MyFrame2(String title) { setTitle(title); setSize(400,300); Container contentPane = getContentPane(); JButton ok = new JButton("first_ok_button"); contentPane.add(ok); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { MyFrame2 frame = new MyFrame2("MyFrame");

In-Class Activity Compile the examples to add a JButton to a frame

GUI Components Component is the superclass of Container Container is the superclass of Jcomponent JComponent is the superclass of AbstractButton AbstractButton is the superclass of Jbutton JComponent is the superclass of JTextComponent JTextComponent is the superclass of JTextField JTextComponent is the superclass of JTextArea JComponent is the superclass of JLabel

GUI Components

GUI Components

GUI Components The Component class provides methods such as setting and getting background colours. The Container class provides methods for adding component to the container; setting layout manager; and painting the component.

UI Objects: JButton Mostly used constructors. JButton(String text) E.g. JButton ok = new JButton("first_ok_button");  JButton also inherits all the methods in its super classes such as setBackgroundColor

UI Objects: JTextField A JTextField: user input area. Commonly used constructors and methods are: JTextField(int columns) Creates an empty text field with the specified number of columns. JTextField(String text) Creates a text field initialized with the specified text. Mostly used methods: getText() Returns the string from the text field. setText(String text) Puts the given string in the text field.

UI Objects: JLabel A JLabel is a display area for a short text. Constructors and methods include the following: JLabel() JLabel(String text) setText(String text) getText()

Containers Containers can contain a number of components, e.g. frames and panels, ContentPane We can use the add method to put components in a container: Container contentPane = getContentPane(); JButton ok = new JButton("first_ok_button"); contentPane.add(ok); Jframe is different from Frame, the ‘add’ method is overridden so that you can write: frame.add(child);

JPanel JPanel is a subclass of Jcomponent Used as container or sub-containers for grouping user interface components Often programmers use a JPanel instead of the Content Pane when adding components to a frame as JPanel is a subclass of JComponent; it provides more methods than the ContentPane

Layout Manger Controls how the GUI components within the container can be arranged Java provides a number of predefined layout managers.   FlowLayout GridLayout BorderLayout Containers has a method setLayout(LayoutManager)

FlowLayout FlowLayout is the simplest layout manager. The components are arranged in the container from left to right in the order in which they were added. When one row becomes filled, a new row is started.

FlowLayout public class ShowFlowLayout extends JFrame { public ShowFlowLayout() { Container container = getContentPane(); container.setLayout(new FlowLayout()); for (int i = 1; i <= 10; i++) container.add(new JButton("Component " + i)); } public static void main(String[] args) { ShowFlowLayout frame = new ShowFlowLayout(); ……

FlowLayout

GridLayout The GridLayout manager arranges components in a grid (matrix) formation with the number of rows and columns defined by the constructor. The components are placed in the grid from left to right starting with the first row, then the second, and so on.

GridLayout public class ShowGridLayout extends JFrame { public ShowGridLayout() { Container container = getContentPane(); container.setLayout(new GridLayout(3,2)); container.add(new JLabel("First Name")); container.add(new JTextField()); container.add(new JLabel("MI")); container.add(new JLabel("Last Name")); } ……

GridLayout

BorderLayout Divides the container into five areas: East, South, West, North, and Center. To add a component to a particular area, we use the add(Component, constraint) method, where constraint is BorderLayout.EAST, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.NORTH, BorderLayout.CENTER

BorderLayout public class ShowBorderLayout extends JFrame { public ShowBorderLayout() { Container container = getContentPane(); container.add(new JButton("East"), BorderLayout.EAST); container.add(new JButton("South"), BorderLayout.SOUTH); container.add(new JButton("West"), BorderLayout.WEST); container.add(new JButton("North"), BorderLayout.NORTH); container.add(new JButton("Center"), BorderLayout.CENTER); } ……

BorderLayout

Combining Layouts

Color You can set colours for GUI components by using the java.awt.Color class. MyFrame(String title) { … … Container container = getContentPane(); JButton ok = new JButton("first_ok_button"); container.add(ok); ok.setBackground(Color.red); container.setBackground(Color.green); }

Drawing in Swing Usually create a new class that extends JPanel and override the paintComponent () method The Graphics object g is created automatically by the Java Virtual Machine for every visible GUI component.

Methods in the Graphics object drawLine(50,50,100,60), draws a line from (50,50) to (100,60) drawRect(20,20,100,50), draws a rectangle with (20,20) as the top left corner; 100 is the length and 50 is the width. fillRect(20,20,100,50), draws a rectangle as above but filled with the current colour. drawOval(30,30,50,50), draws an oval bounded by an invisible rectangle whose top left corner is at (30,30); length is 50 and width is 50. drawString(“Hello”, 50,50), draws a string starting from the position (50,50). setColor(Color.red), sets of colour of the graphics object to be red. http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html

paintComponent public class MyCanvas extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.fillRect(10,10,100,50); g.drawRect(10,80,100,50); }

Drawing (1) public class MyExample extends JFrame { MyCanvas canvas = new MyCanvas(); JLabel heading = new JLabel("My drawing"); public MyExample() { Container container = getContentPane(); container.add(heading,BorderLayout.NORTH); container.add(canvas,BorderLayout.CENTER); }

Drawing (2) public static void main(String[] args) { MyExample myDrawing = new MyExample(); myDrawing.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); myDrawing.setSize(300,300); myDrawing.setVisible(true); }

Events Events: mouse movements, mouse clicks, and keystrokes or by the operating system, such as a timer. Java has pre-defined classes to represent different kinds of events. Each kind of event is defined as a class. For example, ActionEvent defines events such as pressing a button; WindowEvent defines events such as closing or opening a window;

Events Events: mouse movements, mouse clicks, and keystrokes or by the operating system, such as a timer. Java has pre-defined classes to represent different kinds of events. Each kind of event is defined as a class. For example, ActionEvent defines events such as pressing a button; WindowEvent defines events such as closing or opening a window;

Listeners Once an event is generated, it needs to be listened by an object: the listener. The listener object receives information about these events and takes some actions to respond to these events, i.e. to handle the events. The actions for handling the events are defined in the methods specified in the relevant listener interface. The listener object’s class must implement the corresponding event-listener interface. The listener object must register with the source object.

Listener interfaces Event Class Listener Interface Listener Methods (Handlers) ActionEvent ActionListener actionPerformed(ActionEvent) WindowEvent WindowListener windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) MouseEvent MouseListener mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseClicked(MouseEvent) mouseExited(MouseEvent) mouseEntered(MouseEvent)

Listener interfaces - example

Summary How to create various GUI components, in particular, JFrame, JPanel, JButton, JTextfield, JLabel. How to use layout managers for containers, in particular, FLowLayout, GridLayout and BorderLayout. How to use the graphics object to draw graphics, in particular how to use the paintComponent method.

In-Class Activity Write a java program to implement the oven front view example

In-Class Activity Write a java program to implement the simple calculator to produce a GUI as shown below.

In-Class Activity Write a java program that displays a frame. In the frame there are buttons labelled “red”, “blue” and “green”. When a button is pressed, the background is changed to that colour. Try extend this to support mixing of colours (so that multiple colours can be selected at once – you may decide to use something other than buttons…)

In-Class Activity Write a java program to implement the simple calculator to produce a GUI as shown below.