Welcome to Dream.In.Code
Become a Java Expert!

Join 149,515 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,340 people online right now. Registration is fast and FREE... Join Now!




GridBagLayout not working?

 
Reply to this topicStart new topic

GridBagLayout not working?

kwikness
19 May, 2007 - 09:22 PM
Post #1

New D.I.C Head
*

Joined: 22 Jan, 2007
Posts: 14


My Contributions
I'm trying to use the GridBagLayout for a panel i'm designing. The problem is that when I compile and run the app, the JButton gets placed directly in the middle of the panel, instead of at 0,0 like I coded it to. Can someone tell me what's wrong with this code?? Here are the two methods that use the GridBagLayout..

[code]
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;


public class BlackjackPanel extends JPanel
{
private BufferedImage picOfMe;
private BufferedImage logo;
private BufferedImage dealBtnPic;
private BufferedImage hitBtnPic;
private BufferedImage stayBtnPic;
private BufferedImage splitBtnPic;

private JButton deal;
private JButton hit;
private JButton stay;
private JButton split;

public BlackjackPanel()
{
setLayout(new GridBagLayout());

//dark green
setBackground(new Color(0, 153, 51));

deal = new JButton("Deal");
add(deal, getConstraints(0,0,5,5,GridBagConstraints.CENTER));
}//end BlackjackPanel c'tor

private GridBagConstraints getConstraints(int gridx,
int gridy, int gridWidth, int gridHeight, int anchor)
{
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.ipadx = 0;
c.ipady = 0;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
c.anchor = anchor;

return c;
}//end getConstraints

}//end BlackjackPanel

This post has been edited by kwikness: 20 May, 2007 - 12:18 AM
User is offlineProfile CardPM
+Quote Post

binarygrrl
RE: GridBagLayout Not Working?
24 May, 2007 - 08:21 AM
Post #2

New D.I.C Head
*

Joined: 24 May, 2007
Posts: 1


My Contributions
I think you may misunderstand what gridx and gridy mean. gridx is the column and gridy is the row. If you add a second button and change the anchor location of one of them you will be able to see what's happening easier. The buttons will stay anchored as you resize the window.

CODE

deal = new JButton("Deal");
add(deal, getConstraints(0,0,5,5,GridBagConstraints.LINE_START));
JButton deal2 = new JButton("Deal2");
add(deal2, getConstraints(0,1,5,5,GridBagConstraints.CENTER));


Hope this helps some!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:00PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month