Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why do layouts never work the way I want them to?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why do layouts never work the way I want them to?

    Ok so these layout managers are driving me crazy. I am trying to create a Tetris program but my **** keeps on getting resized, and once I fix one thing something else gets moved. I created a simple program that encapsulates my problems:

    package BorderTest;
     
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class BorderTest extends JFrame {
    	public BorderTest() {
    		//setLayout(null);
    		setSize(300, 676);
    		BorderPanel borderPanel = new BorderPanel();
    		add(borderPanel);
    		setVisible(true);
    	}
    	public static void main(String[] args) {
    		new BorderTest();
    	}
    }
     
    class BorderPanel extends JPanel {
    	public BorderPanel() {
    		setSize(300, 676);
    		Border b = BorderFactory.createLineBorder(Color.BLACK, 10);
    		this.setBorder(b);
    		setVisible(true);
    	}
     
    	public void paintComponent(Graphics g) {
    		Graphics draw = g;
    		draw.fill3DRect(10, 150, 40, 40, true);
    		draw.fill3DRect(10, 635, 41, 41, true);
    	}
    }

    When the program is compiled and run, you can see that although the size is set to 300x676 the second rectangle does not show unless the bottom is extended, meaning my JFrame height is somehow getting changed to 640ish. I tried setting the layout to null to see if that helped, but the height is still wrong and now the border is messed up. What is causing these changes, how can I fix it, and why must layouts be so difficult?
    Last edited by jps; November 4th, 2012 at 07:32 PM. Reason: profanity removed


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Why do layouts never work the way I want them to?

    You will want to read the layout tutorials to gain a better understanding, but for one, I don't see you setting or using a preferredSize anywhere, and I don't see where you've called pack() on your JFrame. There are all explained in the tutorials.

Similar Threads

  1. I am having major issues with layouts
    By angryfetus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 6th, 2012, 10:27 PM
  2. Does this work?
    By marmanq in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 20th, 2012, 10:51 AM
  3. Help with Layouts and Panels
    By Zookey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 29th, 2011, 06:48 PM
  4. Positioning elements. Is it possible without layouts?
    By goodguy in forum AWT / Java Swing
    Replies: 6
    Last Post: January 21st, 2011, 02:24 PM
  5. Need help understanding GUI screen layouts and labels
    By Bill_H in forum AWT / Java Swing
    Replies: 3
    Last Post: December 2nd, 2009, 11:50 PM