java 使用“setlayout(null)”时小部件的行为异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12308764/
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
Widgets behaving strangely when using "setlayout(null)"
提问by ?????
I'm doing the following call in my code:
我在我的代码中进行以下调用:
...
setLayout(null);
...
I'm trying to place a button and a textfield by specifying their x and y coordinates.
我试图通过指定它们的 x 和 y 坐标来放置一个按钮和一个文本字段。
The problem when I run the program (either with Eclipse or BlueJ) is that I need to run on the panel up to the position of the button and the textfield in order to see respectively the button and the textfield.
当我运行程序(使用 Eclipse 或 BlueJ)时的问题是我需要在面板上运行到按钮和文本字段的位置,以便分别查看按钮和文本字段。
When I find the textfield, it is small. Only when I start writing it assumes the size I specified.
当我找到文本字段时,它很小。只有当我开始写作时,它才会假定我指定的大小。
Does anyone know how to solve it?
有谁知道如何解决它?
回答by lbalazscs
Avoid setLayout (null), unless you have a very good reason for it. You can learn about layout managers here: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
If you still want to use a null layout, you have to set the width and height of the component, not just its x and y position (see the setSize method).
避免 setLayout (null),除非你有很好的理由。您可以在此处了解布局管理器:http: //docs.oracle.com/javase/tutorial/uiswing/layout/using.html
如果你仍然想使用空布局,你必须设置组件的宽度和高度,而不仅仅是它的 x 和 y 位置(参见 setSize 方法)。
From the link mentioned above:
从上面提到的链接:
Although we strongly recommend that you use layout managers, you can perform layout without them. By setting a container's layout property to null, you make the container use no layout manager. With this strategy, called absolute positioning, you must specify the size and position of every component within that container. One drawback of absolute positioning is that it does not adjust well when the top-level container is resized. It also does not adjust well to differences between users and systems, such as different font sizes and locales.
尽管我们强烈建议您使用布局管理器,但您可以在没有它们的情况下执行布局。通过将容器的布局属性设置为 null,您可以使容器不使用布局管理器。使用这种称为绝对定位的策略,您必须指定该容器内每个组件的大小和位置。绝对定位的一个缺点是当顶层容器被调整大小时它不能很好地调整。它也不能很好地适应用户和系统之间的差异,例如不同的字体大小和区域设置。
回答by Nick Rippe
I'd recommend using the setBounds
method instead of the setLocation
我建议使用该setBounds
方法而不是setLocation
JTextField tf = new JTextField(10);
Dimension d = tf.getPreferredSize();
tf.setBounds(x, y, d.width, d.height);
Of course, if you're using a null Layout manager, you also need to take care of your preferredSize
. Here's an example that incorporates all the major aspects:
当然,如果您使用的是空布局管理器,您还需要注意您的preferredSize
. 这是一个包含所有主要方面的示例:
import java.awt.*;
import javax.swing.*;
public class TestProject extends JPanel{
public TestProject(){
super(null);
JTextField tf = new JTextField(10);
add(tf);
Dimension d = tf.getPreferredSize();
tf.setBounds(10, 20, d.width, d.height);
}
@Override
public Dimension getPreferredSize(){
//Hard coded preferred size - but you'd probably want
//to calculate it based on the panel's content
return new Dimension(500, 300);
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setContentPane(new TestProject());
frame.pack();
frame.setVisible(true);
}
});
}
}