Results 1 to 2 of 2
  1. #1
    przenica is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default GridBagLayout doesn't work

    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class JadroCzasu extends JPanel {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -4828549146735079562L;
    	
    	
    	
    	public JadroCzasu(){
    		setPreferredSize(new Dimension(700, 600));
    		setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1,1,2,2,Color.black),BorderFactory.createEmptyBorder(5,5,5,5)));
    	}
    	
    	public static void dodajPanele(Container kontener){
    		JPanel Tarcza1, Tarcza2, menuglowne, menu1, menu2;
    		kontener.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		Tarcza1=new JPanel();
    		c.fill = GridBagConstraints.VERTICAL;
    		c.gridx=0;
    		c.gridy=0;
    		c.gridwidth=1;
    		c.gridheight=2;
    		Tarcza1.setBackground(Color.blue);
    		kontener.add(Tarcza1, c);
    		
    		Tarcza2=new JPanel();
    		c.gridx=1;
    		c.gridy=0;
    		c.gridwidth=1;
    		c.gridheight=2;
    		Tarcza2.setBackground(Color.green);
    		kontener.add(Tarcza2, c);
    		
    		menuglowne=new JPanel();
    		c.gridx=2;
    		c.gridy=0;
    		c.gridwidth=1;
    		c.gridheight=3;
    		menuglowne.setBackground(Color.gray);
    		kontener.add(menuglowne, c);
    		
    		menu1=new JPanel();
    		c.gridx=0;
    		c.gridy=2;
    		c.gridwidth=1;
    		c.gridheight=1;
    		menu1.setBackground(Color.darkGray);
    		kontener.add(menu1, c);
    		
    		menu2=new JPanel();
    		c.gridx=1;
    		c.gridy=2;
    		c.gridwidth=1;
    		c.gridheight=1;
    		menu2.setBackground(Color.darkGray);
    		kontener.add(menu2, c);
    	}
    	
    	private static void stworzipokazGUI() {
            JFrame frame = new JFrame("Jądro Czasu");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JComponent kontener = new JadroCzasu();
            kontener.setOpaque(true);
            frame.setContentPane(kontener);
            dodajPanele(kontener);
    
            frame.pack();
            frame.setVisible(true);
        }
    	
    	public static void main(String args[]){
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	stworzipokazGUI();
                }
            });
    
    
    	}
    	
    }
    I just get some small rectangles in the middle. Why? :( I want big panels covering whole grid.
    Last edited by przenica; 01-16-2011 at 01:14 PM.

  2. #2
    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 przenica View Post
    I just get some small rectangles in the middle. Why? :( I want big panels covering whole grid.
    Whenever I see this type of statement, the first thing I look for is how you're setting your GridBagConstraint weightx and weighty fields, and as expected, you're not. You will want to fix this, and will really want to read some of the many GridBagLayout tutorials that are out there so that other nuances don't trip you up.

Similar Threads

  1. [AWT] GridBagLayout Help.
    By Sandia_man in forum AWT / Swing
    Replies: 2
    Last Post: 05-23-2010, 08:54 PM
  2. GridBagLayout
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 10:12 PM
  3. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 08:35 AM
  4. GridBagLayout
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 12:16 AM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 08:03 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
  •