Getting GUI!<br />LIS4930 © PIC<br />
It All Starts With A Window<br />LIS4930 © PIC<br />A JFrame is the object that represents a window on the screen.<br />
Put Widgets in the Window<br />LIS4930 © PIC<br />Once you have a JFrame, you can put things (‘widgets’) in it by adding t...
Four Steps to Creating a GUI<br />LIS4930 © PIC<br />1<br />2<br />3<br />4<br />Make a frame (JFrame)<br />JFrame frame =...
Clicking the Button Doesn’t do Anything!!!!<br />LIS4930 © PIC<br />How do I get the button to do something specific when ...
How To Know When The User Clicks<br />LIS4930 © PIC<br />In Java, the process of getting and handling a user event is call...
How the Listener and Source Communicate<br />The Listener<br />First tell the button you are listening to it by calling it...
Getting a Button’s ActionEvent<br />LIS4930 © PIC<br />1<br />2<br />3<br />Implement the ActionListener interface<br />Re...
Listening to the Button.<br />LIS4930 © PIC<br />DEMO TIME<br />
Getting back to graphics…<br />LIS4930 © PIC<br />There are three ways to put things on a GUI.<br />1<br />2<br />3<br />P...
Upcoming SlideShare
Loading in …5
×

15a gui

638 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
638
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

15a gui

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

×