Results 1 to 3 of 3
  1. #1
    frederic202 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Question Changing buttonsizes in layoutmanager

    Here I have a code to make a game board. My problem is that I want to change the buttonsize in the grid. Im working with a gridbaglayout so i cant simply use the button.setSize() method..
    I tried a lot but nothing seems to work.

    Here is the code:
    Java Code:
    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Grid2
    {
    	private int rijen,kolommen;
    	private JPanel panel= new JPanel(new GridBagLayout());
    	private Cel[][] hetBord;
    	
    	public Grid2(int zijde)
    	{
    		rijen=zijde;
    		kolommen=zijde;
    	}
    	
    	public void setHetBord(Cel[][] cel)
    	{
    		hetBord=cel;
    	}
    	
    	public void addPanel()
    	{
    	
    	Color brown=new Color(80,0,0);
    	int grootte=2*rijen-1;
    	panel.setBackground(Color.black);
    	GridBagConstraints c=new GridBagConstraints();
    	c.fill=GridBagConstraints.BOTH;
    	c.weightx=0.2;
    	c.weighty=0.6;
    	Icon auto1=new ImageIcon(getClass().getResource("afbeeldingen/auto1.gif"));
    	Color rood=new Color(150,0,0);
    	Color blauw=new Color(0,0,150);
    	Color groen =new Color(0,150,0);
    	
    	for(int i=0;i<grootte;i++)
    	{
    		for(int j=0;j<grootte;j++)
    		{
    			if(i%2==0&&j%2==0)
    			{	
    				if(hetBord[j][i].getVoorwerp() instanceof Pion)
    				{	auto1=new ImageIcon(getClass().getResource("afbeeldingen/auto1.gif"));
    					if(j==rijen-1)
    						auto1=new ImageIcon(getClass().getResource("afbeeldingen/auto2.gif"));
    					Color kleur=Color.red;
    					
    					switch (((Pion)hetBord[j][i].getVoorwerp()).getKleur()) 
    					{
    		            case 0: kleur=rood ; break;
    		            case 1: kleur=Color.yellow; break;
    		            case 2: kleur=blauw; break;
    		            case 3: kleur=groen; break;
    					}
    
    					JButton buttonCel=new JButton("",auto1);
    					buttonCel.setBackground(kleur);
    					c.gridx=i;
    					c.gridy=j;
    					panel.add(buttonCel,c);
    				}
    				else
    				{
    				JButton buttonCel=new JButton("c");
    				buttonCel.setBackground(brown);
    				c.gridx=i;
    				c.gridy=j;
    				panel.add(buttonCel,c);
    				}
    									
    			
    			}
    			if(i%2==0&&j%2==1)
    			{
    				final JButton buttonMuur=new JButton("m");
    				buttonMuur.setBackground(Color.gray);
    				buttonMuur.addActionListener(new ActionListener()
    				{
    				  public void actionPerformed(ActionEvent e)
    				  {
    				    buttonMuur.setBackground(Color.black);
    				  }
    				});
    				c.gridx=i;
    				c.gridy=j;
    				panel.add(buttonMuur,c);
    			}
    			if(i%2==1&&j%2==0)
    			{
    				final JButton buttonMuur=new JButton("m");
    				buttonMuur.setBackground(Color.gray);
    				buttonMuur.addActionListener(new ActionListener()
    				{
    				  public void actionPerformed(ActionEvent e)
    				  {
    				    buttonMuur.setBackground(Color.black);
    				  }
    				});
    				c.gridx=i;
    				c.gridy=j;
    	
    				panel.add(buttonMuur,c);
    			}
    
    		}
    	}
    	}
    	
    	public JPanel getPaneel()
    	{
    		return panel;
    	}
    
    }
    The result looks like this:


    The cells with an "m" on it, are supposed to be walls. Those cells need to be smaller than the actual brown board cells.
    The result should be more like this drawing:



    please help me out with this problem! It means a lot to me.

    thanx

  2. #2
    frederic202 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    anyone please?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    setPreferredSize(...)

    GridBagConstraints#weightx / weighty

    db

Similar Threads

  1. Changing the values of a map.
    By Onra in forum New To Java
    Replies: 1
    Last Post: 02-26-2010, 12:20 AM
  2. What layoutmanager is suitable for forms.
    By Somelauw in forum AWT / Swing
    Replies: 9
    Last Post: 12-05-2009, 01:04 PM
  3. Extending or changing DTD for validator
    By dbil in forum Web Frameworks
    Replies: 0
    Last Post: 10-09-2009, 10:54 AM
  4. its not changing bgcolor
    By javanoobita in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 02:29 PM
  5. Changing the Jframe
    By Nemo1959 in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 03:58 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
  •