Results 1 to 2 of 2
  1. #1
    trill is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with gaps in GridLayout

    Hi, I have a grid, with 12 buttons in it. Once displayed there is a small gap inbetween the buttons, maybe 5 pixels. I have been using the setHgap and setVgap methods to try and get rid of this gap but cannot. Can it be done?
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    class MainPanel extends JFrame{
    
    	Container container = getContentPane();
    	JPanel gamePanel = new JPanel();
    
    	String[] buttonsImageNames ={
    
    				"bart0.jpg",
    				"bart1.jpg",
    				"bart2.jpg",
    				"bart3.jpg",
    				"bart4.jpg",
    				"bart5.jpg",
    				"bart6.jpg",
    				"bart7.jpg",
    				"bart8.jpg",
    				"bart9.jpg",
    				"bart10.jpg",
    				"bart11.jpg",
    				};
    	JButton [] buttonArray = new JButton[buttonsImageNames.length];
    	Insets buttons = new Insets(0,0,0,0);
    	ImageIcon [] imagesArray = new ImageIcon[buttonsImageNames.length];
    	GridLayout layout = new GridLayout(0,1,0,0);
    
    
    	public MainPanel()
    
    	{
    		setTitle("Jake's Cash Machine!");
    		setSize(500,430);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		for(int i=0; i<buttonsImageNames.length; i++){
    			buttonArray[i] = new JButton(new ImageIcon(buttonsImageNames[i]));
    			buttonArray[i].setMargin(buttons);
    			gamePanel.add(buttonArray[i]);
    
    		}
    
    		container.setLayout(layout);
    		container.add(gamePanel);
    		container.setBackground(Color.white);
    
    		System.out.println(layout);
    
    		setVisible (true);
    
    	}
    }
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    class MainPanelRx extends JFrame{
    
    	Container container = getContentPane();
    	JPanel gamePanel = new JPanel();
    
    	String[] buttonsImageNames ={
    
    				"bart0.jpg",
    				"bart1.jpg",
    				"bart2.jpg",
    				"bart3.jpg",
    				"bart4.jpg",
    				"bart5.jpg",
    				"bart6.jpg",
    				"bart7.jpg",
    				"bart8.jpg",
    				"bart9.jpg",
    				"bart10.jpg",
    				"bart11.jpg",
    				};
    	JButton [] buttonArray = new JButton[buttonsImageNames.length];
    	Insets buttons = new Insets(0,0,0,0);
    	ImageIcon [] imagesArray = new ImageIcon[buttonsImageNames.length];
    	GridLayout layout = new GridLayout(0,1,0,0);
    
    
    	public MainPanelRx()
    
    	{
    		setTitle("Jake's Cash Machine!");
    		setSize(500,430);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		// Set the gamePanel layout to the GridLayout
    		gamePanel.setLayout(layout);
    		for(int i=0; i<buttonsImageNames.length; i++){
    			buttonArray[i] = new JButton(new ImageIcon(buttonsImageNames[i]));
    			buttonArray[i].setMargin(buttons);
    			gamePanel.add(buttonArray[i]);
    
    		}
    
    		// gamePanel still has the default FlowLayout which
    		// explains the gaps...
    		// Let's leave the contentPanes default BorderLayout
    //		container.setLayout(layout);
    		// and add gamePanel to the center section.
    		container.add(gamePanel);
    		container.setBackground(Color.white);
    
    		System.out.println(layout);
    
    		setVisible (true);
    
    	}
    
    	public static void main(String[] args) { new MainPanelRx(); }
    }

Similar Threads

  1. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM
  2. GridLayout with animation?
    By tojas in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2007, 10:16 PM
  3. Use GridLayout in java
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 12:44 AM
  4. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 11:48 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
  •