How do I write code to get text from JTextField
and convert to a double?
I have created a bank account class and a bank account GUI with amountField
to show the amount i wish to withdraw and deposit.
Using public void actionPerformed(ActionEvent e)
method how can I write code to get text from amountField
and convert to a double?
I want to input account details and be able to withdraw and deposit whilst storing values within a string
- write event handler for deposit button
- write event handler for withdraw button
public class BankAccountGUI extends JFrame implements ActionListener
{
private Label amountLabel = new Label("Amount");
private JTextField amountField = new JTextField(5);
private JButton depositButton = new JButton("DEPOSIT");
private JButton withdrawButton = new JButton("WITHDRAW");
private Label balanceLabel = new Label("Starting Balance = 0" );
private JPanel topPanel = new JPanel();
private JPanel bottomPanel = new JPanel();
private JPanel middlePanel = new JPanel();
BankAccount myAccount = new BankAccount("James","12345");
// declare a new BankAccount object (myAccount) with account number and name of your choice here
public BankAccountGUI()
{
setTitle("BankAccount GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(340, 145);
setLocation(300,300);
depositButton.addActionListener(this);
withdrawButton.addActionListener(this);
topPanel.add(amountLabel);
topPanel.add(amountField);
bottomPanel.add(balanceLabel);
middlePanel.add(depositButton);
middlePanel.add(withdrawButton);
add (BorderLayout.NORTH, topPanel);
add(BorderLayout.SOUTH, bottomPanel);
add(BorderLayout.CENTER, middlePanel);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//What goes here?
}
}