Question about Java container issues

I am using the following:

java.awt.Container.add(Component comp, Object constraints) 

How do I specify a constraint object? I need to be able to place a component inside a container.

Oh and my class extends JInternalFrame if that helps ...

I need to specify the coordinates to put the component in the container

+4
source share
5 answers

The constraints objects depend on which layout manager you are using.

For example, with BorderLayout you will only have some constants: container.add(element, BorderLayout.CENTER)

If the container layout manager has a GridBagLayout , you will have a GridBagConstraints with the specified parameters.

Some layout managers (such as FlowLayout or GridLayout ) do not need any restrictions, since they actually decide how to put things on their own.

As a note, if you need absolute positioning, you will not have a layout manager:

 container.setLayout(null); container.add(element1); Insets insets = pane.getInsets(); element1.setBounds(..); //here you set absolute position 
+1
source

Check out the tutorials for LayoutManagers ! In the examples, you will see what restrictions are used with layouts and how.

+3
source

From java.awt.Container class'es javadoc:

The restrictions are determined by the specific layout manager used. For example, the BorderLayout class defines five constraints: BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, and BorderLayout.CENTER.

The GridBagLayout class requires a GridBagConstraints object. Failure to pass an object of type "the correct type" throws an IllegalArgumentException.

This comment can be found in the protected addImpl method.

+1
source

It depends on the layout manager used. For example, if you use BorderLayout , you can use values ​​such as BorderLayout.CENTER and BorderLayout.NORTH . If you are not using the layout manager, you need to manually set the position of the components.

0
source

The native constraint object depends on the current LayoutManager .

If you use BorderLayout , for example, the constraint object may, for example, be BorderLayout.SOUTH .

0
source

Source: https://habr.com/ru/post/1308047/


All Articles