SlideShare uma empresa Scribd logo
1 de 10
Getting GUI! LIS4930 © PIC
It All Starts With A Window LIS4930 © PIC A JFrame is the object that represents a window on the screen.
Put Widgets in the Window LIS4930 © PIC Once you have a JFrame, you can put things (‘widgets’) in it by adding them to the JFrame. The most common include JButton, JRadioButton, JLable, JList, JScrollButton, JSlider, JTextField, and JTable. Look at the javax.swing package for more widgets.
Four Steps to Creating a GUI LIS4930 © PIC 1 2 3 4 Make a frame (JFrame) JFrame frame = new JFrame( ); Make a widget (button, text field, etc.) Jbutton Button = new JButton(“Click Me”); Add a widget to the frame frame.getContentPane( ).add(Button); Display it (give it a size and make it visible) 	frame.setSize(300, 300); frame.setVisible(true); DEMO TIME
Clicking the Button Doesn’t do Anything!!!! LIS4930 © PIC How do I get the button to do something specific when the user clicks it? 1 2 A METHOD to be called when the user clicks  A way to KNOW when the user clicks We Need To Know Two Things
How To Know When The User Clicks LIS4930 © PIC In Java, the process of getting and handling a user event is called event-handling. If you care about the button’s events, implement an interface that says, “I’m listening for your events”. A listener interface is the bridge between the listener (you) and the event source (the button) An event source (like the button) creates an event object when the user does something that matters (like click the button). Every event type has a matching listener interface. If you want MouseEvents, implement the MouseListener interface. Want WindowEvents, implement WindowListener. You get the idea.
How the Listener and Source Communicate The Listener First tell the button you are listening to it by calling its addActionListener(this) method and giving it a reference to the ActionListener (in this case it is you “this”) As an ActionListener you MUST implement the ActionListener interface and its only method, actionPerformed( ). The Event Source Each of the interested ActionListeners are stored in a list by the button and when the event occurs, the button calls all the actionPerformed( ) methods of the ActionListeners. LIS4930 © PIC
Getting a Button’s ActionEvent LIS4930 © PIC 1 2 3 Implement the ActionListener interface Register with the button (tell it you want to listen for events) Define the event-handling method (implement the actionPerformed( ) method from the ActionListener interface)
Listening to the Button. LIS4930 © PIC DEMO TIME
Getting back to graphics… LIS4930 © PIC There are three ways to put things on a GUI. 1 2 3 Put widgets on a frame 	Add buttons, menus, radio buttons, etc. frame.getContentPane( ).add(myButton); Draw 2D graphics on a widget 	Use a graphics object to paint shapes 	graphics.fillOval(70,70,100,100); Put a JPEG on a widget 	You can put your own images on a widget 	graphics.drawImage(myPic,10,10,this);

Mais conteúdo relacionado

Semelhante a 15a gui

Enhance the ButtonViewer program so that it prints the time at which t.docx
Enhance the ButtonViewer program so that it prints the time at which t.docxEnhance the ButtonViewer program so that it prints the time at which t.docx
Enhance the ButtonViewer program so that it prints the time at which t.docxtodd401
 
10 awt event model
10 awt event model10 awt event model
10 awt event modelBayarkhuu
 
ACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptxACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptxMattFlordeliza1
 
Multimedia lecture ActionScript3
Multimedia lecture ActionScript3Multimedia lecture ActionScript3
Multimedia lecture ActionScript3Mohammed Hussein
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Javasuraj pandey
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Introduction to GUIs with guizero
Introduction to GUIs with guizeroIntroduction to GUIs with guizero
Introduction to GUIs with guizeroprimeteacher32
 
3.4 events and interactivity
3.4   events and interactivity3.4   events and interactivity
3.4 events and interactivityallenbailey
 
Actionscript 3 - Session 2 Getting Started Flash IDE
Actionscript 3 - Session 2 Getting Started Flash IDEActionscript 3 - Session 2 Getting Started Flash IDE
Actionscript 3 - Session 2 Getting Started Flash IDEOUM SAOKOSAL
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.pptusama537223
 
Keyboard and mouse events in python
Keyboard and mouse events in pythonKeyboard and mouse events in python
Keyboard and mouse events in pythonmal6ayer
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2PRN USM
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVASrajan Shukla
 
Mobile Application Development
Mobile Application DevelopmentMobile Application Development
Mobile Application DevelopmentMuhammad Sajid
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFCSunil OS
 

Semelhante a 15a gui (20)

Lecture 8.pdf
Lecture 8.pdfLecture 8.pdf
Lecture 8.pdf
 
