java - How to tell where a JPanel is when dragging a JLabel -


I am pulling a jellabal around the screen, and when I move above the zeppel, it is completely covered. Also, if I release somewhere else, it should be photographed at its original place. I have a part of the picture, but I do not know how it is telling if it is on the zennial. I have my code.

  Import java.awt.Color; Java.awt.Dimension; Import javax.swing *; Import javax.swing.event.MouseInputAdapter; Import java.awt.event.MouseEvent; Public class main {public stable ending end CARD_HEIGHT = 97; Public stable final distance CARD_WIDTH = 73; / ** * Mouse Handler Components * replaces the location of JLab with the mouse * / Public Static Mouse InputAdaptor MouseSandler = New MouseInputEdapter () {Public IntLLDisX; Public int label dcc; Public zero mousePressed (MouseEvent e) {labelDisX = e.getX (); LabelDisY = e.getY (); // Take the card above all other Egate Components (). GetParent () SetComponentZOrder (e.getComponent (), 0); . E.getComponent () getParent () repaint () .; } Public Zero Mouse (MouseEvent E) {// If not above the panel, then go to the original location (above!}} Public Zero MouseDragged (Mouse Event E) {JPNL Panel = (JPNL) E.A.T.C. GetParent (); // Initial new x coordinate int newX = e.getComponent (). GetX () + e.getX () - labelDisX; // Initial new y coordinate int newY = e.getComponent (). GetY ) + E.getY () - did not take the edges of the labelDisY; // JFrame if (newX> gt; panel.getWidth () - CARD_WIDTH) {newX = panel.getWidth () - CARD_WIDTH;} if (new y & Gt; Panel.GetHyight () - CARD_HEIGHT) {newY = panel.getHyight () - CARD_HEIGHT;} (NewX <0) {newX = 0;} if (new y and lt; 0) {newi = 0;} E.getComponent (). SetLocation (newX, newY);}}; / * * * Check to see if JLABL is on JPNL * @ Return * / Public Static Boolean.} / ** * JLabel * @ Ultimate Label * / Public Stable Zero Odorag (JLabel Label) {System.out.println ( Adds the ability to drag the "add to draw"); label.addMouseMotionListener (mouseHandler); Label.addMouseListener (mouseHandler); } Public static zero main (string [] args) {// Create a Jeffrey GeFrom frame = new Jeffrey ("Example Frame"); // JPNL to add to JLLL JPN panel = new JPNL (); // DropTargetArea dropPanel = Add a drop target text field to the center of new DropTargetArea (); DropPanel.setPreferredSize (new dimension (CARD_WIDTH, CARD_HEIGHT)); DropPanel.setBackground (Color.gray); Panel.add (dropPanel); // Add many dragable labels to the container JLabel blue = new JLabel (); Blue.setOpaque (right); Blue.setPreferredSize (new dimension (CARD_WIDTH, CARD_HEIGHT)); Blue.setBackground (Color.blue); AddDrag (blue); Panel.add (blue); // Add container to frame frame. Add (panel); // Display the frame frame. Setphrase size (new dimension (400,400)); Frame.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE); Frame.setVisible (true); Frame.pack (); }}  

I did this using a JLR panel which holds JPNL which It holds itself, using a granular border layout of JPNEL, every small zeppel representing the chess class, and each small zeppel can accept a single jlabble I added the mouse adapter to JLRDepain, and when clicked , Then go to see it Scratches what's going on below the JLABL click. If so, JLABBL is taken up to JLearpier's drag_lare, and then when released, check that the mouse cursor of JPNL is over, and leave JLable, if it is a valid class, otherwise it will Revert to its original location You can see your code.


Comments