Solve the problem of component overwrite in Java!
Sometimes there are two component overrides when moving a component, but you want the overridden component to show up or not be overwritten!
when designing the GUI, you can already define the stacking order of the components (in the order in which the components are placed).
The real hassle is responding to which component, which creates a linked list, saves the component objects sequentially, scans the list in response to the event, and presses
Select the response Component object in the order of precedence in the linked list.
So to meet their own needs, then add components in the time to pay attention to the order is good!
In addition, if you do not want to use the above method, then you use JLayeredPane this class, layered panel can help you solve the problem in the overlay!
For example Movetofront (componet), Movetoback (Component), SetPosition (), Setlayer () ....
Import Java.awt.button;import java.awt.dimension;import Java.awt.flowlayout;import Java.awt.frame;import Java.awt.point;import Java.awt.event.mouseadapter;import Java.awt.event.mouseevent;import Java.awt.event.mousemotionadapter;import Java.awt.event.windowadapter;import Java.awt.event.WindowEvent;import javax.swing.jlayeredpane;//applet to implement mouse Move component public class Parentandchild {static point oldpt;static jlayeredpane pane = new JLayeredPane (); static button btn = New button (), btn1 = New button ("giggle");p ublic static void Main (string[] args) {Frame fm = new Frame ("hahaha"), fm.setvisible (True);p ane.setlayout (New FlowLayout ()), Fm.add (pane, "Center"), Btn.setlabel ("La la la") ; Btn.addmouselistener (New Mouseadapter () {public void mousepressed (MouseEvent e) {oldpt=e.getlocationonscreen ();} public void mousereleased (MouseEvent e) {pane.setposition (BTN,-1);}); Btn.addmousemotionlistener (New Mousemotionadapter () {public void mousedragged (MouseEvent e) {Point pt = E.getlocationonscreen (); Btn.setlocation (New PoiNT (BTN.GETX () + Pt.x-oldpt.x, btn.gety () + pt.y-oldpt.y)); oldpt=pt;}}); Btn.setpreferredsize (new Dimension);p Ane.add (BTN);p ane.add (BTN1); Fm.setsize (new Dimension (600, 800)); Fm.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}});}}
Troubleshoot problems with component overlap in Java (e.g. when mouse moves components)