Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 241 – Computer Programming II Lab

Similar presentations


Presentation on theme: "CS 241 – Computer Programming II Lab"— Presentation transcript:

1 CS 241 – Computer Programming II Lab
Kalpa Gunaratna –

2 Contact Contact by e-mail Office hours kalpa@knoesis.org 376 Joshi
Mondays & Wednesday 3:00 pm – 4:00 pm

3 ActionListener class If ActionListener class is exteneded, actionPerformed abstract method should be implemented. Whatever written inside this method will be executed when an action is performed relevant to this ActionListener. Any GUI component registered to an ActionListener will call this method when an action is performed on the GUI component (press mouse on , etc).

4 Register an action for a JButton.
Example on how to implement ActionListener on a class called NewJFrame. Register an action for a JButton. Action handling in method public class NewJFrame extends JFrame implements ActionListener { JButton button = new JButton(); button.setText("Press"); button.setSize(20, 60); button.addActionListener(this); public void actionPerformed(ActionEvent e) { System.out.println("Mouse pressed"); }

5 Setting an layout for a panel
Example : setting BorderLayout and adding button to south. jPanel.setLayout(new BorderLayout()); jPanel.add(BorderLayout.SOUTH, button);

6 In Lab Implement ActionListener and register it to a button.
When button is pressed add balls to the panel. Button should be added to the south of the panel.

7 Post Lab Add two methods called as detectCollision and processCollision. Before you move balls, collisions should be detected and perform something as a results. Deleting one ball, change colors, etc.


Download ppt "CS 241 – Computer Programming II Lab"

Similar presentations


Ads by Google