[英]Why don't these widgets appear in my Frame when using LayoutManager?
I want to make use of the JFrame
's Container
's default layout : a LayoutManager
, avoiding to specialize it with FlowLayout
¹. 我想利用
JFrame
的Container
的默认布局: LayoutManager
,避免使用FlowLayout
专门化它。
So I wrote this code : 所以我写了这段代码:
LayoutManager layout_manager = this.getContentPane().getLayout();
layout_manager.addLayoutComponent(null, text_field_add_items);
(NB : this
points to a JFrame
object, and text_field_add_items
is a TextField
for which I specified a size thanks to setSize
) (注意:
this
指向一个JFrame
对象, text_field_add_items
是一个TextField
,我通过setSize
为其指定了一个大小)
But nothing appears. 但没有出现。
¹ : I really would want to use LayoutManager
because it will allow me to use other layout than the default one (which is a FlowLayout
), if I need it in the future. ¹:我真的想要使用
LayoutManager
因为它允许我使用除默认布局之外的其他布局(这是一个FlowLayout
),如果我将来需要它的话。 Do you know why ? 你知道为什么吗 ?
Entire source : 整个来源:
package tp4.bundle_clients;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.LayoutManager;
import javax.swing.WindowConstants;
public class Gui extends JFrame {
public Gui() {
this.setTitle("Client's graphical interface");
this.setSize(500, 250);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.addWidgets();
this.setVisible(true);
}
private void addWidgets() {
JTextField text_field_add_items = new JTextField();
text_field_add_items.setSize(100, 100);
JButton button_add_items = new JButton("Add items");
button_add_items.setSize(100, 100);
JTextField text_field_remove_items = new JTextField();
JButton button_remove_items = new JButton("Remove items");
JButton button_display_storage = new JButton("Display storage");
LayoutManager layout_manager = this.getContentPane().getLayout();
layout_manager.addLayoutComponent(null, text_field_add_items);
layout_manager.addLayoutComponent(null, button_add_items);
}
}
In the JavaDoc for FlowLayout
, the description for addLayoutComponent
reads as follows: 在JavaDoc for
FlowLayout
, addLayoutComponent
的描述如下:
Adds the specified component to the layout.
将指定的组件添加到布局中。 Not used by this class.
本课程不使用。
Your code might have worked if the LayoutManager
were different, but with FlowLayout
, nothing happens. 如果
LayoutManager
不同,您的代码可能有效,但是使用FlowLayout
,没有任何反应。
Unfortunately, you really do need to code with the specific LayoutManager
you're using in mind; 不幸的是,您确实需要使用您正在使用的特定
LayoutManager
进行编码; depending upon the LayoutManager
, you might want to use add
, addLayoutComponent
, or something else entirely. 根据
LayoutManager
,您可能希望完全使用add
, addLayoutComponent
或其他内容。 That being said, it might be a good idea to delegate layout code to separate methods or classes so that it's easy to change the LayoutManager
without breaking your code. 话虽这么说,将布局代码委托给单独的方法或类可能是个好主意,这样可以在不破坏代码的情况下轻松更改
LayoutManager
。
EDIT: As pointed out by camickr, the default LayoutManager
is BorderLayout
. 编辑:正如camickr所指出的,默认的
LayoutManager
是BorderLayout
。 With border layout, you need to use constraints when you use addLayoutComponent
; 使用边框布局时,您需要在使用
addLayoutComponent
时使用约束; see the JavaDoc . 看看JavaDoc 。
I really would want to use LayoutManager because it will allow me to use other layout than the default one (which is a FlowLayout),
我真的想要使用LayoutManager,因为它允许我使用除默认布局之外的其他布局(这是一个FlowLayout),
The default layout manager of the content pane of a JFrame is a BorderLayout
not a FlowLayout. JFrame的内容窗格的默认布局管理器是
BorderLayout
而不是FlowLayout。
Using LayoutManager
doesn't make sense because you need to know what the layout manager of the panel is so you can use the layout manager properly. 使用
LayoutManager
没有意义,因为您需要知道面板的布局管理器是什么,以便您可以正确使用布局管理器。 That is many layout managers require the use of a "constraint" when you add the component to the panel. 这就是许多布局管理器在将组件添加到面板时需要使用“约束”。
Only simple layout managers like FlowLayout, GridLayout, BoxLayout allow you to add components without a constraint. 只有像FlowLayout,GridLayout,BoxLayout这样的简单布局管理器允许您添加没有约束的组件。
So my suggestion is don't try to use LayoutManager or the addLayoutComponent(...)
method. 所以我的建议是不要尝试使用LayoutManager或
addLayoutComponent(...)
方法。 Just add the components to the panel with the appropriate constraints when required. 只需在需要时使用适当的约束将组件添加到面板。 It will make the code easier to understand and maintain.
它将使代码更易于理解和维护。
You should not call LayoutManager
methods directly. 您不应该直接调用
LayoutManager
方法。 They are used by the Container
class to layout its children. Container
类使用它们来布局其子类。
You should add components in following way: 您应该按以下方式添加组件:
this.getContentPane().add(text_field_add_items);
If you will need to use another layout in future just change it calling 如果您将来需要使用其他布局,只需更改它即可
this.setLayout(newLayout);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.