Author Topic:   FlowLayout - lets component assume its natural shape
psethura
ranch hand
posted April 11, 2000 07:55 AM         
In API,

"A flow layout lets each component assume its natural (preferred) size."

But when I try to create a Button of different size, the size is not affected.
Code:

Frame f = new Frame("h");
f.setLayout(new FlowLayout());
Button b = new Button("a");
b.setSize(350 , 200); //Even If i change , the size ofbutton is unchanged Why??
f.add(b);
f.setSize(400,500);
f.setVisible(true);

maha anna
bartender
posted April 11, 2000 03:43 PM             
FlowLayour Manager always honours the component's preferred size. There is no doubt in that. It can honour the changed size ,only when the component wants to. The 'Button' Component in Java AWT doesn't change its preferred size when you call setSize(..) on that. Each component reacts to the size related methods differently according to their wish. In fact the Button component's preferred size changes when you call the 'setLabel(..)' method. So the changed preferred size is honoured by the FlowLayout Manager. The foll small prog illustrates what I said.
Simillarly other components may react differently for the other methods called on them.
regds
maha anna

import java.awt.*;
class mframe extends Frame{
public mframe(){
Button b1 = new Button("Button1");
b1.setFont(new Font("Monospaced",Font.PLAIN,24));
setLayout(new FlowLayout());
add(b1);

//b1.setSize(100,200);
b1.setLabel("My name is chagned");

setSize(400,300);
setVisible(true);

showSizeInfo(b1);
}

void showSizeInfo(Component comp) {
Font font = comp.getFont();
String style = "????";
switch (font.getStyle()) {
case Font.PLAIN:
style = "plain";
break;
case Font.BOLD:
style = "bold";
break;
case Font.ITALIC: style = "italic";
break;
case Font.BOLD + Font.ITALIC:
style = "bold italic";
}
System.out.println("\n" + comp.getName());
System.out.println(" " + font.getSize() + " " + font.getName() + " " + style);

System.out.println(" actual: \t" + comp.getWidth() + ",\t"+ comp.getHeight());

System.out.println("preferred:\t" + comp.getPreferredSize().width +",\t"+ comp.getPreferredSize().height);

System.out.println(" minimum: \t"+ comp.getMinimumSize().width + ",\t"+comp.getMinimumSize().height);
System.out.println("maximum:\t" +comp.getMaximumSize().width + ",\t"+comp.getMaximumSize().height); }

public static void main(String args[]){ new mframe(); }
}

[This message has been edited by maha anna (edited April 11, 2000).]

|