Pergunta

as far as i know the syntax of the add method is

add(Component , String); 

ex.

add(new Button("North"), BorderLayout.NORTH);

but i saw that syntax of the add method in a class and it passses the compilation proccess.

add(String , Component);

and it was the below add method :

add("North", new Button("North")); 

Can someone explain me what happened here and if is this going to work and why ?

Foi útil?

Solução

Has shown in the Java Doc, the Container class has 5 different add() methods overloads

In your first example, BorderLayout.NORTH is a constant defining the position of your component in the Container.

In your second example, "North" is the name you give to your component.

Outras dicas

The method add(Component , Object); adds a component, with the contraints specified in the second parameter. The BorderLayout class uses String instances for the constraints, but other layouts use other object types. For instance, the GridBagLayout uses an instance of GridBagContraints.

The method add(String , Component); adds a component with its name as the first parameter.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top