简体   繁体   English

使用LayoutManager时,为什么这些小部件不会出现在我的Frame中?

[英]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 ¹. 我想利用JFrameContainer的默认布局: 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 FlowLayoutaddLayoutComponent的描述如下:

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 ,您可能希望完全使用addaddLayoutComponent或其他内容。 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所指出的,默认的LayoutManagerBorderLayout 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.

相关问题 使用null LayoutManager时,为什么我的JScrollPane与JTextArea不可见? - Why isn't my JScrollPane with a JTextArea visible when using null LayoutManager? 为什么我的JSlider或JLabel不会出现在框架的任何位置? - Why won't my JSlider or JLabel appear anywhere on my frame? JTree:为什么我的目录看起来不像目录? - JTree : Why does my directory don't appear like a directory? 为什么我的日志消息没有出现在日志文件中? - Why don't my log messages appear in log files? 在Swing中使用null布局时,直到将鼠标悬停在它们上时,我的组件才会显示 - When using null layout in Swing, my components don't appear until I hover on them 使用AbstractTableModel时,列名不出现 - Column names don't appear when using AbstractTableModel 添加 RecyclerView Layoutmanager 时我的应用程序停止 - My app stopped when adding a RecyclerView Layoutmanager 为什么在创建连接四时我的董事会没有出现? - Why Doesn't My Board Appear when creating connect four? 为什么在运行此JFrame时我的按钮不出现? - Why won't my buttons appear when I run this JFrame? 为什么内容不显示? 但会在调整框架时出现 - Why does the content not appear ? but it will appear when the frame is adjusted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM