L24 - Computer Science 101 SS C Lecture 24 Contents! 2!...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CompSci101 Lecture 25 1 Computer Science 101 SS C Lecture 24 Contents Which component generated the ActionEvent - the getSource() instance method. Repainting the screen. Drawing objects. Course Book: §16 2 ActionEvent - Which component? The actionPerformed() method deFned inside the JPanel class is executed every time an ActionEvent occurs on a component which has previously called the instance method, . ...addActionListener(this). We use the ActionEvent object (the parameter supplied by the Java runtime inside the actionPerformed() method) to call the getSource() instance method. Often we want to do different things depending on which component generated the event (e.g. which JButton a user has clicked). The getSource() method returns a reference to the component object which generated the event. 3 Example 1 - Which component? This JPanel displays the total number of minutes, the total number of hours as a double or the JPanel resets the three JText±ields to 0. What is displayed depends on which JButton the user presses. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 4 Example 1 - Which component? import java.awt.*; import java.swing.*; public class L24JPanel01 extends JPanel { private JTextField messageT, minutesT, hoursT; private JButton hoursB, minutesB, clearB; public L24JPanel01() { hoursT = new JTextField(2); minutesT = new JTextField(2); messageT = new JTextField(16); hoursB = new JButton("HOURS"); minutesB = new JButton("MINUTES"); clearB = new JButton("CLEAR"); import java.awt.event .*; implements ActionListener hoursB. addActionListener( this ); minutesB. addActionListener( this ); clearB. addActionListener( this ); 5 16 17 18 - 23 24 25 - 27 Example 1 - Which component? hoursT.setText("5"); minutesT.setText("9"); add(hoursT); add(minutesT); add(messageT); add(hoursB); add(minutesB); add(clearB); } private String to2DecPlaces(double num) { //code not shown here } 6 Example 1 - Which component? public void actionPerformed(ActionEvent e) { int hours, minutes; if ( e.getSource() == hoursB ) { } else if ( e.getSource() == minutesB ) { } else if ( e.getSource() == clearB ) { } } } 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 hours =Integer.parseInt(hoursT.getText()); minutes = Integer.parseInt(minutesT.getText()); messageT.setText( (hours * 60 + minutes) + " MINUTES"); hours = Integer.parseInt(hoursT.getText()); minutes = Integer.parseInt(minutesT.getText()); messageT.setText(to2DecPlaces( hours + minutes/60.0) + " HOURS"); messageT.setText("");
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 4

L24 - Computer Science 101 SS C Lecture 24 Contents! 2!...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online