Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java gui bank account program

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class AccountPanel extends JPanel implements ActionListener {  
    private JLabel amountLabel, resultLabel;
    private JTextField amountTextField;   
    private JButton depositButton, withdrawButton;
    private bankAccount account;
    double result;

    public AccountPanel() {
        JPanel displayPanel = new JPanel(); 
        displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        amountLabel = new JLabel("Amount:"); 
        displayPanel.add(amountLabel);

        amountTextField = new JTextField(13); 
        displayPanel.add(amountTextField); 

        JPanel resultPanel = new JPanel(); 
        resultPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        resultLabel = new JLabel("Balance = "); 
        resultPanel.add(resultLabel);

        //buttons
        JPanel buttonPanel = new JPanel(); 
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        // deposit button 
        depositButton = new JButton("Deposit"); 
        buttonPanel.add(depositButton); 

        // withdraw
        withdrawButton = new JButton("Withdraw"); 
        buttonPanel.add(withdrawButton); 

        // add panels to main panel 
        this.setLayout(new BorderLayout()); 
        this.add(displayPanel, BorderLayout.WEST);
        this.add(resultPanel, BorderLayout.SOUTH); 
        this.add(buttonPanel, BorderLayout.EAST); 
    }



    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource(); 
        if (source == depositButton) {
            double dp = Double.parseDouble(amountTextField.getText());
            double dpamount = account.getBalance() + dp;
            account.deposit(dpamount);
            result = dpamount;
            resultLabel.setText("" + result);
            depositButton.addActionListener(this); 
        }
        else if (source == withdrawButton) { 
            double wd = Double.parseDouble(amountTextField.getText());
            account.withdraw(wd);
            resultLabel.setText("" + result);
            withdrawButton.addActionListener(this); 
        } 
   }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #java #gui #bank #account #program
ADD COMMENT
Topic
Name
8+7 =