问题
I am writing a small card game and so far i have the following :
I have a custom JPanel Card component where i override the getPreferredSize() method
I also have a custom HandView component which essentially just adds a bunch of card components to it. I also override the getPreferredSize() and use a flowlayout.
My problem is when too many Card components are added to the handView they are not shown. I do not want the card components and the hand component to resize . What i would like is to make the card components start overlapping each other when too many are added. Is that possible with a flowlayout?
回答1:
Is that possible with a flowlayout?
You can specify a horizontal gap when creating the FlowLayout
. If you use a negative value then you will get overlapping.
However, you may want to consider the Overlap Layout which was written specifically for this type of layout. It provides different option for how the overlapping should work.
What i would like is to make the card components start overlapping each other when too many are added
In both cases you would need to dynamically reset the gap as cards are added/removed and then revalidate the panel.
来源:https://stackoverflow.com/questions/32910225/make-components-overlap-in-flowlayout