简体   繁体   中英

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 ¹.

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 )

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. 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:

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.

Unfortunately, you really do need to code with the specific LayoutManager you're using in mind; depending upon the LayoutManager , you might want to use add , addLayoutComponent , or something else entirely. 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.

EDIT: As pointed out by camickr, the default LayoutManager is BorderLayout . With border layout, you need to use constraints when you use addLayoutComponent ; see the 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),

The default layout manager of the content pane of a JFrame is a BorderLayout not a 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. 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.

So my suggestion is don't try to use LayoutManager or the addLayoutComponent(...) method. 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. They are used by the Container class to layout its children.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM