FEN 2006-11-15IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Object-Oriented Software Engineering Java with added Swing.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Graphical User Interface (GUI) Design using Swing Course Lecture Slides.
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
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.
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
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.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Basics of GUI Programming Chapter 11 and Chapter 22.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
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.
Lesson 28: More on the GUI button, frame and actions.
1 Lecture 8: User Interface Components with Swing.
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
CSC 205 Programming II Lecture 5 AWT - I.
A First Look at GUI Applications
Graphical User Interface
Graphics Programming - Frames
Steps to Creating a GUI Interface
1 Graphical User Interfaces
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners

FEN IntroJava2006 AAU2 Components GUIs are build using standard components, for instance: –Windows (Frames in Java) –Menu bars –Text fields –List boxes –Buttons –Etc.

FEN IntroJava2006 AAU3 Java.awt and javax.swing

FEN IntroJava2006 AAU4 Some AWT and Swing Classes java.lang.Objectjava.awt.Component java.awt.Container javax.swing.JComponent java.awt.Window java.awt.Frame javax.swing.JFrame javax.swing.JLabel javax.swing.JToolbar javax.swing.JPopupMenu javax.swing.JFileChooser javax.swing.JPanel Note the use of inheritance

FEN IntroJava2006 AAU5 Graphical User Interfaces A Graphical User Interface (GUI) is created with at least three kinds of objects –components –events –Listeners Java standard class library provides these objects for us

FEN IntroJava2006 AAU6 Components and Containers A GUI component defines a screen element to display information or allow the user to interact with the program –buttons, text fields, labels, menus, etc. A container is a special component that holds and organizes other components –dialog boxes, applets, frames, panels, etc.

FEN IntroJava2006 AAU7 Hello World Example import javax.swing.*; public class HelloWorldSwing { public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200,200); frame.setVisible(true); }

FEN IntroJava2006 AAU8 Frame The main component is a frame. Frame is the Java term for what is generally called a ‘window’. In Swing frames are realised by the class JFrame A frame is a rectangular area: –title bar at the top –The title bar contains buttons for closing, maximizing and minimizing of the frame –Below the title bare is an area at which further graphical components can be placed –This area is divided into two parts: space for a menu bar at the upper edge content pane below

FEN IntroJava2006 AAU9 Elements of a JFrame Title Menu bar Content pane Window controls

FEN IntroJava2006 AAU10 import java.awt.*; import javax.swing.*; public class FirstFrame extends JFrame { public FirstFrame() { super("My First Frame"); setSize(200,200); setVisible(true); } Import libraries Inherit JFrame

FEN IntroJava2006 AAU11 import java.awt.*; import javax.swing.*; public class SecondFrame extends JFrame{ //Components neede to build the menubar private JMenuBar menuBar; private JMenu fileMenu, editMenu; private JMenuItem openItem, quitItem; public SecondFrame() { super("My Second Frame"); buildMenu(); setSize(200,200); setVisible(true); } private void buildMenu(){ menuBar = new JMenuBar(); fileMenu = new JMenu("File"); editMenu = new JMenu("Edit"); menuBar.add(fileMenu); menuBar.add(editMenu); openItem = new JMenuItem("Open"); quitItem = new JMenuItem("Quit"); fileMenu.add(openItem); fileMenu.add(quitItem); this.setJMenuBar(menuBar); }

FEN IntroJava2006 AAU12 private void buildInputpart(){ panelIO = new JPanel(); panelIO.setLayout(null); labFirstName = new JLabel("First Name:"); labLastName = new JLabel("Last Name: "); txtFirstName = new JTextField(); txtFirstName.setToolTipText("Input first name"); txtLastName = new JTextField(); txtLastName.setToolTipText("Input last name"); addComponent(panelIO, labFirstName, 20,20,70,20); addComponent(panelIO, txtFirstName, 100,20,150,20); addComponent(panelIO, labLastName, 20,45,70,20); addComponent(panelIO, txtLastName, 100,45,150,20); this.add(panelIO); } private void buildButtonpart(){ panelButton = new JPanel(); panelButton.setLayout(null); submitBut = new JButton("Submit"); clearBut = new JButton("Clear"); addComponent(panelButton, submitBut, 20,80, 100,20); addComponent(panelButton, clearBut, 130,80, 100,20); this.add(panelButton); } private void addComponent(Container container, Component c,int x,int y,int width,int height){ c.setBounds(x,y,width, height); container.add(c); }

FEN IntroJava2006 AAU13 This is unbearable! Instead an IDE supporting Drag and Drop For instance NetBeans: GUI Building in NetBeans IDE 5.0 or

FEN IntroJava2006 AAU14 Creating a GUI Project 1.Choose File > New Project. Alternately, you can click the New Project icon in the IDE toolbar. 2.In the Categories pane, select the General node and in the Projects pane, choose Java Application. Click Next. 3.Enter ContactEditor in the Project Name field and specify the project location. 4.Ensure that the Set as Main Project checkbox is selected and deselect Create Main Class if it is selected. 5.Click Finish. The IDE creates the ContactEditor folder on your system in the designated location. This folder contains all of the project's associated files, including its Ant script, folders for storing sources and tests, and a folder for project- specific metadata. To view the project structure, use the IDE's Files window.

FEN IntroJava2006 AAU15 Adding a Main Component To create a JFrame container: 1.In the Projects window, right-click the ContactEditor node and choose New > JFrame Form. 2.Enter ContactEditorUI as the Class Name. 3.Enter my.contacteditor as the package. 4.Click Finish. The IDE creates the ContactEditorUI form and the ContactEditorUI class within the ContactEditorUI.java application and opens the ContactEditorUI form in the GUI Builder. Notice that the my.contacteditor package replaces the default package.

FEN IntroJava2006 AAU16 Event Driven Programs with graphical user interfaces (GUIs) are event driven: –the program reacts to actions of the user –these actions generate (‘fire’) events –examples of events are: pressing a key moving the mouse clicking on a button selecting a menu item –Components that reacts to events are called Listeners

FEN IntroJava2006 AAU17 Eventhandling User interface GUI Components fires the event in response to being clicked Listener React to the events Java code Components

FEN IntroJava2006 AAU18 Hello World – now with buttons! private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText("Hello World!"); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } jButton1.setText("Say Hello"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); Listener generated by NetBeans Called when the event occurs We write the body of the event handler

FEN IntroJava2006 AAU19 Do it! The tutorial: Adding Functionality to Buttons: A Beginners GuideAdding Functionality to Buttons: A Beginners Guide