Enhance the ButtonViewer program so that it prints the time at which t.docx
Enhance the ButtonViewer program so that it prints the time at which t.docxEnhance the ButtonViewer program so that it prints the time at which t.docx
Enhance the ButtonViewer program so that it prints the time at which t.docx
 
10 awt event model
10 awt event model10 awt event model
10 awt event model
 
ACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptxACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptx
 
Multimedia lecture ActionScript3
Multimedia lecture ActionScript3Multimedia lecture ActionScript3
Multimedia lecture ActionScript3
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Androd Listeners
Androd ListenersAndrod Listeners
Androd Listeners
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Introduction to GUIs with guizero
Introduction to GUIs with guizeroIntroduction to GUIs with guizero
Introduction to GUIs with guizero
 
3.4 events and interactivity
3.4   events and interactivity3.4   events and interactivity
3.4 events and interactivity
 
Actionscript 3 - Session 2 Getting Started Flash IDE
Actionscript 3 - Session 2 Getting Started Flash IDEActionscript 3 - Session 2 Getting Started Flash IDE
Actionscript 3 - Session 2 Getting Started Flash IDE
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
Keyboard and mouse events in python
Keyboard and mouse events in pythonKeyboard and mouse events in python
Keyboard and mouse events in python
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
Mobile Application Development
Mobile Application DevelopmentMobile Application Development
Mobile Application Development
 
What is Event
What is EventWhat is Event
What is Event
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 

Mais de Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Database basics
Database basicsDatabase basics
Database basics
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 
Xhtml
XhtmlXhtml
Xhtml
 
Webdev
WebdevWebdev
Webdev
 
Web architecture
Web architectureWeb architecture
Web architecture
 
Sdlc
SdlcSdlc
Sdlc
 
Mysocial
MysocialMysocial
Mysocial
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Html5
Html5Html5
Html5
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
Javascript2
Javascript2Javascript2
Javascript2
 

15a gui

  • 2. It All Starts With A Window LIS4930 © PIC A JFrame is the object that represents a window on the screen.
  • 3. Put Widgets in the Window LIS4930 © PIC Once you have a JFrame, you can put things (‘widgets’) in it by adding them to the JFrame. The most common include JButton, JRadioButton, JLable, JList, JScrollButton, JSlider, JTextField, and JTable. Look at the javax.swing package for more widgets.
  • 4. Four Steps to Creating a GUI LIS4930 © PIC 1 2 3 4 Make a frame (JFrame) JFrame frame = new JFrame( ); Make a widget (button, text field, etc.) Jbutton Button = new JButton(“Click Me”); Add a widget to the frame frame.getContentPane( ).add(Button); Display it (give it a size and make it visible) frame.setSize(300, 300); frame.setVisible(true); DEMO TIME
  • 5. Clicking the Button Doesn’t do Anything!!!! LIS4930 © PIC How do I get the button to do something specific when the user clicks it? 1 2 A METHOD to be called when the user clicks A way to KNOW when the user clicks We Need To Know Two Things
  • 6. How To Know When The User Clicks LIS4930 © PIC In Java, the process of getting and handling a user event is called event-handling. If you care about the button’s events, implement an interface that says, “I’m listening for your events”. A listener interface is the bridge between the listener (you) and the event source (the button) An event source (like the button) creates an event object when the user does something that matters (like click the button). Every event type has a matching listener interface. If you want MouseEvents, implement the MouseListener interface. Want WindowEvents, implement WindowListener. You get the idea.
  • 7. How the Listener and Source Communicate The Listener First tell the button you are listening to it by calling its addActionListener(this) method and giving it a reference to the ActionListener (in this case it is you “this”) As an ActionListener you MUST implement the ActionListener interface and its only method, actionPerformed( ). The Event Source Each of the interested ActionListeners are stored in a list by the button and when the event occurs, the button calls all the actionPerformed( ) methods of the ActionListeners. LIS4930 © PIC
  • 8. Getting a Button’s ActionEvent LIS4930 © PIC 1 2 3 Implement the ActionListener interface Register with the button (tell it you want to listen for events) Define the event-handling method (implement the actionPerformed( ) method from the ActionListener interface)
  • 9. Listening to the Button. LIS4930 © PIC DEMO TIME
  • 10. Getting back to graphics… LIS4930 © PIC There are three ways to put things on a GUI. 1 2 3 Put widgets on a frame Add buttons, menus, radio buttons, etc. frame.getContentPane( ).add(myButton); Draw 2D graphics on a widget Use a graphics object to paint shapes graphics.fillOval(70,70,100,100); Put a JPEG on a widget You can put your own images on a widget graphics.drawImage(myPic,10,10,this);