使用FlowLayout时JTextField显示为狭缝...请解释 [英] JTextField displayed as slit when using FlowLayout...please explain

查看:88
本文介绍了使用FlowLayout时JTextField显示为狭缝...请解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释一下,为什么每次我使用FlowLayout布局管理器时 我的文本字段显示为狭缝.

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

推荐答案

来自Swing布局管理器教程

From the Swing layout manager tutorial

FlowLayout类将组件排成一行,并按其首选大小调整大小.如果容器中的水平空间太小而无法将所有组件放在一行中,则FlowLayout类将使用多行.如果容器的宽度超出了一排组件所需的宽度,则默认情况下,该行在容器内水平居中

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

因此,您需要调整文本字段的首选大小,最好使用setColumns方法.

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

请注意,如果您希望文本字段跨越整个宽度,则可能要使用其他布局,出于上述原因,请使用FlowLayout

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

例如,以下代码给出了很好的外观JTextField,但是我已经硬编码了列数

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

这篇关于使用FlowLayout时JTextField显示为狭缝...请解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