Results 1 to 5 of 5
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default [SOLVED] JFrame Layout

    Hi, I just started using swing with JFrames and the like. In teaching myself, I wanted to make a simple username/password verification application. Unfortunately, when I try to set up the layout (not using GridLayout) the buttonResize button I add is taking up the entire frame. I tried adding buttonSubmit after buttonResize, and the same problem happened with buttonSubmit.

    Java Code:
    	    JButton buttonResize = new JButton("Resize");
    	    ActionListener actionListener2 = new ActionListener()
    	    {
    	    	public void actionPerformed(ActionEvent actionEvent)
    	    	{
    	    		frame.setSize(200, 100);
    	    	}
    	    };
    	    buttonResize.addActionListener(actionListener2);
    	    frame.add(buttonResize);
    Java Code:
    	    usernameLabel.setBounds(0, 0, 96, 16);
    	    passLabel.setBounds(0, 16, 96, 16);
    	    username.setBounds(96, 0, 96, 16);
    	    pass.setBounds(96, 16, 96, 16);
    	    buttonSubmit.setBounds(0, 32, 96, 16);
    	    buttonResize.setBounds(96, 32, 96, 16);
    EDIT: Sorry, I just realized there was a section for awt/swing ... Next time I'll remember. ;)
    Last edited by AndrewM16921; 04-09-2009 at 12:47 AM.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Which LayoutManager are you using? If you're not using one at all you have to manually deal with the bounds of every component.

    You're also going to need to call revalidate() and/or repaint() after changing the size of the frame.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    I'm setting all of the bounds manually, just as practice. I used GridLayout and it worked fine, however when I try using setBounds with everything for some reason buttonResize takes up the entire frame. I do use "buttonResize.setBounds(96, 32, 96, 16);" I tried adding buttonSubmit after buttonResize, and the same problem happened with buttonSubmit, and buttonResize was in the right place. So this problem happens with the last thing I add to frame. Here's a picture:

    Last edited by AndrewM16921; 04-09-2009 at 12:48 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by AndrewM16921 View Post
    I'm setting all of the bounds manually
    Yes, but to confirm: which layout manager are you using? Do you setLayout(null) at some point?

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

Similar Threads

  1. Layout manager
    By Nicholas Jordan in forum Java Applets
    Replies: 2
    Last Post: 11-24-2008, 03:02 PM
  2. Border Layout
    By mark-mlt in forum Java Applets
    Replies: 5
    Last Post: 05-12-2008, 09:48 AM
  3. Help with JButton and layout
    By adlb1300 in forum AWT / Swing
    Replies: 1
    Last Post: 12-25-2007, 08:33 AM
  4. Layout Managers
    By gmioannou in forum AWT / Swing
    Replies: 1
    Last Post: 12-24-2007, 04:12 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
  •