15a gui
Upcoming SlideShare
Loading in...5
×
 
  • 641 views

 

Statistics

Views

Total Views
641
Views on SlideShare
641
Embed Views
0

Actions

Likes
0
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

15a gui 15a gui Presentation Transcript

  • 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);