Troubleshoot problems with component overlap in Java (e.g. when mouse moves components)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.