JTextField displayed as slit when using FlowLayout…please explain

◇◆丶佛笑我妖孽 提交于 2019-12-01 05:02:47

问题


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);
    }
}

回答1:


From the Swing layout manager tutorial

The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows. If the container is wider than necessary for a row of components, the row is, by default, centered horizontally within the container

So you need to adjust the preferred size of your textfield, preferably by using the setColumns method.

Note that if you want your text field to span the whole width you might want to use another layout then the FlowLayout for the reason quoted above

For example, the following code gives a nice looking JTextField, but I have hardcoded the number of columns

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;

public class TextFieldWithFlowLayout {
  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame console = new JFrame("ArchiveConsole");
        Container base  = console.getContentPane();
        base.setLayout(new FlowLayout( FlowLayout.CENTER, 5,5));

        JTextField tf = new JTextField();
        tf.setColumns( 20 );
        base.add(tf);
        console.pack();
        console.setVisible(true);
      }
    } );
  }
}



回答2:


Use: JTextField tf = new JTextField(25);

Instead of: JTextField tf = new JTextField(); tf.setSize(base.getWidth(), 25);




回答3:


When you use LayoutManagers, don't try to set size and position manually. You are conflicting with what the LayoutManager does. Layoutmanagers size and position components based on constraints and preferred/minimum/maximum size. Most Swing components handle that automatically, so you should usually not force their preferred size.

As for your JTextField, since it does not contain any text, its preferred size is close to nothing. Use setColumns to indicate a preferredSize or invoke setPreferredSize




回答4:


Thanks!

The parameter that using in the JTextField function is very important!

JPanel panel_buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel_buttons.add(...);
...
panel_buttons.add(...);
JTextField txt_search = new JTextField(20);
panel_buttons.add(txt_search);

If change 20 to 30 or large, may be u couldn't find it.May be the txt_search is in the next line.



来源:https://stackoverflow.com/questions/10549627/jtextfield-displayed-as-slit-when-using-flowlayout-please-explain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!