Presentation is loading. Please wait.

Presentation is loading. Please wait.

For (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } The for Statement.

Similar presentations


Presentation on theme: "For (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } The for Statement."— Presentation transcript:

1 for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } The for Statement

2 The body of the loop contains another loop ◦ Each time through the outer loop, the inner loop goes through its full set of iterations

3 X=6; while (x >=4) { y = 3; while (y <=6) { y++; System.out.println(y + “ “ + x); } x--; }

4  Write a method to: ◦ Receive an int number in a method called count ◦ Using a for loop, print asterisks across the console, the number of asterisks to be the passed number

5

6 Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button (Buttons other objects generate events: Source ) Object e.g. button OS Interrupt Listener or middleman (interface) Generates an Event

7 Class InterfaceEvent Handler Methods (contains instructions to be executed when event occurs) Generated by event source ActionEventactionPerformed Button pressed JTextField-text changed JComboBox-alternative chosen ChangeEventstateChangedJComponent-state changed FocusEventfocusGained focusLost Component-gained or lost focus WindowEventWindow opened, closed, is closing, is iconified, or restored MouseEventmouseClicked mouseEntered mouseExited mousePressed mouseReleased mouseDragged mouseMoved Component Mouse Mouse pressed or released, mouse moved into or out of component Component moved or dragged

8 GUI Components and Screen Design GUI Demo Message Hello World CloseClearDisplay Panel Instance Textfield Instance Label Instance Button Instance Frame Instance

9 Building Applications with Buttons

10 JButton button = new JButton("Add Interest"); JLabel label = new JLabel("balance: " + account.getBalance()); JPanel panel = new JPanel() or extends JPanel; panel.add(button); panel.add(label); frame.add(panel); (or the class that extends a panel) frame.add (aClass);

11 class AddInterestListener implements ActionListener { public void actionPerformed(ActionEvent event) { double interest = account.getBalance() * INTEREST_RATE / 100; bank.deposit(interest); label.setText("balance=" + account.getBalance()); } } Building Applications with Buttons

12 1.Add the class within the original class where buttons are activated 2.Create a global listener reference: ActionListener listener; 3. In the constructor listener = new AddInterestListener (); 4.button1.addActionListener(listener ); this.add(button1); STEPS IN THE PROCESS

13 package investment; import javax.swing.Jframe; public class InvestmentViewer { public static void main (String [] args) { JFrame frame = new JFrame ("Investment Viewer"); frame.setSize(30, 200); Investment inv = new Investment (); frame.add (inv); frame.setDefaultCloseOperation(JFrame.EXIT_ ON_CLOSE); frame.setVisible(true); } InvestmentViewer.java (note changes from text)

14 package investment; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.Jpanel; public class Investment extends JPanel { private JButton button; private JLabel label, ansLabel; private BankAccount bank; private final int INTEREST_RATE = 7; ActionListener listener = new AddInterestListener (); public Investment () { bank = new BankAccount (); getPanelReady (); } public void getPanelReady () { button = new JButton ("Sample Button"); button.addActionListener(listener); label = new JLabel ("GJY"); ansLabel = new JLabel (); add(button); add(label); add(ansLabel); } class AddInterestListener implements ActionListener { public void actionPerformed (ActionEvent arg0) { double interest; interest = bank.getBalance () + (INTEREST_RATE / 100.0); ansLabel.setText("Balance: " + interest); }

15 LAB: (Homework through the week was: Chapter 9 : Exercise P14) Lab: P9. 15

16 public interface MouseListener { void mousePressed(MouseEvent event); // Called when a mouse button has been pressed on a component void mouseReleased(MouseEvent event); // Called when a mouse button has been released on a component void mouseClicked(MouseEvent event); // Called when the mouse has been clicked on a component void mouseEntered(MouseEvent event); // Called when the mouse enters a component Mouse Events

17 void mouseExited(MouseEvent event); // Called when the mouse exits a component } mousePressed, mouseReleased: called when a mouse button is pressed or released mouseClicked: if button is pressed and released in quick succession, and mouse hasn't moved mouseEntered, mouseExited: mouse has entered or exited the component's area Mouse Events

18 Add a mouse listener to a component by calling the addMouseListener method: public class MyMouseListener implements MouseListener { // Implements five methods } MouseListener listener = new MyMouseListener(); component.addMouseListener(listener); Sample program: enhance RectangleComponent – when user clicks on rectangle component, move the rectangle Mouse Events


Download ppt "For (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } The for Statement."

Similar presentations


Ads by Google