180 likes | 365 Views
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?
E N D
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
Anatomy of a Java Swing GUI • JFrame • JPanel • Layout Mgr • JComponents JFrame JPanel Layout Manager JButton
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
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 c w 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 • MouseListener • WindowListener • … click JButton 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 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)
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