By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

Gridbaglayout - mysterious spaces

P: 2
Hi,
I'm trying to design a kind of calculator GUI using gridbaglayout.
At the top of the calculator will be a couple of menus and then will come the section with all the buttons etc.
Initially, I have designed the menu titles purely as labels (since I haven't figured out how to create actual menus - any help there would be great!) but the final menu item is separated by a mysterious space from the other ones.
Here is the code I've got so far - the "About" label is appearing far to the right of the other labels.

LayoutExample window = new LayoutExample();
window.setTitle("Layout Example Application");
window.pack();
window.setVisible(true);

public LayoutExample()
{
//setLayout(new GridLayout(1,2));

p2 = new Panel();
p2.setBackground(Color.gray);
add(p2);
gridBag();
}

public void gridBag()
{
// Set up GridBagConstraints and layout and apply it to the panel
GridBagConstraints c = new GridBagConstraints();
GridBagLayout gridbag = new GridBagLayout();
p2.setLayout(gridbag);

// Create the labels and add them to the panel
Label top = new Label("File");
top.setAlignment(Label.CENTER);
gridbag.setConstraints(top, c);
p2.add(top);

Label top2 = new Label("Edit");
top2.setAlignment(Label.CENTER);
gridbag.setConstraints(top2, c);
p2.add(top2);

Label top3 = new Label("View");
top3.setAlignment(Label.CENTER);
gridbag.setConstraints(top3, c);
p2.add(top3);

c.gridwidth = GridBagConstraints.REMAINDER;
Label top4 = new Label("About");
top4.setAlignment(Label.CENTER);
gridbag.setConstraints(top4, c);
p2.add(top4);

// On next line, create the textarea
TextArea comments = new TextArea(3, 50);
comments.setEditable(true);
comments.setText("Comments:");
gridbag.setConstraints(comments, c);
p2.add(comments);

}
Sep 19 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.