1 / 18

Java Swing and Events

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?

stockton
Télécharger la présentation

Java Swing and Events

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Swing and Events Chris North cs3724: HCI

  2. Presentations • nadine edwards, • steve terhar • Vote: UI Hall of Fame/Shame?

  3. 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()

  4. 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

  5. Swing Set Demo

  6. Anatomy of a Java Swing GUI • JFrame • JPanel • Layout Mgr • JComponents JFrame JPanel Layout Manager JButton

  7. Build from bottom up Layout • Create: • Frame • Panel • Layout manager • Components • Listeners • Add: (bottom up) • layout manager to panel • listeners to components • components to panel • panel to frame Listener JButton JPanel JFrame

  8. 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);

  9. 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 c w e s

  10. 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);

  11. Events • Register with a component to receive events • Give component a ref to a Listener object • ActionListener • KeyListener • MouseListener • WindowListener • … click JButton register ActionEvent Listener

  12. Code myListener = new myListenClass; btn.addActionListener(myListener); Class myListenClass implements ActionListener { public void actionPerformed(ActionEvent e){ // button pressed, do stuff here } }

  13. 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

  14. 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

  15. Simplifying: Implements Class myframe extends JFrame implements ActionListener { public myframe(){ // create panel, buttons, … btn.addActionListener(this); } public void actionPerformed(ActionEvent e){ // button pressed, do stuff here } } Like a pure abstract base class (methods, no code)

  16. 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

  17. In JBuilder • Application • JFrame, JPanel, JButton • Layout managers • Event listeners

  18. 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

More Related