java 使用 FlowLayout 时 JTextField 显示为狭缝...请解释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10549627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JTextField displayed as slit when using FlowLayout...please explain
提问by TrashCan
Can someone please explain to me, why every time I use the FlowLayout Layout manager my textfields are displayed as slits.
有人可以向我解释一下,为什么每次我使用 FlowLayout 布局管理器时,我的文本字段都显示为狭缝。
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);
}
}
回答by Robin
From the Swing layout manager tutorial
来自 Swing 布局管理器教程
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
FlowLayout 类将组件放在一行中,大小为其首选大小。如果容器中的水平空间太小而无法将所有组件放在一行中,则 FlowLayout 类使用多行。如果容器比一行组件所需的宽度要宽,则默认情况下,该行在容器内水平居中
So you need to adjust the preferred size of your textfield, preferably by using the setColumns
method.
因此,您需要调整文本字段的首选大小,最好使用该setColumns
方法。
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
请注意,如果您希望您的文本字段跨越整个宽度,您可能想要使用另一种布局,那么FlowLayout
出于上面引用的原因
For example, the following code gives a nice looking JTextField
, but I have hardcoded the number of columns
例如,下面的代码看起来不错JTextField
,但我已经硬编码了列数
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);
}
} );
}
}
回答by fiveelements
Use: JTextField tf = new JTextField(25);
使用:JTextField tf = new JTextField(25);
Instead of: JTextField tf = new JTextField(); tf.setSize(base.getWidth(), 25);
而不是: JTextField tf = new JTextField(); tf.setSize(base.getWidth(), 25);
回答by Guillaume Polet
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.
当您使用 LayoutManagers 时,不要尝试手动设置大小和位置。您与 LayoutManager 的功能相冲突。布局管理器根据约束和首选/最小/最大尺寸来调整和定位组件。大多数 Swing 组件会自动处理,因此您通常不应强制使用它们的首选大小。
As for your JTextField, since it does not contain any text, its preferred size is close to nothing. Use setColumnsto indicate a preferredSize or invoke setPreferredSize
至于您的 JTextField,由于它不包含任何文本,因此其首选大小几乎为零。使用setColumns来指示一个 preferredSize 或调用setPreferredSize
回答by libz
Thanks!
谢谢!
The parameter that using in the JTextField function is very important!
JTextField 函数中使用的参数非常重要!
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.
如果将 20 更改为 30 或较大,可能是您找不到它。可能是 txt_search 在下一行。