📄 bankaccountgui.java
字号:
//**********************************************************
//BankAccountGUI.java Author:ZhaoHaiyan
//
//Design the interface for the program.
//**********************************************************
import pps.PPSFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BankAccountGUI extends PPSFrame implements ActionListener
{
private SavingsAccount savings; // model the credit and
private CreditAccount credit; // savings accounts
private JButton deposit; // buttons for the user to
private JButton withdraw;
private JButton transfer;
private JButton close;
private JLabel savingsBalanceLabel; // labels for the account
private JLabel creditBalanceLabel; // balances
private JTextField savingsBalance; // text fields to display
private JTextField creditBalance; // the current balances
private JTextField depositAmount; // text fields for the user
private JTextField withdrawAmount; // to indicate values for each
private JTextField transferAmount; // of the transactions
private JRadioButton savingsOperations; // radio buttons for the user
private JRadioButton creditOperations; // to indicate which account
private ButtonGroup group = new ButtonGroup(); // they're working with
private Font display; // font to use in components
public BankAccountGUI()
{
super();
// instantiate the font to be used
display = new Font("Monospaced", Font.PLAIN, 12);
// instantiate and add GUI components
savingsBalanceLabel = new JLabel("Savings Acc. Balance: $");
savingsBalanceLabel.setFont(display);
add(savingsBalanceLabel);
savingsBalance = new JTextField(25);
add(savingsBalance);
// add label and textfield for savingsBalance
creditBalanceLabel = new JLabel("Credit Acc.Balance: $");
creditBalanceLabel.setFont(display);
add(creditBalanceLabel);
creditBalance = new JTextField(25);
add(creditBalance);
//add label and textfield for creditBalance
depositAmount = new JTextField(30);
add(depositAmount);
deposit = new JButton(" Deposit");
deposit.setFont(display);
add(deposit);
//add textfield and button for deposit
withdrawAmount = new JTextField(30);
add(withdrawAmount);
withdraw = new JButton("Withdraw");
withdraw.setFont(display);
add(withdraw);
//add textfield and button for withdraw
transferAmount = new JTextField(30);
add(transferAmount);
transfer = new JButton("Transfer");
transfer.setFont(display);
add(transfer);
//add textfield and button for transfer
savingsOperations = new JRadioButton("Savings Account");
creditOperations = new JRadioButton("Credit Account");
group.add(savingsOperations);
group.add(creditOperations);
creditOperations.setSelected(true);
add(savingsOperations);
add(creditOperations);
//add radio buttons
close = new JButton("Close Savings Account");
close.setFont(display);
add(close);
//add button for close
// add action listener for the buttons
deposit.addActionListener(this);
withdraw.addActionListener(this);
transfer.addActionListener(this);
close.addActionListener(this);
// set up bank account objects
savings = new SavingsAccount(1000);
savingsBalance.setText(""+savings.getBalanceString());
credit = new CreditAccount(0);
creditBalance.setText(""+credit.getBalanceString());
// set the initial values in the text fields.
savingsBalance.setEditable(false);
creditBalance.setEditable(false);
repaint();
}
public void actionPerformed(ActionEvent e)
{
boolean success = false;
Integer intWrapper;
if (e.getSource() == deposit)
{
intWrapper = new Integer(depositAmount.getText());
if (savingsOperations.isSelected())
{
success = savings.deposit(intWrapper.intValue());
repaint();
depositAmount.setText(""); //deposit money into savingsAccount
}
else if (creditOperations.isSelected())
{
success = credit.deposit(intWrapper.intValue());
repaint();
depositAmount.setText(""); //deposit money into creditAccount
}
if (!success)
{
JOptionPane.showMessageDialog( null,
"Deposit failed. See console window for details",
"Deposit failed", JOptionPane.ERROR_MESSAGE);
}
}
if(e.getSource()==withdraw)
{
intWrapper=new Integer(withdrawAmount.getText());
if(creditOperations.isSelected()) //withdraw money from creditAccount
{
success=credit.withdraw(intWrapper.intValue());
repaint();
withdrawAmount.setText("");
if (!success)
{
JOptionPane.showMessageDialog( null,
"Deposit failed. See console window for details",
"Deposit failed", JOptionPane.ERROR_MESSAGE);
}
}
else if(savingsOperations.isSelected()) //withdraw money from savingsAccount
{
success=savings.withdraw(intWrapper.intValue());
repaint();
withdrawAmount.setText("");
if (!success)
{
JOptionPane.showMessageDialog( null,
"Deposit failed. See console window for details",
"Deposit failed", JOptionPane.ERROR_MESSAGE);
}
}
}
if(e.getSource()==transfer) //transfer money from savingsAccount to creditAccount
{
intWrapper=new Integer(transferAmount.getText());
if(savingsOperations.isSelected())
{
success=savings.withdraw(intWrapper.intValue());
success=credit.deposit(intWrapper.intValue());
repaint();
transferAmount.setText("");
}
if(creditOperations.isSelected())
{
JOptionPane.showMessageDialog(null,
"Transfer failed. See console window for details",
"Transfer failed", JOptionPane.ERROR_MESSAGE);
}
if (!success)
{
JOptionPane.showMessageDialog(null,
"Transfer failed. See console window for details",
"Transfer failed", JOptionPane.ERROR_MESSAGE);
}
}
/************************************************************************
* override the paint method to force the GUI components to re-draw *
************************************************************************/
public void paint(Graphics g)
{
super.paintComponents(g);
savingsBalance.setText(savings.getBalanceString());
creditBalance.setText(credit.getBalanceString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -