JLayeredPane: what is the functional difference between depth and position?

I am working on a Connect Four game for a Java school project. I have a β€œhow” to JLayeredPane, and it works as expected, but I don’t quite understand the β€œwhy” behind a certain concept.

Here is my understanding:

JLayeredPane is a container similar to JPanel, which allows you to specify the depth and position for each component. Depth is an integer, with 0 being the bottom layer, n-1 being the top layer, n the number of components. A position is an int (yes, one uses the Integer wrapper class, and the other uses a primitive!), Which indicates the component position inside the layer, with 0 being the topmost layer, -1 is the lowest layer and positive ints in between, the lower the number , the higher the position. Thus, four components in one layer can be ordered to the position 0, 1, 2, -1 from the highest to the lowest.

My question is that you need to have both concepts?

For example, I created three JLabels with images: frontBoard, backBoard and part. A piece sometimes goes in front of the frontBoard and sometimes goes between the frontBoard and the backBoard. Consider the second example.

I can get the same effect in one of the following ways:

1) I can set the backBoard to level 0, position 0; piece to layer 1, position 0; and frontBoard to level 2, position 0

 or 

2) I can set the backBoard to level 0, position -1; part to layer 0, position 1; and frontBoard to level 0, position 0

I tested both of these methods, and I cannot find the functional difference between the two methods.

Can anyone shed light on this mystery for me?

+3
source share
1 answer

Firstly, the best thing to do in this case is to take a look at the tutorial, they are usually very informative: http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

, javadoc , JLayeredPane.

, , : , . , :

  • "" : , 0 . .
  • , , z- : "position". n , 0, 0 n-1.

, , "", , z "". , .

, , :

  • DEFAULT_LAYER
  • PALETTE_LAYER
  • MODAL_LAYER
  • POPUP_LAYER
  • DRAG_LAYER

, , : , , . , n. , .., .

. , , MODAL_LAYER, : , .

, Swing ( JLayeredPane ), setVisible (boolean) setModal (boolean) JDialog, , .

+1

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


All Articles