button/textfield relationships


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: button/textfield relationships

  1. #1
    Shorty_tec Guest

    button/textfield relationships


    Ok, I'm kinda new at this, but here goes.

    I'm writing a program that displays a bank account balance.
    There are two textfields. One for the current balance and one for the user
    to enter an amount which we'll call cashflow. There are three buttons.
    One to deposit, one to withdraw, and one to reset the entire thing. My problem
    is that I cannot get the amount in the balance field to change. I put an
    amount into the cashflow field and hit any button and nothing happens. I
    believe it lies in init. I'm unsure as to where to go with this, and unfortunately,
    my teacher is of little help. I'm including my source code, if anyone can
    help me that would be great! Bear in mind, there are a few other things
    missing yet, but I'll hash those out later.
    Thank you everyone and anyone who can help me.



    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;

    public class BankAccount extends Applet implements ActionListener {

    private int amtEntered, currBalAmt;

    private Button deposit, withdrawal, playAgain;

    private TextField amtField, currBalField;


    public void init() {

    this.setFont(new java.awt.Font ("Dialog", 0, 36));

    playAgain = new Button (" Play again ");
    add(playAgain);
    playAgain.addActionListener(this);

    deposit = new Button (" Deposit ");
    add(deposit);
    deposit.addActionListener(this);

    withdrawal = new Button ("Withdrawal");
    add(withdrawal);
    withdrawal.addActionListener(this);

    Label line = new Label
    ("___________________________________________________________");
    add(line);

    amtField = new TextField(10);
    add(amtField);
    amtField.addActionListener(this);


    currBalField = new TextField(11);
    add(currBalField);
    currBalField.addActionListener(this);


    }



    public void paint (Graphics g) {
    g.drawString("Amount $" , 30, 150);
    g.drawString("Current Balance $" , 700, 150);

    if (amtEntered < 0) {
    g.drawString ("ERROR, please enter positive amount", 50, 100);
    }

    if (currBalAmt < 0) {
    g.setColor (Color.red);
    }

    }


    public void actionPerformed (ActionEvent event) {

    currBalAmt = Integer.parseInt(currBalField.getText());
    amtEntered = Integer.parseInt(amtField.getText());


    if (event.getSource() == deposit) {
    currBalAmt = currBalAmt + amtEntered;
    currBalField.setText("" + currBalAmt);

    }

    if (event.getSource() == withdrawal) {
    currBalAmt = currBalAmt - amtEntered;
    }

    if (event.getSource() == playAgain) {
    currBalAmt = 25;
    }



    repaint();
    }
    }


  2. #2
    Paul Clapham Guest

    Re: button/textfield relationships

    Your code that starts with

    if (event.getSource() == deposit) {

    is correct, because it does setText on some component to change what's
    displayed. But the other cases below that code don't setText on anything,
    therefore the display doesn't change... which was your problem, right?

    PC2

    "Shorty_tec" <Shorty_tec@hotmail.com> wrote in message
    news:3c4fcc7e$1@10.1.10.29...
    >
    > Ok, I'm kinda new at this, but here goes.
    >
    > I'm writing a program that displays a bank account balance.
    > There are two textfields. One for the current balance and one for the user
    > to enter an amount which we'll call cashflow. There are three buttons.
    > One to deposit, one to withdraw, and one to reset the entire thing. My

    problem
    > is that I cannot get the amount in the balance field to change. I put an
    > amount into the cashflow field and hit any button and nothing happens. I
    > believe it lies in init. I'm unsure as to where to go with this, and

    unfortunately,
    > my teacher is of little help. I'm including my source code, if anyone can
    > help me that would be great! Bear in mind, there are a few other things
    > missing yet, but I'll hash those out later.
    > Thank you everyone and anyone who can help me.
    >
    >
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import java.applet.*;
    > import javax.swing.*;
    >
    > public class BankAccount extends Applet implements ActionListener {
    >
    > private int amtEntered, currBalAmt;
    >
    > private Button deposit, withdrawal, playAgain;
    >
    > private TextField amtField, currBalField;
    >
    >
    > public void init() {
    >
    > this.setFont(new java.awt.Font ("Dialog", 0, 36));
    >
    > playAgain = new Button (" Play again ");
    > add(playAgain);
    > playAgain.addActionListener(this);
    >
    > deposit = new Button (" Deposit ");
    > add(deposit);
    > deposit.addActionListener(this);
    >
    > withdrawal = new Button ("Withdrawal");
    > add(withdrawal);
    > withdrawal.addActionListener(this);
    >
    > Label line = new Label
    > ("___________________________________________________________");
    > add(line);
    >
    > amtField = new TextField(10);
    > add(amtField);
    > amtField.addActionListener(this);
    >
    >
    > currBalField = new TextField(11);
    > add(currBalField);
    > currBalField.addActionListener(this);
    >
    >
    > }
    >
    >
    >
    > public void paint (Graphics g) {
    > g.drawString("Amount $" , 30, 150);
    > g.drawString("Current Balance $" , 700, 150);
    >
    > if (amtEntered < 0) {
    > g.drawString ("ERROR, please enter positive amount", 50, 100);
    > }
    >
    > if (currBalAmt < 0) {
    > g.setColor (Color.red);
    > }
    >
    > }
    >
    >
    > public void actionPerformed (ActionEvent event) {
    >
    > currBalAmt = Integer.parseInt(currBalField.getText());
    > amtEntered = Integer.parseInt(amtField.getText());
    >
    >
    > if (event.getSource() == deposit) {
    > currBalAmt = currBalAmt + amtEntered;
    > currBalField.setText("" + currBalAmt);
    >
    > }
    >
    > if (event.getSource() == withdrawal) {
    > currBalAmt = currBalAmt - amtEntered;
    > }
    >
    > if (event.getSource() == playAgain) {
    > currBalAmt = 25;
    > }
    >
    >
    >
    > repaint();
    > }
    > }
    >




  3. #3
    Shorty_tec Guest

    Re: button/textfield relationships


    Actually, the display doesn't change on anything. even when I use the deposit
    button. Nothing works.



    Clapham" <pclapham@core-mark.com> wrote:
    >Your code that starts with
    >
    >if (event.getSource() == deposit) {
    >
    >is correct, because it does setText on some component to change what's
    >displayed. But the other cases below that code don't setText on anything,
    >therefore the display doesn't change... which was your problem, right?
    >
    >PC2
    >
    >"Shorty_tec" <Shorty_tec@hotmail.com> wrote in message
    >news:3c4fcc7e$1@10.1.10.29...
    >>
    >> Ok, I'm kinda new at this, but here goes.
    >>
    >> I'm writing a program that displays a bank account balance.
    >> There are two textfields. One for the current balance and one for the

    user
    >> to enter an amount which we'll call cashflow. There are three buttons.
    >> One to deposit, one to withdraw, and one to reset the entire thing. My

    >problem
    >> is that I cannot get the amount in the balance field to change. I put

    an
    >> amount into the cashflow field and hit any button and nothing happens.

    I
    >> believe it lies in init. I'm unsure as to where to go with this, and

    >unfortunately,
    >> my teacher is of little help. I'm including my source code, if anyone

    can
    >> help me that would be great! Bear in mind, there are a few other things
    >> missing yet, but I'll hash those out later.
    >> Thank you everyone and anyone who can help me.
    >>
    >>
    >>
    >> import java.awt.*;
    >> import java.awt.event.*;
    >> import java.applet.*;
    >> import javax.swing.*;
    >>
    >> public class BankAccount extends Applet implements ActionListener {
    >>
    >> private int amtEntered, currBalAmt;
    >>
    >> private Button deposit, withdrawal, playAgain;
    >>
    >> private TextField amtField, currBalField;
    >>
    >>
    >> public void init() {
    >>
    >> this.setFont(new java.awt.Font ("Dialog", 0, 36));
    >>
    >> playAgain = new Button (" Play again ");
    >> add(playAgain);
    >> playAgain.addActionListener(this);
    >>
    >> deposit = new Button (" Deposit ");
    >> add(deposit);
    >> deposit.addActionListener(this);
    >>
    >> withdrawal = new Button ("Withdrawal");
    >> add(withdrawal);
    >> withdrawal.addActionListener(this);
    >>
    >> Label line = new Label
    >> ("___________________________________________________________");
    >> add(line);
    >>
    >> amtField = new TextField(10);
    >> add(amtField);
    >> amtField.addActionListener(this);
    >>
    >>
    >> currBalField = new TextField(11);
    >> add(currBalField);
    >> currBalField.addActionListener(this);
    >>
    >>
    >> }
    >>
    >>
    >>
    >> public void paint (Graphics g) {
    >> g.drawString("Amount $" , 30, 150);
    >> g.drawString("Current Balance $" , 700, 150);
    >>
    >> if (amtEntered < 0) {
    >> g.drawString ("ERROR, please enter positive amount", 50, 100);
    >> }
    >>
    >> if (currBalAmt < 0) {
    >> g.setColor (Color.red);
    >> }
    >>
    >> }
    >>
    >>
    >> public void actionPerformed (ActionEvent event) {
    >>
    >> currBalAmt = Integer.parseInt(currBalField.getText());
    >> amtEntered = Integer.parseInt(amtField.getText());
    >>
    >>
    >> if (event.getSource() == deposit) {
    >> currBalAmt = currBalAmt + amtEntered;
    >> currBalField.setText("" + currBalAmt);
    >>
    >> }
    >>
    >> if (event.getSource() == withdrawal) {
    >> currBalAmt = currBalAmt - amtEntered;
    >> }
    >>
    >> if (event.getSource() == playAgain) {
    >> currBalAmt = 25;
    >> }
    >>
    >>
    >>
    >> repaint();
    >> }
    >> }
    >>

    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center