Building Applications with Buttons

JButton button = new JButton("Add Interest");

JLabel label = new JLabel("balance: " + account.getBalance());

JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
frame.add(panel);

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

Click here to see the example.

How do you place the "balance: . . ." message to the left of the "Add Interest" button?

   Answer: First add label to the panel, then add button


Why was it not necessary to declare the button variable as final?

   Answer: The actionPerformed method does not access that variable.