Java Swing and Events by tsk11317

VIEWS: 9 PAGES: 18

									Java Swing and Events

       Chris North
      cs3724: HCI
              Presentations

• nadine edwards,
• steve terhar




• Vote: UI Hall of Fame/Shame?
                          Review
• Java Application vs. Applet?
     • Running in browser, security
     • Web Start
     • Can make a class that does both


• Where does an application start execution?
     • Main static in a class
     • Java myclass
• Where does an applet start execution?
     • Japplet.init(), .start()
                  AWT to Swing
• AWT: Abstract Windowing Toolkit
     • import java.awt.*
• Swing: new with Java2
     •   import javax.swing.*
     •   Extends AWT
     •   Tons o’ new improved components
     •   Standard dialog boxes, tooltips, …
     •   Look-and-feel, skins
     •   Event listeners
• http://java.sun.com/j2se/1.3/docs/api/index.html
Swing Set Demo
     Anatomy of a Java Swing GUI
•   JFrame
•   JPanel        JFrame

•   Layout Mgr     JPanel

•   JComponents       Layout Manager



                            JButton
            Build from bottom up
                                   Layout
• Create:                                   Listener
     •   Frame
     •   Panel
                                            JButton
     •   Layout manager
     •   Components
     •   Listeners
                                       JPanel
• Add: (bottom up)
     •   layout manager to panel
     •   listeners to components
     •   components to panel          JFrame
     •   panel to frame
                  Code
JFrame f = new JFrame(“title”)
JPanel p = new JPanel( );
JButton b = new JButton(“press me”);

p.add(b);              // add button to panel
f.setContentPane(p);   // add panel to frame

f.setVisible(true);
                Layout Managers
• Variety of screen sizes, look-and-feels
• Frees programmer from handling ugly details
• Some layout managers:
     •   null (no manager, programmer sets x,y,w,h)
     •   Flowlayout
     •   GridLayout
     •   BorderLayout
                                                      n

                                              w       c   e

                                                      s
                  Code
JFrame f = new JFrame(“title”)
JPanel p = new JPanel( );
JButton b = new JButton(“press me”);

b.setBounds(new Rectangle(10,10, 100,50));
p.setLayout(null);       // x,y layout
p.add(b);
f.setContentPane(p);
                     Events
• Register with a component to receive events
• Give component a ref to a Listener object

•   ActionListener
•   KeyListener                              click

•   MouseListener                        JButton
•   WindowListener            register
                                             ActionEvent
•   …
                                         Listener
                   Code

myListener = new myListenClass;
btn.addActionListener(myListener);



Class myListenClass implements ActionListener {
  public void actionPerformed(ActionEvent e){
     // button pressed, do stuff here
  }
}
       Simplifying: Inheritance

Class myframe extends JFrame{
  public myframe(){
     // create panel, buttons, …
     setContentPane(p);        // I am a jframe
  }
  public static void main(){
     JFrame f = new myframe();
  }
}
• Myframe creates JFrame via inheritance
       Simplifying: Inheritance
Class myframe extends JFrame{
  public myframe(){
     // create panel, buttons, …
  }
  public static void main(){
     JFrame f = new myframe();
  }
  public void paint(Graphics g){
     super.paint(g);   //call overriden method
     // paint stuff here
  }
}
• Override JFrame methods to add functionality
        Simplifying: Implements
Class myframe extends JFrame
                                      Like a pure
     implements ActionListener {
                                      abstract base
                                      class
    public myframe(){                 (methods,
       // create panel, buttons, …    no code)
       btn.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
       // button pressed, do stuff here
    }
}
    Simplifying: Anonymous classes
Class myframe extends JFrame {

    public myframe(){
        // create panel, buttons, …
        btn.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent e){
              // button pressed, do stuff here
            }
          }
        );                       Defining and
    }                            instantiating a
}
                                 class on the fly
                  In JBuilder
•   Application
•   JFrame, JPanel, JButton
•   Layout managers
•   Event listeners
                    Next
• Hw2: rest due today!
• Midterm: next class
• Proj 2: design due feb 28

Presentations: UI critique, HW2 results
• Next Tues: midterm
• Next Thurs: brian ward, peter hou

								
To top