1

So here is my question:

I would like to create a new panel that asks the user to enter Bank Account Information (Account Name, and Account Number) and when the user clicks on the Login button, it changes panels to the Withdraw/Deposit Panel and also displays your Account Name at the top. I have the Withdraw/Deposit Panel all completed, but am stumped on how to create the Information Panel and to make it appear BEFORE the Withdraw/Deposit Panel, etc.

Here is my code:

public class MyFrame extends JFrame {

    private JPanel panel;
    private JLabel wordsLabel;
    private JLabel balanceLabel;
    private JLabel choiceLabel;
    private JTextField transactionAmount;
    private JRadioButton depositButton;
    private JRadioButton withdrawButton;
    private double balance;

    public MyFrame() {
        final int FIELD_WIDTH = 5;
        balance = 500;
        panel = new JPanel();
        wordsLabel = new JLabel();
        balanceLabel = new JLabel();
        choiceLabel = new JLabel();
        transactionAmount = new JTextField(FIELD_WIDTH);
        JPanel buttonPanel = new JPanel();
        ButtonGroup myGroup = new ButtonGroup();
        depositButton = new JRadioButton("Deposit");
        withdrawButton = new JRadioButton("Withdraw");
        transactionAmount.setText("0");
        wordsLabel.setText("Welcome to Wes Banco! Your current balance is: ");
        balanceLabel.setText(String.format("%10.2f", balance));
        choiceLabel.setText("How much would you like to deposit/withdraw? ");
        panel.setLayout(new GridLayout(4, 4, 5, 10));
        panel.add(wordsLabel);
        panel.add(balanceLabel);
        panel.add(choiceLabel);
        panel.add(transactionAmount);
        myGroup.add(depositButton);
        myGroup.add(withdrawButton);
        buttonPanel.add(depositButton);
        buttonPanel.add(withdrawButton);
        ButtonListener myListener = new ButtonListener();
        depositButton.addActionListener(myListener);
        withdrawButton.addActionListener(myListener);    
        panel.add(buttonPanel);
        this.add(panel);
    }

    class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            double amount = Double.parseDouble(transactionAmount.getText());
            if (amount == 0) {
                JOptionPane.showMessageDialog(null, 
                       "You cannot deposit or withdraw nothing!");
                JOptionPane.showMessageDialog(null, 
                       "Please enter a valid amount.");
            } else {
                if (event.getSource() == depositButton) {
                    JOptionPane.showMessageDialog(null,
                            "You have deposited: " + amount);
                    balance += amount;
                } else if (event.getSource() == withdrawButton) {
                    if (balance < amount) {
                        JOptionPane.showMessageDialog(null,
                                "You do not have sufficient funds to complete this transaction.");
                        JOptionPane.showMessageDialog(null,
                                "Please enter a valid amount.");
                    } else {
                        JOptionPane.showMessageDialog(null,
                                "You have withdrawn: " + amount);
                        balance -= amount;
                    }
                }
                balanceLabel.setText(String.valueOf(balance));
            }
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Consider using [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), as shown in this [example](http://stackoverflow.com/a/9349137/1057230) – nIcE cOw Jul 24 '13 at 08:39

1 Answers1

1

My advice would be: Don´t create the panel in your JFrame constructor. Make an InfoPanel class and a WithdrawPanel class. Then you can decide programatically which panel is displayed in your frame.

Jannis Alexakis
  • 1,279
  • 4
  • 19
  • 38