Lecture12 java swing

1,975 views
1,919 views

Published on

Hariprasanna V (9843824677)

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,975
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
113
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lecture12 java swing

  1. 1. Java Swing and Events Chris North cs3724: HCI
  2. 2. Presentations <ul><li>nadine edwards, </li></ul><ul><li>steve terhar </li></ul><ul><li>Vote: UI Hall of Fame/Shame? </li></ul>
  3. 3. Review <ul><li>Java Application vs. Applet? </li></ul><ul><ul><ul><li>Running in browser, security </li></ul></ul></ul><ul><ul><ul><li>Web Start </li></ul></ul></ul><ul><ul><ul><li>Can make a class that does both </li></ul></ul></ul><ul><li>Where does an application start execution? </li></ul><ul><ul><ul><li>Main static in a class </li></ul></ul></ul><ul><ul><ul><li>Java myclass </li></ul></ul></ul><ul><li>Where does an applet start execution? </li></ul><ul><ul><ul><li>Japplet.init(), .start() </li></ul></ul></ul>
  4. 4. AWT to Swing <ul><li>AWT: Abstract Windowing Toolkit </li></ul><ul><ul><ul><li>import java.awt.* </li></ul></ul></ul><ul><li>Swing: new with Java2 </li></ul><ul><ul><ul><li>import javax.swing.* </li></ul></ul></ul><ul><ul><ul><li>Extends AWT </li></ul></ul></ul><ul><ul><ul><li>Tons o’ new improved components </li></ul></ul></ul><ul><ul><ul><li>Standard dialog boxes, tooltips, … </li></ul></ul></ul><ul><ul><ul><li>Look-and-feel, skins </li></ul></ul></ul><ul><ul><ul><li>Event listeners </li></ul></ul></ul><ul><li>http://java.sun.com/j2se/1.3/docs/api/index.html </li></ul>
  5. 5. Swing Set Demo
  6. 6. Anatomy of a Java Swing GUI <ul><li>JFrame </li></ul><ul><li>JPanel </li></ul><ul><li>Layout Mgr </li></ul><ul><li>JComponents </li></ul>JPanel JButton JFrame Layout Manager
  7. 7. Build from bottom up <ul><li>Create: </li></ul><ul><ul><ul><li>Frame </li></ul></ul></ul><ul><ul><ul><li>Panel </li></ul></ul></ul><ul><ul><ul><li>Layout manager </li></ul></ul></ul><ul><ul><ul><li>Components </li></ul></ul></ul><ul><ul><ul><li>Listeners </li></ul></ul></ul><ul><li>Add: (bottom up) </li></ul><ul><ul><ul><li>layout manager to panel </li></ul></ul></ul><ul><ul><ul><li>listeners to components </li></ul></ul></ul><ul><ul><ul><li>components to panel </li></ul></ul></ul><ul><ul><ul><li>panel to frame </li></ul></ul></ul>JPanel JButton Layout Listener JFrame
  8. 8. Code <ul><li>JFrame f = new JFrame(“title”) </li></ul><ul><li>JPanel p = new JPanel( ); </li></ul><ul><li>JButton b = new JButton(“press me”); </li></ul><ul><li>p.add(b); // add button to panel </li></ul><ul><li>f.setContentPane(p); // add panel to frame </li></ul><ul><li>f.setVisible(true); </li></ul>
  9. 9. Layout Managers <ul><li>Variety of screen sizes, look-and-feels </li></ul><ul><li>Frees programmer from handling ugly details </li></ul><ul><li>Some layout managers: </li></ul><ul><ul><ul><li>null (no manager, programmer sets x,y,w,h) </li></ul></ul></ul><ul><ul><ul><li>Flowlayout </li></ul></ul></ul><ul><ul><ul><li>GridLayout </li></ul></ul></ul><ul><ul><ul><li>BorderLayout </li></ul></ul></ul>c n s e w
  10. 10. Code <ul><li>JFrame f = new JFrame(“title”) </li></ul><ul><li>JPanel p = new JPanel( ); </li></ul><ul><li>JButton b = new JButton(“press me”); </li></ul><ul><li>b.setBounds(new Rectangle(10,10, 100,50)); </li></ul><ul><li>p.setLayout(null); // x,y layout </li></ul><ul><li>p.add(b); </li></ul><ul><li>f.setContentPane(p); </li></ul>
  11. 11. Events <ul><li>Register with a component to receive events </li></ul><ul><li>Give component a ref to a Listener object </li></ul><ul><li>ActionListener </li></ul><ul><li>KeyListener </li></ul><ul><li>MouseListener </li></ul><ul><li>WindowListener </li></ul><ul><li>… </li></ul>JButton Listener click ActionEvent register
  12. 12. Code <ul><li>myListener = new myListenClass; </li></ul><ul><li>btn.addActionListener(myListener); </li></ul><ul><li>Class myListenClass implements ActionListener { </li></ul><ul><li>public void actionPerformed(ActionEvent e){ </li></ul><ul><li>// button pressed, do stuff here </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  13. 13. Simplifying: Inheritance <ul><li>Class myframe extends JFrame{ </li></ul><ul><li>public myframe(){ </li></ul><ul><li>// create panel, buttons, … </li></ul><ul><li>setContentPane(p); // I am a jframe </li></ul><ul><li>} </li></ul><ul><li>public static void main(){ </li></ul><ul><li>JFrame f = new myframe(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Myframe creates JFrame via inheritance </li></ul>
  14. 14. Simplifying: Inheritance <ul><li>Class myframe extends JFrame{ </li></ul><ul><li>public myframe(){ </li></ul><ul><li>// create panel, buttons, … </li></ul><ul><li>} </li></ul><ul><li>public static void main(){ </li></ul><ul><li>JFrame f = new myframe(); </li></ul><ul><li>} </li></ul><ul><li>public void paint (Graphics g){ </li></ul><ul><li>super.paint(g); //call overriden method </li></ul><ul><li>// paint stuff here </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Override JFrame methods to add functionality </li></ul>
  15. 15. Simplifying: Implements <ul><li>Class myframe extends JFrame </li></ul><ul><li>implements ActionListener { </li></ul><ul><li>public myframe(){ </li></ul><ul><li>// create panel, buttons, … </li></ul><ul><li>btn.addActionListener( this ); </li></ul><ul><li>} </li></ul><ul><li>public void actionPerformed (ActionEvent e){ </li></ul><ul><li>// button pressed, do stuff here </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Like a pure abstract base class (methods, no code)
  16. 16. Simplifying: Anonymous classes <ul><li>Class myframe extends JFrame { </li></ul><ul><li>public myframe(){ </li></ul><ul><li>// create panel, buttons, … </li></ul><ul><li>btn.addActionListener( </li></ul><ul><li> new ActionListener() { </li></ul><ul><li>public void actionPerformed (ActionEvent e){ </li></ul><ul><li>// button pressed, do stuff here </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Defining and instantiating a class on the fly
  17. 17. In JBuilder <ul><li>Application </li></ul><ul><li>JFrame, JPanel, JButton </li></ul><ul><li>Layout managers </li></ul><ul><li>Event listeners </li></ul>
  18. 18. Next <ul><li>Hw2: rest due today! </li></ul><ul><li>Midterm: next class </li></ul><ul><li>Proj 2: design due feb 28 </li></ul><ul><li>Presentations: UI critique, HW2 results </li></ul><ul><li>Next Tues: midterm </li></ul><ul><li>Next Thurs: brian ward, peter hou </li></ul>

×