Can someone please explain to me, why every time I use the FlowLayout Layout manager
my textfields are displayed as slits.
I have bumped my head against this problem for some time now, and I can’t seem to figure
out why it goes wrong.
I get a feeling it is a simple thing that I am overlooking time and time again, so if
someone would please explain this phenomenon to me, I would be forever grateful.
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Console
{
public Console()
{
makeConsole();
}
private void makeConsole()
{
JFrame console = new JFrame("ArchiveConsole");
Container base = console.getContentPane();
base.setSize(400, 250);
console.setSize(base.getSize());
base.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5));
JTextField tf = new JTextField();
tf.setSize(base.getWidth(), 25);
base.add(tf);
console.setVisible(true);
}
}
From the Swing layout manager tutorial
So you need to adjust the preferred size of your textfield, preferably by using the
setColumnsmethod.Note that if you want your text field to span the whole width you might want to use another layout then the
FlowLayoutfor the reason quoted aboveFor example, the following code gives a nice looking
JTextField, but I have hardcoded the number of columns