GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.

Slides:



Advertisements
Similar presentations
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Lecture 19 Graphics User Interfaces (GUIs)
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
GUI Basics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
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 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
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.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
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.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
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.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Basics of GUI Programming Chapter 11 and Chapter 22.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
A Quick Java Swing Tutorial
Introduction to Graphics
Java Applets.
Object-Orientated Analysis, Design and Programming
Ellen Walker Hiram College
Java Applets.
Week 8 Swing NetBeans GUI Builder
Advanced Programming in Java
Chapter 12 GUI Basics.
Graphical User Interface
Presentation transcript:

GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout Managers Event Handlers

Terms GIU component container coordinates graphics context

Types of interfaces Console applications Using print and Scanner A simple GUI Using JoptionPane A complete GUI using a Frame Can be created using AWT Can be create using Swing

The swing versus the AWT AWT part of JDK v1 Mapped to platform specific components Heavyweight Swing introduced in V2 Platform independent components Lightweight

Components JButton tnOK = new JButton(“OK”); JLabel tnOK = new JLabel(“OK”); JTextField tfNAME = new JTextField(“Enter Name”); JCheckBox cbOK = new JTextBox(“OK”); JComboBox cmb = new JComboBox(new String[] {“Red”,”Green”,Blue”});

The Class chart JComponent AbstractButton  Jbutton  JToggleButton JCheckBox JTextComponent JTextField JLabel JPanel … many others …

Classes Container Classes JFrame JDialog JApplet JPanel Helper Classes Graphics, Color, Font, FontMetrics LayoutManager

Frames Public class myframe { public static void main(String[] args) { JFrame yframe = new JFrame(“MyFrame”); myframe.setsize(300,300); myframe.setvisible=true; myframe.setdefaultcloseoperation(Jframe.EXIT_ON_C LOSE); }

Frames with components (limited example) Import javax.swing.*; Public class myframe { public static void main(String[] args) { JFrame yframe = new JFrame(“MyFrame”); java.awt.Container mycnt = frame.getContentPane(); Jbutton btOK = new JButton(“OK”); mycnt.add(btOK); myframe.setsize(300,300); myframe.setvisible=true; myframe.setdefaultcloseoperation(Jframe.EXIT_ON_CLOSE); }

Getting the screen size Dimension ScrS = Toolkit.getDefaultToolkit().getScreenSize(); int wid = screensize.width; int hgt = screensize.height; This can be used to center or dynamically size frame. Frame.setlocation(x,y); Frame.setwidth(wid); Frame.sethight(hgt);

Layout Managers Use to determine how components will be positioned on the frame or panel. To create and apply a layout manager to a component. LayoutManager Lm = new FlowLayout(); Frame.setLayout(LM);

Type of layout managers Flow Grid Border Others …. Properties.setHGap(10);.setVGap(10);.setAlignment(FlowLayout.RIGHT);

Creating a subclass of Frame Import javax.swing.*; Import java.awt.*; Public class theframe extends Jframe { Public theframe() { Container mycnt = getContentPane(); mtcnt.setLayout(new FlowLayout()); Jbutton btOK = new JButton(“OK”); mycnt.add(btOK); } public static void main(String[] args) { theframe myframe = new theframe(); myframe.settitle(“Frame subclass”); myframe.setsize(300,300); myframe.setvisible=true; myframe.setdefaultcloseoperation(Jframe.EXIT_ON_CLOSE); }

The color Class Color c = new Color(100,100,100) The color constructor expects: Color(R,G,B) Red, Green, Blue btnOK.setForeground( c );

The Font class Font ( name, Style, size) Font F = new Font(“Serif”, Font.BOLD + Font.ITALIC, 12); btnOK.setFont(F);

Drawing shapes drawline drawrect fillrect drawarc fillarc drawstring draw3drect drawroundedrect drawpolygon

Making a window based application import javax.awt.*; import javax.swing.*; public class MyWindow extends JFrame { public MyWindow() { super(“My Window”); setSize(400,200); setVisible(true); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.fillRect(20,20,100,100); } public static void main( String args[]) { MyWindow app1=new MyWindow(); app1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }

Event Handlers Event-driven programming A change in the state of a GUI component triggers a call to an event handler method Event source is the object that the user interacts with to trigger the event. Event handler is an object with an event handler method designed to handle a specific event type.

Implementing Event Handlers 1 – Code a class for the event handler. The class must implement the correct listener. This can be the same class file as out application runs in. 2 – Create an event handler object from the class. 3 – Register the handler object with the GUI component.

Event Handlers – Step 1 Create a class to implement the Handler public class MyProgram extends JFrame implements ActionListener { … /** This method will be invoked when a button is clicked */ public void actionPerformed(ActionEvent e) { System.out.println("The " + e.getActionCommand() + " button is " + "clicked at\n " + new java.util.Date(e.getWhen())); } … }

Event Handlers – Step 2 Create event handler object //ButtonListener btListener = new ButtonListener(); This step may be skipped if we are using the Program class as the event object.

Event Handlers – Step 3 Register the event handler object with the source object. In this example a button. … private JButton jbtOk = new JButton("OK"); jbtOk.addActionListener(this); …

Example import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestActionEvent extends JFrame implements ActionListener { // Create two buttons private JButton jbtOk = new JButton("OK"); private JButton jbtCancel = new JButton("Cancel"); public TestActionEvent() { // Set the window title setTitle("TestActionEvent"); // Set FlowLayout manager to arrange the components inside the frame getContentPane().setLayout(new FlowLayout()); // Add buttons to the frame getContentPane().add(jbtOk); getContentPane().add(jbtCancel); // Create a listener object //ButtonListener btListener = new ButtonListener(); // Register listeners jbtOk.addActionListener(this); jbtCancel.addActionListener(this); } /** Main method */ public static void main(String[] args) { TestActionEvent frame = new TestActionEvent(); frame.setDefaultCloseOperation(JFrame.E XIT_ON_CLOSE); frame.setSize(100, 80); frame.setVisible(true); } /** This method will be invoked when a button is clicked */ public void actionPerformed(ActionEvent e) { System.out.println("The " + e.getActionCommand() + " button is " + "clicked at\n " + new java.util.Date(e.getWhen())); }

Getting and Setting the value of a TextField Getting a value from a Textfield String s; s= TheTextField.getText(); Setting the value in a TextField TheTextField.setText(“A value”);

Summary AWT and Swing API Containers – JFrame Labels, TextFields and Buttons Layout Managers Graphics commands Handling Events Layout Managers