GBL question
I've been trying to master the GBL .. hahaha!
in my reading i see that an anchor controls the location of the component within its cell (if it is smaller than its display cell).
and the weight{x,y} control how extra space is distributed to cells.
so why is it that i can't do something like this and have the button show up in the upper left corner?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
publicclass GridBadLayoutTestextends JPanel{
private GridBagLayout gbl =new GridBagLayout();
private GridBagConstraints c =new GridBagConstraints();
public GridBadLayoutTest(){
super();
init();
}
privatevoid init(){
setBorder(new LineBorder(Color.RED, 2));
setLayout(gbl);
setPreferredSize(new Dimension(500, 500));
JLabel label =new JLabel("Label 1");
label.setBorder(new LineBorder(Color.BLACK, 2));
c.anchor = GridBagConstraints.FIRST_LINE_START;
//c.weightx = 0.01;
//c.weighty = 0.01;
c.fill = GridBagConstraints.NONE;
add(label, c);
}
privatestaticvoid createAndShowGUI(){
//Create and set up the window.
JFrame frame =new JFrame("ActionDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create/set menu bar and content pane.
GridBadLayoutTest demo =new GridBadLayoutTest();
demo.setOpaque(true);
frame.setContentPane(demo);
//Display the window.
frame.pack();
frame.setVisible(true);
}
publicstaticvoid main(String[] args){
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowGUI();
}
});
}
}
if you uncomment the two weight{x,y} lines, it looks like i expect it to. what ive found is that if i anchor horizontally i have to set the weightx and if i anchor vertically i have to set weighty. if i anchor something with both horz and vert .. i have to set both. (you must set them to > 0.0 because setting them to 0.0 is the same as not setting them).
so why does the left over distribution of space affect an anchor? the two just dont seem to be related to me. and nor have i found any documentation that says "anchor wont work unless ..."
ive seen it mentioned that fill does no good if weightx and weighty are not set...but nothing about anchor.

