Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Exclamation Buttons and TextFields

    Hello everyone,

    I'm pretty new to Java, and I was hoping someone could help me with my Java Application. I'm trying to program an ATM, and at the start menu, I have it so that the user can type in what they want to do. Example: if they type "O" and click the "OK" button, then they will Open a new account, and bla bla bla.

    I kind of figured out how to get my button to do something, but I can't get it to do exactly what I want it to. Here's my code so far.

    Java Code:
    final JFrame menuFrame = new JFrame();
    		menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		menuFrame.setTitle("ATM");
    		//menuFrame.setLocationRelativeTo(null); //Suppose to center it
    		menuFrame.setLocation(600, 300);
    		menuFrame.setVisible(true);
    		menuFrame.setSize(325, 175);
    		
    		JLabel menuLabel = new JLabel("Please choose one of the following:");
    		menuLabel.setBounds(20, 15, 250, 18);
    		JLabel menu1 = new JLabel("O - Open New Account");
    		menu1.setBounds(45, 35, 250, 18);
    		JLabel menu2 = new JLabel("T - Perform a Transaction on an Account");
    		menu2.setBounds(45, 55, 250, 18);
    		JLabel menu3 = new JLabel("Q - Quit Program");
    		menu3.setBounds(45, 75, 250, 18);
    		menuLabel.setVerticalAlignment(SwingConstants.TOP);
    		
    		final JTextField txtBox = new JTextField();
    		txtBox.setBounds(100, 95, 30, 30);
    		
    		JButton okBtn = new JButton("OK");
    		okBtn.setBounds(150, 95, 55, 30);
    		okBtn.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				String menuChoosen = txtBox.getText();
    			}
    		});
    		
    		JPanel menuPanel = new JPanel();
    		menuPanel.setLayout(null);
    		menuPanel.add(menuLabel);
    		menuPanel.add(menu1);
    		menuPanel.add(menu2);
    		menuPanel.add(menu3);
    		menuPanel.add(okBtn, BorderLayout.CENTER);
    		menuPanel.add(txtBox);
    		menuPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
    		menuFrame.setContentPane(menuPanel);
    Okay, so that creates a nice little box with some labels in it, a small textbox, and a button that says "OK". Right now, when the OK button is clicked, all it does is: "String menuChoosen = txtBox.getText();" however...I can't seem to use the variable menuChoosen outside of this actionPerformed(); I don't really understand the ActionListener thing at all. If someone could help me out with this, AND direct me to a GREAT tutorial on this, that would be wonderful. I appreciate it, thank you. (P.S. Also, how can I close the current JFrame when the button is clicked too?)

  2. #2
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Could you post the complete class?

  3. #3
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    That was everything in it. Here's everything I have: Ignore the commented out stuff, saving that for later.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ATMMachine
    {
    	public static void main(String[] args)
    	{	
    		//Variables
    		String menu = "";
    		int atmMenu = 1; //1 is on, 0 is off
    		NewAccount[] account = new NewAccount[50];
    		int accountsOpen = -1;
    		
    		final JFrame menuFrame = new JFrame();
    		menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		menuFrame.setTitle("ATM");
    		//menuFrame.setLocationRelativeTo(null); //Suppose to center it
    		menuFrame.setLocation(600, 300);
    		menuFrame.setVisible(true);
    		menuFrame.setSize(325, 175);
    		
    		JLabel menuLabel = new JLabel("Please choose one of the following:");
    		menuLabel.setBounds(20, 15, 250, 18);
    		JLabel menu1 = new JLabel("O - Open New Account");
    		menu1.setBounds(45, 35, 250, 18);
    		JLabel menu2 = new JLabel("T - Perform a Transaction on an Account");
    		menu2.setBounds(45, 55, 250, 18);
    		JLabel menu3 = new JLabel("Q - Quit Program");
    		menu3.setBounds(45, 75, 250, 18);
    		menuLabel.setVerticalAlignment(SwingConstants.TOP);
    		
    		final JTextField txtBox = new JTextField();
    		txtBox.setBounds(100, 95, 30, 30);
    		
    		JButton okBtn = new JButton("OK");
    		okBtn.setBounds(150, 95, 55, 30);
    		okBtn.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				String menuChoosen = txtBox.getText();
    			}
    		});
    		
    		JPanel menuPanel = new JPanel();
    		menuPanel.setLayout(null);
    		menuPanel.add(menuLabel);
    		menuPanel.add(menu1);
    		menuPanel.add(menu2);
    		menuPanel.add(menu3);
    		menuPanel.add(okBtn, BorderLayout.CENTER);
    		menuPanel.add(txtBox);
    		menuPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
    		menuFrame.setContentPane(menuPanel);		
    		
    		/*
    		while(atmMenu == 0)
    		{
    			if (menu.equals("O") || menu.equals("o"))
    			{
    				//Open a new account
    				atmMenu = 0;
    				accountsOpen++;
    				account[0].name = "Andy";
    			}
    			else if (menu.equals("T") || menu.equals("t"))
    			{
    				//Make a transaction
    				atmMenu = 0;
    			}
    			else if (menu.equals("Q") || menu.equals("q"))
    			{
    				//Quit
    				atmMenu = 0;
    				System.out.print("Thank you for using this ATM!");
    			}
    			else
    			{
    				//Go back to the main menu
    			}
    		}
    		*/
    	}
    	
    	class GetMenuOption
    	{
    		
    	}
    	
    	class NewAccount
    	{
    		private String name = "";
    		private String address = "";
    		private String telephone = "";
    		private int initialBalance = 0;
    		
    		public NewAccount()
    		{
    			
    		}
    		public NewAccount(String name, String address, String telephone, int initialBalance)
    		{
    			this.name = name;
    			this.address = address;
    			this.telephone = telephone;
    			this.initialBalance = initialBalance;
    		}
    		public String getName()
    		{
    			return name;
    		}
    		public void setName(String name)
    		{
    			this.name = name;
    		}
    		public String getAddress()
    		{
    			return address;
    		}
    		public void setAddress(String address)
    		{
    			this.address = address;
    		}
    		public String getTelephone()
    		{
    			return telephone;
    		}
    		public void setTelephone(String telephone)
    		{
    			this.telephone = telephone;
    		}
    		public int getInitialBalance()
    		{
    			return initialBalance;
    		}
    		public void setInitialBalance(int initialBalance)
    		{
    			this.initialBalance = initialBalance;
    		}
    	}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First of all, you seem to have created a GUI class that is nothing more than a huge public static void main method, and this won't do. I strongly suggest that you make a true OOP GUI class with constructor, and all, otherwise this program will not get very far at all.

    Next, if this were my program, I'd have several buttons that would allow me to decide which action to do, an Open new account button (though I've never seen an ATM that does this), a Transaction button and a quit button.

    Next, I would avoid using absolute positioning for placement of my buttons, jlabels, fields and whatnots. I would use the layout managers such as BorderLayout, BoxLayout, and GridLayout to make placement of widgets easier. The Sun Swing tutorials have an excellent section on this.

    Next, I'd recommend you not add components after setting the JFrame visible. You should add components first, THEN set the JFrame visible (and pack if using layout managers).

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Can you explain exactly what you want?
    What has to happen when you click the button?
    Check for wich command the used has typed?
    And why do you need the variable outside the actionPerformed?

  7. #7
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    I don't really understand half of that. What do you mean make a true OOP GUI class with constructor and all?

    First off all, I have to make it so that I can type in an action. I would rather use buttons as well, but this if for a class project and we have to type our actions. Would you like me to type out the actual assignment? Maybe that would help?

    About absolute positioning, I dont know how to use the layout managers, and I don't even know what a layout manager is. I guess I'll try to look for a tutorial on this I suppose.

    Does it make a difference if I make the frame visible first or not?

  8. #8
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    Bubbless, yes, I would like to check which command the user typed. I have other code that will check if the typed command is an "O", "T", or "Q". But I don't know how to use that String outside of the actionPerformed. I'm sorry guys...I'm REALLY new to Java.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that he's trying to do a direct conversion of a console program with println console menus into a GUI, but this isn't how event-driven programming works. With time, study, and perseverance, he should be able to succeed though.

  10. #10
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    Well, the assignment was written to be a console program, but I thought it would be better if I did it as a GUI. If I can't figure this out quickly, I'll have to revert back to the console program. This is due Wed.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Xystus777 View Post
    I don't really understand half of that. What do you mean make a true OOP GUI class with constructor and all?
    Have you gone through constructors, objects, and object oriented programming in class yet? Right now your GUI "class" isn't a true class in that all it does is hold a single main method. While you can create programs this way, it gets unwieldy fast, and is not using Java to its greatest advantage.

    First off all, I have to make it so that I can type in an action. I would rather use buttons as well, but this if for a class project and we have to type our actions. Would you like me to type out the actual assignment? Maybe that would help?
    That might help.

    About absolute positioning, I dont know how to use the layout managers, and I don't even know what a layout manager is. I guess I'll try to look for a tutorial on this I suppose.
    They can help you a great deal.

    Does it make a difference if I make the frame visible first or not?
    Do you see your buttons, labels, and textfields when the program first starts? Do you have to move the edge of the program for these to become visible? If so, I think that you've answered your own question here.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Xystus777 View Post
    Well, the assignment was written to be a console program, but I thought it would be better if I did it as a GUI. If I can't figure this out quickly, I'll have to revert back to the console program. This is due Wed.
    Ah, I'd stick with console if I were you. You may be over your head at this stage. If you had time to study the tutorials for a few weeks, then no problem, but I think that you still need some basic Java concepts under your belt before you start tackling GUI.
    Last edited by Fubarable; 03-16-2009 at 10:03 PM.

  13. #13
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Post the actual assignment and I'll give it a try.

  14. #14
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    Alright, well I've decided to stick with console after all. Thanks. Here is my current code:

    Java Code:
    //Andrew Davis & Scott Hempel
    //ATM Machine
    //Lab 8 HW 7
    
    import java.util.Scanner;
    
    public class ATMMachine
    {
    	public static void main(String[] args)
    	{	
    		//Variables
    		Scanner input = new Scanner(System.in);
    		
    		String menu = "";
    		
    		int atmMenu = 1; //1 is on, 0 is off
    		int accountsOpen = -1;
    		
    		NewAccount[] account = new NewAccount[50];
    		
    		
    		while(atmMenu == 1)
    		{
    			System.out.println("Please choose one of the following: " +
    					"\n\tO - Open New Account" +
    					"\n\tT - Make A Transaction On An Account" +
    					"\n\tQ - Quit Program");
    			System.out.print("Your Choice: ");
    			menu = input.next();
    			
    			if (menu.equals("O") || menu.equals("o"))
    			{
    				//New account variables
    				String name = "";
    				String address = "";
    				String telephone = "";
    				double initialBalance = 0.0;
    				//Open a new account
    				atmMenu = 1;
    				accountsOpen++;
    				System.out.print("\nPlease enter your first and last name only: ");
    				name = input.nextLine();
    				account[accountsOpen].setName(name);
    				System.out.print("Your name is " + account[0]);
    			}
    			else if (menu.equals("T") || menu.equals("t"))
    			{
    				//Make a transaction
    				atmMenu = 1;
    			}
    			else if (menu.equals("Q") || menu.equals("q"))
    			{
    				//Quit
    				atmMenu = 0;
    				System.out.print("Thank you for using this ATM!");
    			}
    			else
    			{
    				//User didn't choose appropriate action
    				atmMenu = 1;
    				System.out.println("\nPlease choose an appropriate action!" +
    						"\nPress Enter To Continue");
    				input.nextLine();
    				input.nextLine();
    			}
    		}
    	}
    	
    	class NewAccount
    	{
    		private String name = "";
    		private String address = "";
    		private String telephone = "";
    		private double initialBalance = 0;
    		
    		public NewAccount()
    		{
    			
    		}
    		public NewAccount(String name, String address, String telephone, double initialBalance)
    		{
    			this.name = name;
    			this.address = address;
    			this.telephone = telephone;
    			this.initialBalance = initialBalance;
    		}
    		public String getName()
    		{
    			return name;
    		}
    		public void setName(String name)
    		{
    			this.name = name;
    		}
    		public String getAddress()
    		{
    			return address;
    		}
    		public void setAddress(String address)
    		{
    			this.address = address;
    		}
    		public String getTelephone()
    		{
    			return telephone;
    		}
    		public void setTelephone(String telephone)
    		{
    			this.telephone = telephone;
    		}
    		public double getInitialBalance()
    		{
    			return initialBalance;
    		}
    		public void setInitialBalance(int initialBalance)
    		{
    			this.initialBalance = initialBalance;
    		}
    	}
    	
    	class Transaction
    	{
    		private double balance = 0;
    		String[] transactionHistory = new String[3];
    		String[] tempTransaction = new String[1];
    		
    		public Transaction()
    		{
    			
    		}
    		public double getBalance()
    		{
    			return balance;
    		}
    		public void depositAmount(double deposit)
    		{
    			balance += deposit;
    			transactionHistory(deposit);
    			
    		}
    		public void withdrawAmount(double withdraw)
    		{
    			if (balance >= withdraw)
    			{
    				balance -= withdraw;
    				transactionHistory(withdraw);
    			}
    			else
    			{
    				if (balance < 10)
    				{
    					balance -= balance;
    					transactionHistory(withdraw);
    				}
    				else
    				{
    					balance -= 10;
    					transactionHistory(withdraw);
    				}
    			}
    		}
    		public String[] transactionHistory(double trans)
    		{
    			String transaction = String.valueOf(trans);
    			transactionHistory[0] = transactionHistory[1];
    			transactionHistory[1] = transactionHistory[2];
    			transactionHistory[2] = transaction;
    			return transactionHistory;
    		}
    	}
    }
    I'm having an issue creating 50 instances of my NewAccount class. Okay, now here is the "actual assignment".

    The following main menu is repeated until 'q' is chosen.
    o - Open new account
    t- perform Transaction on an account
    q - quit program

    When a new account is opened, that is, when 'o' is chosen, the customer provides name, address, telephone, and an initial balance to open the account. The information needs to be in the format as described below. The name must contain only two words, the first name and the last name. For example "Jane Doe", "John Smith", "Bob Barr" etc. If an invalid name is entered you must keep asking for the name until a valid name is entered.

    The address can be any string. The telephone number should be of the format "ddd-ddd-dddd" where each d is a digit. The initial balance with which the account is opened must be a positive number. Once an account is opened, the customer gets an account number back from the program. The account number has to be different for each account. You can start the account number at 578 and increment this by one for each new account number.

    Once an account is opened, a customer may choose a transaction on their account by picking 't' from the main menu. To execute any transaction the customer must first enter their correct account number or their correct zip code in the address. Zip code is the last five digits in the address string. If they do not have a zip code in the address string, then they must have a correct account number. Then the customer can choose to withdraw money, deposit money, make a balance enquiry or request transaction details using the following menu.

    d- Deposit amount
    w - Withdraw amount
    e - enquire balance on my account
    t - request transaction details
    g - go back to the main menu

    If 'd' is chosen, the customer is asked for an amount to be deposited and that amount is added to the account. If 'w' is chosen, the customer is asked for the amount that he/she wants to withdraw. If the balance is more than that amount, then withdrawal is done successfully. If the balance is not enough to cover the withdraw, the account is charged $10, if the account has $10, otherwise if the account has less than $10, the account is charged whatever amount is in the account. If 'e' is chosen, the current balance is displayed. If 't' is chosen the last three changes to the account are displayed (ex. +9.58, -100.53, +100.38). 'g' goes back to the main menu. Since all the accounts are stored in an array, it is possible to interleave the transaction of various customers. For example, a customer will open an account immediately another customer may perform a transaction. Make sure you can store up to 50 accounts in the system.

    You must validate every input that is given. You must ensure that menu choices are validated, amount are validated etc. You must present also documentation of the project.



    That's the assignment. My code is obviously not completed yet, i'm just running into some errors along the way. Thanks guys!!!
    Last edited by Xystus777; 03-16-2009 at 11:50 PM.

  15. #15
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    That's quite an assigment. :)
    I'm sorry but I'm not going to try to make the whole assignment.
    Whats your latest issue or which error do you get?

  16. #16
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    I don't expect you to do the assignment for me. I want to learn. Right now, I'm having trouble with this:

    NewAccount[] account = new NewAccount[50];

    I'm hoping this creates 50 instances of my NewAccount class?

    Also, then I will be asking for a first and last name when opening an account. If it's valid (which I have covered), then set it to the first instance of account, which I assume is:

    account[accountsOpen].setName(name);

    the compiler doesn't pick it up, but when I run it, it just stops and goes to Debug mode. I'm using Eclipse by the way. Any ideas?

  17. #17
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    What do you mean with instances.
    (I'm from Belgium so my english isn't perfect :))

    Do you want to create 50 accounts at once and then add all the names etc. or do you want to add them one by one?

  18. #18
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    I want to create 50 default accounts at runtime, then change them as I need them. Everytime I open an account, I do "accountsOpen++" so that when I use "account[accountsOpen]" it should be a reference to that particular account. And I wanted to set particular names, addresses, and whatever to each account instance.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    When you create an array of 50 objects, you are only creating 50 placeholders for these objects; you don't create the objects themselves. To fix this, after you create the array, you could loop through it and create new object instances for each item in the array.

    for example, if I have a class called "Fubar", to create an array of 100 usable Fubar objects, I could do this:
    Java Code:
    Fubar[] fubars = new Fubar[100];
    for (int i = 0; i < fubars.length; i++)
    {
       fubars[i] = new Fubar();  //instantiate each item in the array
    }
    You can think of creating an array as if you created a parking lot with 50 spaces. Even though you've created the lot, there are no cars initially. To be able to use cars, you have to fill the spaces with cars first.

  20. #20
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Exclamation

    Okay, so I did that, and I'm getting an error. Here is my code:

    Java Code:
    NewAccount[] account = new NewAccount[50];
    		
    		for (int i = 0; i < account.length; i++)
    		{
    			account[i] = new NewAccount(); //ERROR IS ON THIS LINE!!!!
    		}
    However, I'm getting an error under "new NewAccount[50]" that says:
    "No enclosing instance of type ATMMachine is accessible. Must qualify the allocation with an enclosing instance of type ATMMachine (e.g. x.new A() where x is an instance of ATMMachine)."

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 02-11-2009, 11:12 PM
  2. Applet buttons
    By h3ckf1r3 in forum Java Applets
    Replies: 6
    Last Post: 09-22-2008, 09:15 PM
  3. How to use Radio Buttons
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:26 PM
  4. How to use SWT Buttons
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM
  5. information from a textfields
    By bbq in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-28-2007, 05:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •