SlideShare a Scribd company logo
Getting GUI!LIS4930 © PIC
It All Starts With A WindowLIS4930 © PICA JFrame is the object that represents a window on the screen.
Put Widgets in the WindowLIS4930 © PICOnce 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 GUILIS4930 © PIC1234Make 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 frameframe.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 © PICHow do I get the button to do something specific when the user clicks it?12A METHOD to be called when the user clicks A way to KNOW when the user clicksWe Need To Know Two Things
How To Know When The User ClicksLIS4930 © PICIn 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 CommunicateThe ListenerFirst 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 SourceEach 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 ActionEventLIS4930 © PIC123Implement the ActionListener interfaceRegister 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 © PICDEMO TIME
Getting back to graphics…LIS4930 © PICThere are three ways to put things on a GUI.123Put 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);

More Related Content

PPTX
PPT
Java Event Handling
PDF
Swing
PPTX
Python keyboard and mouse events
DOCX
Lecture8 oopj
PDF
Mac OS Lion Tips and Tricks
PDF
Ordenara los vectores
PDF
Programming Without Coding Technology (PWCT) - Simple GUI Application
Java Event Handling
Swing
Python keyboard and mouse events
Lecture8 oopj
Mac OS Lion Tips and Tricks
Ordenara los vectores
Programming Without Coding Technology (PWCT) - Simple GUI Application

Similar to 15a gui (20)

PDF
Lecture 8.pdf
DOCX
Enhance the ButtonViewer program so that it prints the time at which t.docx
PPTX
10 awt event model
PPTX
ACtionlistener in java use in discussion.pptx
PDF
Multimedia lecture ActionScript3
PPTX
Chapter 11.5
PPTX
Androd Listeners
PPT
Basic of Abstract Window Toolkit(AWT) in Java
PPTX
Event Handling in java
PPTX
Introduction to GUIs with guizero
PPTX
3.4 events and interactivity
PPT
Actionscript 3 - Session 2 Getting Started Flash IDE
PPT
event handling new.ppt
PPTX
Keyboard and mouse events in python
PPT
Graphical User Interface (GUI) - 2
PPTX
Event Handling in JAVA
PDF
Mobile Application Development
PPTX
What is Event
PPT
Java Swing JFC
PDF
OOPS unit V JavaFX Event Handling Controls and Components
Lecture 8.pdf
Enhance the ButtonViewer program so that it prints the time at which t.docx
10 awt event model
ACtionlistener in java use in discussion.pptx
Multimedia lecture ActionScript3
Chapter 11.5
Androd Listeners
Basic of Abstract Window Toolkit(AWT) in Java
Event Handling in java
Introduction to GUIs with guizero
3.4 events and interactivity
Actionscript 3 - Session 2 Getting Started Flash IDE
event handling new.ppt
Keyboard and mouse events in python
Graphical User Interface (GUI) - 2
Event Handling in JAVA
Mobile Application Development
What is Event
Java Swing JFC
OOPS unit V JavaFX Event Handling Controls and Components
Ad

More from Program in Interdisciplinary Computing (20)

Ad

15a gui

  • 2. It All Starts With A WindowLIS4930 © PICA JFrame is the object that represents a window on the screen.
  • 3. Put Widgets in the WindowLIS4930 © PICOnce 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 GUILIS4930 © PIC1234Make 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 frameframe.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 © PICHow do I get the button to do something specific when the user clicks it?12A METHOD to be called when the user clicks A way to KNOW when the user clicksWe Need To Know Two Things
  • 6. How To Know When The User ClicksLIS4930 © PICIn 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 CommunicateThe ListenerFirst 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 SourceEach 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 ActionEventLIS4930 © PIC123Implement the ActionListener interfaceRegister 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 © PICDEMO TIME
  • 10. Getting back to graphics…LIS4930 © PICThere are three ways to put things on a GUI.123Put 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);