import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /** * This class is the GUI that will be used for lab 6 * It will provide a graphical user interface for the * Bank class that was developed in labs 2 and 3. * * @author Bob Broeg * @version CS 162: Fall, 2007 */ public class GUI implements ActionListener { private JFrame frame; private Container contentPane; private static JTextArea output; private JScrollPane outputView; private JTextArea inputName, inputNumber, inputBalance, inputRate, inputDeposit, inputWithdrawal; private JLabel nameLabel, numberLabel, balanceLabel, rateLabel, depositLabel, withdrawalLabel; private JPanel namePanel, numberPanel, balancePanel, ratePanel, depositPanel, withdrawalPanel; private JButton clearButton; private JPanel column1Panel; private Box upperBox, lowerBox; private JMenuBar menuBar; private JMenu checkingMenu, savingsMenu, bankMenu, currentMenu; private JMenuItem quitItem, listAllItem, searchByNameItem, searchByNumberItem, printCurrentItem, depositItem, withdrawalItem, deleteCurrentItem, setInterestItem, updateInterestItem, newSavingsItem, newCheckingItem; //In order to compile, the class instance variable must be commented out until you add the //Bank class to the project. When the Bank class is added, uncomment this field. //private Bank bank; /** * The default constructor */ public GUI() { } /** * This is the constructor designed to connect the GUI to the bank class. * * In order to compile, this method must be commented out until you add the * Bank class to the project. When the Bank class is added, uncomment this * method. * * @param bank The bank object that was developed in lab 3. */ // public GUI ( Bank bank ) { // this.bank = bank; // } /** * This is the method that constructs the GUI */ public void makeGUI() { frame = new JFrame( "First Bank of WOU" ); frame.setBounds( 50, 50, 650, 550 ); contentPane = frame.getContentPane(); contentPane.setLayout( new GridLayout( 1, 2 ) ); //Account Name inputName = new JTextArea( 1, 15 ); nameLabel = new JLabel( " Account Name:", SwingConstants.RIGHT ); namePanel = new JPanel(); namePanel.add( nameLabel ); namePanel.add( inputName ); namePanel.add( Box.createHorizontalGlue() ); //Account Number inputNumber = new JTextArea( 1, 15 ); numberLabel = new JLabel( " Account Number:", SwingConstants.RIGHT ); numberPanel = new JPanel(); numberPanel.add( numberLabel ); numberPanel.add( inputNumber ); numberPanel.add( Box.createHorizontalGlue() ); //Account Balance inputBalance = new JTextArea( 1, 15 ); balanceLabel = new JLabel( " Account Balance:", SwingConstants.RIGHT ); balancePanel = new JPanel(); balancePanel.add( balanceLabel ); balancePanel.add( inputBalance ); balancePanel.add( Box.createHorizontalGlue() ); //Savings Account Interest Rate inputRate = new JTextArea( 1, 15 ); rateLabel = new JLabel( " Interest Rate:", SwingConstants.RIGHT ); ratePanel = new JPanel(); ratePanel.add( rateLabel ); ratePanel.add( inputRate ); ratePanel.add( Box.createHorizontalGlue() ); //Account Deposit inputDeposit = new JTextArea( 1, 15 ); depositLabel = new JLabel( " Deposit Amount:", SwingConstants.RIGHT ); depositPanel = new JPanel(); depositPanel.add( depositLabel ); depositPanel.add( inputDeposit ); depositPanel.add( Box.createHorizontalGlue() ); //Account Withdrawal inputWithdrawal = new JTextArea( 1, 15 ); withdrawalLabel = new JLabel( " Withdraw Amount:", SwingConstants.RIGHT ); withdrawalPanel = new JPanel(); withdrawalPanel.add( withdrawalLabel ); withdrawalPanel.add( inputWithdrawal ); withdrawalPanel.add( Box.createHorizontalGlue() ); //Clear the output area clearButton = new JButton( " Clear Output " ); clearButton.setBorder( BorderFactory.createRaisedBevelBorder() ); clearButton.addActionListener( this ); //Make the right side of the GUI: the input area upperBox = new Box( BoxLayout.Y_AXIS ); lowerBox = new Box( BoxLayout.Y_AXIS ); upperBox.add( namePanel ); upperBox.add( numberPanel ); upperBox.add( balancePanel ); upperBox.add( ratePanel ); upperBox.add( depositPanel ); upperBox.add( withdrawalPanel ); upperBox.add( Box.createVerticalGlue() ); lowerBox.add( Box.createVerticalStrut( 100 ) ); lowerBox.add( clearButton ); lowerBox.add( Box.createVerticalStrut( 50 ) ); lowerBox.add( new JLabel( new ImageIcon( "wolf.gif" ) ) ); column1Panel = new JPanel(); column1Panel.add( upperBox ); column1Panel.add( lowerBox ); column1Panel.setBackground( new Color( 150, 0, 0 ) ); //Make the left side of the GUI: the output area output = new JTextArea(); output.setEditable( false ); outputView = new JScrollPane( output ); //Add the two sides to the the content frame contentPane.add( outputView ); contentPane.add( column1Panel ); //Create the menu bar menuBar = new JMenuBar(); frame.setJMenuBar( menuBar ); //Create the menus bankMenu = new JMenu( " Bank " ); currentMenu = new JMenu( " Current " ); checkingMenu = new JMenu( " Checking " ); savingsMenu = new JMenu( " Savings " ); //Create the menu items for the Bank menu listAllItem = new JMenuItem( " List All " ); listAllItem.addActionListener( this ); searchByNameItem = new JMenuItem( " Search By Name " ); searchByNameItem.addActionListener( this ); searchByNumberItem = new JMenuItem( " Search By Number " ); searchByNumberItem.addActionListener( this ); quitItem = new JMenuItem( " Quit " ); quitItem.addActionListener( this ); bankMenu.add( listAllItem ); bankMenu.add( searchByNameItem ); bankMenu.add( searchByNumberItem ); bankMenu.addSeparator(); bankMenu.add( quitItem ); //Create the menu items for the Current menu printCurrentItem = new JMenuItem( " Print Current Account " ); printCurrentItem.addActionListener( this ); depositItem = new JMenuItem( " Make Deposit " ); depositItem.addActionListener( this ); withdrawalItem = new JMenuItem( " Make Withdrawal " ); withdrawalItem.addActionListener( this ); deleteCurrentItem = new JMenuItem( " Delete Current Account " ); deleteCurrentItem.addActionListener( this ); currentMenu.add( printCurrentItem ); currentMenu.add( depositItem ); currentMenu.add( withdrawalItem ); currentMenu.addSeparator(); currentMenu.add( deleteCurrentItem ); //Create the menu items for the Checking menu newCheckingItem = new JMenuItem( " New Checking Account " ); newCheckingItem.addActionListener( this ); checkingMenu.add( newCheckingItem ); //Create the menu items for the Savings menu newSavingsItem = new JMenuItem( " New Savings Account " ); newSavingsItem.addActionListener( this ); setInterestItem = new JMenuItem( " Set Interest " ); setInterestItem.addActionListener( this ); updateInterestItem = new JMenuItem( " Add Interest " ); updateInterestItem.addActionListener( this ); savingsMenu.add( newSavingsItem ); savingsMenu.add( setInterestItem ); savingsMenu.add( updateInterestItem ); //Add the menus to the menu bar menuBar.add( bankMenu ); menuBar.add( currentMenu ); menuBar.add( checkingMenu ); menuBar.add( savingsMenu ); //Make everything visible frame.setVisible( true ); } /** * This method allows other classes to print to the GUI's output area and it * imitates System.out.print( "String" ). * * This is a static method, so it is called using the format * GUI.print( "The String" ); * * @param message The String to be appended to the output. */ public static void print( String message ) { output.append( message ); } /** * This method allows other classes to print to the GUI's output area and it * imitates System.out.println( "String" ). That is, it prints the String and * then appends a new line. * * This is a static method, so it is called using the format * GUI.println( "The String" ); * * @param message The String to be appended to the output. */ public static void println( String message ) { output.append( message + "\n" ); } /** * This method allows other classes to print to the GUI's output area and it * imitates System.out.println(). That is, it prints a blank line to the * output area * * This is a static method, so it is called using the format * GUI.println(); */ public static void println() { output.append( "\n" ); } /** * An accessor method for the Account Name input field of the GUI. * * @return The String that is in the Name field. */ public String getName() { return inputName.getText(); } /** * An accessor method for the Account Number field of the GUI * * @return An int that is the number in the Account field or a 0 * if the field cannot be parsed as an integer. */ public int getNumber() { int number = 0; String temp = inputNumber.getText(); try { number = Integer.parseInt( temp ); } catch (NumberFormatException e ) { output.append( "ERROR: Unable to determine number, using value 0.\n " ); } return number; } /** * An accessor method for the Account Balance field of the GUI * * @return A double that is the value in the Balance field or a 0.0 * if the field cannot be parsed as a double. */ public double getBalance() { double balance = 0.0; String temp = inputBalance.getText(); try { balance = Double.parseDouble( temp ); } catch (NumberFormatException e ) { output.append( "ERROR: Unable to determine number, using value 0.0.\n " ); } return balance; } /** * An accessor method for the Interest Rate field of the GUI * * @return A double that is the value in the Interest Rate field * or a 0.0 if the field cannot be parsed as a double. */ public double getRate() { double rate = 0.0; String temp = inputRate.getText(); try { rate = Double.parseDouble( temp ); } catch( NumberFormatException e ) { output.append( "ERROR: Unable to determine number, using value 0.\n " ); } return rate; } /** * An accessor method for the Deposit field of the GUI * * @returns A double that is the value in the Deposit field or a 0.0 * if the field cannot be parsed as a double. */ public double getDeposit() { double deposit = 0.0; String temp = inputDeposit.getText(); try { deposit = Double.parseDouble( temp ); } catch( NumberFormatException e ) { output.append( "ERROR: Unable to determine number, using value 0.\n " ); } return deposit; } /** * An accessor method for the Withdrawal field of the GUI * * @returns A double that is the value of the Withdrawal field or a 0.0 * if the field cannot be parsed as a double. */ public double getWithdrawal() { double withdrawal = 0.0; String temp = inputWithdrawal.getText(); try { withdrawal = Double.parseDouble( temp ); } catch( NumberFormatException e ) { output.append( "ERROR: Unable to determine number, using value 0.\n " ); } return withdrawal; } /** * A mutator method for the Account Name field of the GUI * * @param name The String to be placed in the Name field */ public void setNameField( String name ) { inputName.setText( name ); } /** * A mutator method for the Account Number field of the GUI * * @param number The String to be placed in the Number field */ public void setNumberField( String number ) { inputNumber.setText( number ); } /** * A mutator method for the Account Balance field of the GUI * * @param balance The String to be placed in the Balance field */ public void setBalanceField( String balance ) { inputBalance.setText( balance ); } public void setRateField( String rate ) { inputRate.setText( rate ); } /** * A mutator method for the Account Deposit field of the GUI * * @param deposit The String to be placed in the Deposit field */ public void setDepositField( String deposit ) { inputDeposit.setText( deposit ); } /** * A mutator method for the Account Withdrawal field of the GUI * * @param withdrawal The String to be placed in the Withdrawal field */ public void setWithdrawalField( String withdrawal ) { inputWithdrawal.setText( withdrawal ); } /** * This method clears all the input boxes in the GUI. */ public void clearInputFields() { inputName.setText( "" ); inputNumber.setText( "" ); inputBalance.setText( "" ); inputRate.setText( "" ); inputDeposit.setText( "" ); inputWithdrawal.setText( "" ); } /** * This method is required because the class implements the * ActionListener interface * * @param e The Action Event object that caused this method * to be called. */ public void actionPerformed( ActionEvent e ) { } }