Bring a component on a JPanel to front (Java)

后端 未结 5 620
花落未央
花落未央 2021-01-11 20:27

In VB, you can use zOrder. In .Net, it\'s .SetChildIndex.

Before you ask, no I\'m not using a layout manager in this case. If you have two

相关标签:
5条回答
  • 2021-01-11 20:53

    you can also use the LayeredPane of your JFrame. With JFrame.getLayeredPane() you get a reference of the pane in use.

    the add method of LayeredPane has up to 2 additional int parameters. the first specified the Layer of the component you'd add and the second contols the order inside this layer.

    You can find more infos at http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html#layeredpane

    0 讨论(0)
  • 2021-01-11 20:55

    Firstly, this appears to be a poor design. Though label.repaint() works, its not the way you should do it. You are not specifying explicitly how these components overlap one on the other. For that a better approach would be Layered Panes. The java's counterpart for zOrder is Layered Pane. This link should help you: http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

    just tell me if you have any problem.

    0 讨论(0)
  • 2021-01-11 20:59

    It isn't that obvious to make overlapping components behave in the right way. The most supported way is to use an OverlayLayout within your panel.

    0 讨论(0)
  • 2021-01-11 21:00

    When compnents overlap on the panel then you need to tell the panel so it can make sure it repaints the components in their proper ZOrder:

    You do this by overriding the isOptimizedDrawingEnabled() method of the JPanel to return false.

    0 讨论(0)
  • 2021-01-11 21:14

    I'm not sure, but would repaint work? bit of a hack if it does, maybe someone else could find a better way...

    0 讨论(0)
提交回复
热议问题