18
Java Swing and Events Chris North cs3724: HCI

Lecture12 java swing

Embed Size (px)

DESCRIPTION

Hariprasanna V (9843824677)

Citation preview

Page 1: Lecture12 java swing

Java Swing and Events

Chris North

cs3724: HCI

Page 2: Lecture12 java swing

Presentations

• nadine edwards, • steve terhar

• Vote: UI Hall of Fame/Shame?

Page 3: Lecture12 java swing

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

Page 4: Lecture12 java swing

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

Page 5: Lecture12 java swing

Swing Set Demo

Page 6: Lecture12 java swing

Anatomy of a Java Swing GUI

• JFrame

• JPanel

• Layout Mgr

• JComponents

JPanel

JButton

JFrame

Layout Manager

Page 7: Lecture12 java swing

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

Page 8: Lecture12 java swing

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

Page 9: Lecture12 java swing

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

Page 10: Lecture12 java swing

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

Page 11: Lecture12 java swing

Events

• Register with a component to receive events

• Give component a ref to a Listener object

• ActionListener

• KeyListener

• MouseListener

• WindowListener

• …

JButton

Listener

click

ActionEventregister

Page 12: Lecture12 java swing

Code

myListener = new myListenClass;

btn.addActionListener(myListener);

Class myListenClass implements ActionListener {

public void actionPerformed(ActionEvent e){

// button pressed, do stuff here

}

}

Page 13: Lecture12 java swing

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

Page 14: Lecture12 java swing

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

Page 15: Lecture12 java swing

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)

Page 16: Lecture12 java swing

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

Page 17: Lecture12 java swing

In JBuilder

• Application

• JFrame, JPanel, JButton

• Layout managers

• Event listeners

Page 18: Lecture12 java swing

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