如何在 Java GUI 中设置文本字段位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5610952/
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
How set text field location in Java GUI?
提问by Ignas
How set text field location in Java GUI?
如何在 Java GUI 中设置文本字段位置?
I tried this:
我试过这个:
public Apletas()
{
inputLine.setLocation(null);
inputLine.setLocation(80, 80);
add(inputLine);
}
But not working.
但不工作。
回答by Behrang Saeedzadeh
First, set the layout of your applet to null
:
首先,将小程序的布局设置为null
:
...
public void init()
{
setLayout(null);
}
...
回答by Andrew Thompson
Ignore setLocation()
/setBounds()
& most especiallysetLayout(null)
. Abandon all hope (& the last remnants of sanity), ye' who enter there.
忽略setLocation()
/ setBounds()
&最特别setLayout(null)
。放弃所有希望(以及最后的理智),你们进入那里。
Set locations of components using layout managers.
使用布局管理器设置组件的位置。
For sizing of components, it is usually sufficient to provide the appropriate arguments in the constructor (e.g. new JTextArea(rows, columns)
), or in some cases, using layout constraints (e.g BorderLayout.CENTER
).
对于组件的大小调整,通常在构造函数中提供适当的参数(例如new JTextArea(rows, columns)
)或在某些情况下使用布局约束(例如BorderLayout.CENTER
)就足够了。
For spacing between components, look into both the javax.swing.border
package and arguments to the constructor of layout managers, or in some cases, layout constraints (e.g GridBagLayout
& GridBagConstraints
).
对于组件之间的间距,查看javax.swing.border
包和布局管理器构造函数的参数,或者在某些情况下,查看布局约束(例如GridBagLayout
& GridBagConstraints
)。
Example:
例子:
//<applet code='Apletas' width='600' height='400'></applet>
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Apletas extends JApplet {
private JTextField inputLine;
public Apletas()
{
inputLine = new JTextField(20);
JPanel mainGui = new JPanel(new BorderLayout(20,20));
mainGui.setBorder(new EmptyBorder(80,80,80,80));
mainGui.add(inputLine, BorderLayout.NORTH);
mainGui.add(new JScrollPane(new JTextArea(20,10)), BorderLayout.CENTER);
JTree tree = new JTree();
tree.expandRow(2);
mainGui.add(new JScrollPane(tree), BorderLayout.WEST);
setContentPane(mainGui);
validate();
}
}
To compile & run
编译和运行
prompt> javac Apletas.java
prompt> appletviewer Apletas.java
See also
也可以看看
Laying Out Components Within a Container& How to Use Bordersin the Java Tutorial.
回答by Henry Wang
You have to null the default layout as by using the code
您必须使用代码将默认布局设为空
setLayout(null);
and then can use the setBounds method to locate your swings it goes like this
然后可以使用 setBounds 方法来定位你的摆动它是这样的
JTextField jt = new JTextField();
jt.setBounds(x,y,width,height);
回答by Jim Blackler
I'm not sure why you would .setLocation(null)
on a text field and then set it again.
我不确定您为什么要.setLocation(null)
在文本字段上然后再次设置它。
Is inputLine declared elsewhere (in which case post the full code here please) or are you missing a line like
inputLine 是否在其他地方声明(在这种情况下,请在此处发布完整代码)或者您是否缺少类似的行
TextField inputLine = new TextField("Hello world");