Results 1 to 3 of 3
- 03-22-2010, 01:18 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 7
- Rep Power
- 0
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:
The result looks like this: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 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
- 03-23-2010, 04:20 PM #2
Member
- Join Date
- Mar 2010
- Posts
- 7
- Rep Power
- 0
anyone please?
- 03-23-2010, 06:08 PM #3
Similar Threads
-
Changing the values of a map.
By Onra in forum New To JavaReplies: 1Last Post: 02-26-2010, 12:20 AM -
What layoutmanager is suitable for forms.
By Somelauw in forum AWT / SwingReplies: 9Last Post: 12-05-2009, 01:04 PM -
Extending or changing DTD for validator
By dbil in forum Web FrameworksReplies: 0Last Post: 10-09-2009, 10:54 AM -
its not changing bgcolor
By javanoobita in forum Java AppletsReplies: 1Last Post: 02-21-2009, 02:29 PM -
Changing the Jframe
By Nemo1959 in forum New To JavaReplies: 13Last Post: 09-19-2008, 03:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks