Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?

Similar presentations


Presentation on theme: "Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?"— 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 JPanel JButton JFrame Layout Manager

7 Build from bottom up Create: Frame Panel Layout manager Components Listeners Add: (bottom up) layout manager to panel listeners to components components to panel panel to frame JPanel JButton Layout Listener 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 c n s ew

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 … JButton Listener click ActionEvent register

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


Download ppt "Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?"

Similar presentations


Ads by Google