Results 1 to 18 of 18
  1. #1
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default ATM Machine - Action Listeners

    My output is all over the place. I cancelled out the borderlayouts beneath each panel I created and it completely changed the output, and I'm not sure why. The first photo below, shows what it looked like with the layouts and the second shows the output without. I still don't understand why I don't see the digits 1-9.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    
    public class Atm extends JFrame {
    	Atm(){
    		super("ATM");
    		
    		//Create Panels For ATM
    		JPanel buttonPanel1 = new JPanel();    //To Contain Digits 1-9
    		buttonPanel1.setLayout(new GridLayout(3,3));
    		
    		JPanel buttonPanel2 = new JPanel();	 //To Contain Enter, 0, Clear
    		//buttonPanel2.setLayout(new BorderLayout());
    		
    		JPanel titlePanel = new JPanel();    //To Contain Label/Title
    		//titlePanel.setLayout(new BorderLayout());
    		
    		JPanel passwordPanel = new JPanel();	//To Contain JPasswordField
    		//passwordPanel.setLayout(new BorderLayout());
    		
    		//Create 9 Digit Buttons
    		for(int i = 1 ; i <=9 ; i++){
    			buttonPanel1.add(new JButton(" " + i));
    		}
    		//Panel 2 Buttons
    		buttonPanel2.add(new JButton("0"), BorderLayout.CENTER);
    		buttonPanel2.add(new JButton("ENTER"), BorderLayout.LINE_START);
    		buttonPanel2.add(new JButton("CLEAR"), BorderLayout.LINE_END);
    	
    		titlePanel.add(new JLabel("Enter Passcode"), BorderLayout.CENTER);
    		passwordPanel.add(new JPasswordField(10), BorderLayout.PAGE_START);
    		
    	add(buttonPanel1, BorderLayout.NORTH);
    	add(buttonPanel2, BorderLayout.CENTER);
    	add(titlePanel, BorderLayout.PAGE_END);
    	add(passwordPanel, BorderLayout.PAGE_START);
    	}	
    }
    ATM Machine - Action Listeners-screen-shot-2014-03-27-11.59.05-am.jpg

    ATM Machine - Action Listeners-screen-shot-2014-03-27-11.56.26-am.jpg

    Why would setting the layout beneath each object change the layout so much? I stayed consistent in my use of BLayout and GLayout in the program.
    Last edited by javaStooge; 03-27-2014 at 05:10 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: ATM Machine - Action Listeners

    Changing the layouts will of course change the layout of your program. I'm not sure what your question is?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Action Listeners

    I don't understand why I'm getting the layout that I have... I don't understand what I am doing wrong.
    Why does the layout look so messed up?

  4. #4
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: ATM Machine - Action Listeners

    I think that you can't see 9 buttons because it's making just nine buttons one above the others. Because you just tell him to create buttons but not to move each button in a different place... So they're one above the others...

  5. #5
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Action Listeners

    Quote Originally Posted by TheCazziator33 View Post
    I think that you can't see 9 buttons because it's making just nine buttons one above the others. Because you just tell him to create buttons but not to move each button in a different place... So they're one above the others...
    No, I don't think that is the problem, because that is why I used the GridLayout(3,3). It was working fine earlier, before I went to add additional components (JPassword, Text Field).

    Could it have something to do with the BorderLayout._______ components filled out? That they're somehow overlapping or not inputed correctly?
    Last edited by javaStooge; 03-27-2014 at 05:49 PM.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: ATM Machine - Action Listeners

    Quote Originally Posted by javaStooge View Post
    Could it have something to do with the BorderLayout._______ components filled out? That they're somehow overlapping or not inputed correctly?
    Yep. You're adding components to NORTH and PAGE_START, which are the same place. BorderLayout can't have two components in the same place. For that, you'll have to use a nested layout.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: ATM Machine - Action Listeners

    Can you post the code for the first image and the code for the second image? Because i don't understand how you changed the code

  8. #8
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: ATM Machine - Action Listeners

    Ok, maybe i've got a solution... I think that when you don't create a borderlayout for each panel it uses the borderlayout of the window and it resize the dimensions of the objects to fit the size of the location (LINE_START, LINE_END, ecc...).

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,038
    Rep Power
    20

    Default Re: ATM Machine - Action Listeners

    Quote Originally Posted by TheCazziator33 View Post
    Can you post the code for the first image and the code for the second image? Because i don't understand how you changed the code
    The first image is what they want it to look like.
    The second image is what it does look like.
    ETA: WHOOPS...I completely misread the OP!
    :)

    For the OP:
    Where in that code are you setting the layout for the underlying panel of the JFrame?
    That's the one these add() calls are adding to:
    Java Code:
        add(buttonPanel1, BorderLayout.NORTH);
        add(buttonPanel2, BorderLayout.CENTER);
        add(titlePanel, BorderLayout.PAGE_END);
        add(passwordPanel, BorderLayout.PAGE_START);
    because I don't see anything being set to a BorderLayout in there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: ATM Machine - Action Listeners

    Quote Originally Posted by TheCazziator33 View Post
    Ok, maybe i've got a solution... I think that when you don't create a borderlayout for each panel it uses the borderlayout of the window and it resize the dimensions of the objects to fit the size of the location (LINE_START, LINE_END, ecc...).
    It's great that you're trying to help, but please stop guessing at incorrect solutions. I've already mentioned the problem, and everything you've said so far has been incorrect. I know your heart's in the right place, but incorrect help can hurt worse than no help at all.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: ATM Machine - Action Listeners

    Quote Originally Posted by Tolls View Post
    The first image is what they want it to look like.
    The second image is what it does look like.
    ETA: WHOOPS...I completely misread the OP!
    :)

    For the OP:
    Where in that code are you setting the layout for the underlying panel of the JFrame?
    That's the one these add() calls are adding to:
    Java Code:
        add(buttonPanel1, BorderLayout.NORTH);
        add(buttonPanel2, BorderLayout.CENTER);
        add(titlePanel, BorderLayout.PAGE_END);
        add(passwordPanel, BorderLayout.PAGE_START);
    because I don't see anything being set to a BorderLayout in there.
    He's adding it to the JFrame, which has a BorderLayout by default.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Action Listeners

    I think you are correct Cazz and Kevin, but I assumed you could avoid or rather add an additional space above NORTH by using PAGE_START and below SOUTH by using PAGE_END. This doesn't appear to be the case. Given that, I've run into the problem of adding the second panel, buttonPanel2 and the titlePanel, at the bottom of the page. Since I needed to setup the numeric keypad similar to what you might see on your cell phone, I had to create a separate panel from the digit 1-9 in a GridLayout(3,3) for "ENTER", "0", and "CLEAR".

    @KEVIN, you mentioned nested layout..what is that?

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: ATM Machine - Action Listeners

    Quote Originally Posted by javaStooge View Post
    I think you are correct Cazz and Kevin, but I assumed you could avoid or rather add an additional space above NORTH by using PAGE_START and below SOUTH by using PAGE_END. This doesn't appear to be the case. Given that, I've run into the problem of adding the second panel, buttonPanel2 and the titlePanel, at the bottom of the page. Since I needed to setup the numeric keypad similar to what you might see on your cell phone, I had to create a separate panel from the digit 1-9 in a GridLayout(3,3) for "ENTER", "0", and "CLEAR".

    @KEVIN, you mentioned nested layout..what is that?
    You're already using a nested layout with your buttons inside a JPanel with a GridLayout. You'll have to compose your window of multiple JPanels with different layouts to achieve your goal. I assume that's the whole point of the homework.

    More info here: Layouts - Tutorials - Static Void Games
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,038
    Rep Power
    20

    Default Re: ATM Machine - Action Listeners

    Quote Originally Posted by KevinWorkman View Post
    He's adding it to the JFrame, which has a BorderLayout by default.
    Oh....bah!
    It's extending JFrame, isn't it?

    *thrrrrrp*
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Action Listeners

    This is what I was referring to, when I said I cancelled or added "//" to comment the setLayer.

    Java Code:
    Atm(){
    		super("ATM");
    
    		JPanel buttonPanel1 = new JPanel();    //To Contain Digits 1-9
    		buttonPanel1.setLayout(new GridLayout(3,3));
    		
    		JPanel buttonPanel2 = new JPanel();	 //To Contain Enter, 0, Clear
    		//buttonPanel2.setLayout(new BorderLayout());
    		
    		JPanel titlePanel = new JPanel();    //To Contain Label/Title
    		//titlePanel.setLayout(new BorderLayout());
    		
    		JPanel passwordPanel = new JPanel();	//To Contain JPasswordField
    		//passwordPanel.setLayout(new BorderLayout());
    When I included the comments (//):ATM Machine - Action Listeners-screen-shot-2014-03-27-1.16.27-pm.jpg

    Without comments(//): ATM Machine - Action Listeners-screen-shot-2014-03-27-1.16.49-pm.jpg

    This is the result after changing the BorderLayouts when I added the panels. Which leads me to another questions, why is the "0" so much longer and that entire row thinner?

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: ATM Machine - Action Listeners

    You need to take a step back and really understand what each layout is doing. You can't just change random stuff and expect it to work. Recommended reading: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    The zero is so much larger because the layout that you're using is making it larger. Figure out which layout it is, then consult the above link for an explanation of why.

    It'll be much easier to help you debug a single layout at a time than it is to deal with your entire window. Break your program down into smaller individual sections and focus on them one at a time.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,038
    Rep Power
    20

    Default Re: ATM Machine - Action Listeners

    Quote Originally Posted by javaStooge View Post
    I think you are correct Cazz and Kevin, but I assumed you could avoid or rather add an additional space above NORTH by using PAGE_START and below SOUTH by using PAGE_END. This doesn't appear to be the case. Given that, I've run into the problem of adding the second panel, buttonPanel2 and the titlePanel, at the bottom of the page. Since I needed to setup the numeric keypad similar to what you might see on your cell phone, I had to create a separate panel from the digit 1-9 in a GridLayout(3,3) for "ENTER", "0", and "CLEAR".

    @KEVIN, you mentioned nested layout..what is that?
    I posted the API for you yesterday for BorderLayout.
    The very first sentence of which states:
    "
    A border layout lays out a container, arranging and resizing its components to fit in five regions.
    "

    Yes, it's rather silly of them to have provided multiple constants referencing the same thing, but the first two sentences there should have told you what was going on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Action Listeners

    thanks
    Last edited by javaStooge; 03-27-2014 at 08:20 PM.

Similar Threads

  1. question about action listeners
    By MW130 in forum New To Java
    Replies: 2
    Last Post: 05-08-2013, 02:45 PM
  2. Action/Change Listeners
    By Conor-Magee in forum New To Java
    Replies: 21
    Last Post: 11-20-2011, 03:21 AM
  3. action listeners
    By Ike in forum New To Java
    Replies: 13
    Last Post: 11-07-2011, 10:31 AM
  4. Adding Action Listeners
    By mamoonrizwan in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2011, 06:25 PM
  5. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 04:26 AM

Posting Permissions

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