Search
 
SCRIPT & CODE EXAMPLE
 

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); 
        } 
   }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to repeat string in java 
Java :: English print format number in java 
Java :: coin flip random java 
Java :: key caracter from code java 
Java :: java komplettes array ausgeben 
Java :: Transversal operation in java array 
Java :: power of a number in java 
Java :: what is java.io example 
Java :: Copying value from one input field to another input field using checkbox 
Java :: Exercise. Create a simple Java program using array named SumOfArray.java that will accept an input of whole numbers or floating point numbers and will return the Sum result. The program must accept at least 5 numbers. 
Java :: What is Java? 
Java :: java 8 lambda delete from list 
Java :: filterreader converts a string to uppercase java 
Java :: how to write a perfect shuffle method in java 
Java :: leap year java method 
Java :: java nom de la methode actuel 
Java :: jtable fill panel 
Java :: linearview 2 items next to each other 
Java :: OCA Exam Questions 
Java :: set length java 
Java :: jlabel text grösse 
Java :: matrix program 
Java :: java pattern matching 16 
Java :: JAVA CHAR TO keyevents 
Java :: javafx line chaart duplicate series added 
Java :: ring write a character to the stream 
Java :: android how to change focus on confirm button on keyboard 
Java :: how to read space separated characters in java 
Java :: how to get visibility of element android 
Java :: firebase timestamp to textview 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =