13.8 BorderPane 3

Cliquez içi pour plus de vidéos disponibles sur notre chaine youtube !

So in this video we are going to do the 3rd part of the BorderPane so let’s put controls in the other positions of BorderPane just to see what it looks like. The first one will be a top tag. And inside a label tag. which will have a text = This label is a top position and an alignment = center then, we execute the program But obviously, it is not centered, it is always in the top left corner So why is the label always aligned to the left? Well, the alignment property for a label control determines the location of the label content when there is an empty space in the label. So we put the label in the top position of the BorderPane. And that means that the layout gave the label the full width of the pane. Now, because of this, we expect that aligning the text to the center would mean that the text would be centered in the top position. But the thing to remember is that most controls will not stretch to fill the width that has been allocated to them and that is how the label control behaves. As there was no more empty space in the label, the alignment property does nothing. We will place a border around the label. We need a color style This is exactly as we did with the HBox in the previous video. If runs the program. We can now see that the label does not occupy the full width of the top position. So, what we need to do is center the label itself in the top position rather than center its text. As mentioned above, we cannot define the alignment of the BorderPane itself, but we can define an alignment for each of its children. We will therefore use the BorderPane on the alignment property when we want to align a child in its position in the BorderPane. So I come here and I’m going to type BorderPane.alignment = «center», If we run it we should find who is now centered. Now let’s move on to the left and right positions. So what we’re going to do is add a few labels in these positions using the left and right child elements. So I’m going to come here after the top. And hit the left beacon. We will add a label text = this label is a left position An alignment = center and another for the right. And if we run the program again. We have labels sized to their preferred widths and placed on top of left and right positions. Now there is still the central position. We will create a center beacon. So let’s add a label and give it text that will be too long to fit in the remaining space just to see what happens. Label tag. text = this label is in the center of the position and contains a very long text! And if we run again to see what happens Like, you can see, we have our text which is very long! That was it for the BorderPane part. In the next video, we’ll see more layouts Come on, I’ll tell you next time!