0

I'm working at JFrame, and I have these images on JLabel and I want the user to move them so they can choose where to drag them.

public class Level3 extends JFrame {

/**
 * Creates new form Level3
 */
public Level3() {
    initComponents();     
    jLabel16.setVisible(false);
    jLabel17.setVisible(false);
    jLabel18.setVisible(false);
    jLabel19.setVisible(false);
    jLabel20.setVisible(false);
    jLabel21.setVisible(false);
    jLabel22.setVisible(false);
}    private void jButton2ActionPerformed(ActionEvent evt) {                                         
     jLabel16.setVisible(true);
    jLabel17.setVisible(true);
    jLabel18.setVisible(true);
    jLabel19.setVisible(true);
    jLabel20.setVisible(true);
    jLabel21.setVisible(true);
    jLabel22.setVisible(true);
}              
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
student23
  • 68
  • 1
  • 2
  • 12
  • I would suggest a (custom) `WhereverDroppedLayout`. The custom layout should account not only for the positions of the images but for defining the layering of images that overlap. – Andrew Thompson Jun 04 '15 at 18:40
  • BTW - `jLabel16.setVisible(false);` 2 good ways to make a label with no text disappear are to: a) Give it a null icon. b) Give it a transparent icon (if it requires size). – Andrew Thompson Jun 04 '15 at 18:44
  • You can follow this question and the first answer also.Link: http://stackoverflow.com/questions/4893265/dragging-a-jlabel-around-the-screen – Md. Nasir Uddin Bhuiyan Jun 04 '15 at 18:44
  • `jLabel17.setLocation(evt.getXOnScreen() , evt.getYOnScreen() - y_pressed); repaint();` this one worked the image moved but the space between the image and the mouse is so big – student23 Jun 04 '15 at 20:01

1 Answers1

0

If you are just trying to drag a label in the JFrame, you can use this. It's by no mean a perfect code, but it does work for me. So try to modify it to make it better.

public class Test extends JFrame implements MouseMotionListener{

        protected JLabel label; 
        protected Point currentLocation;

        public Test() {
            initComponents(); 

            setSize(new Dimension(500, 500));
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }

        private void initComponents() {
            label = new JLabel("some label"); 
            currentLocation = label.getLocation();

            addMouseMotionListener(this);
            add(label); 
        }

        public JLabel getLabel() {
            return label;
        }

        public void setLabel(JLabel label) {
            this.label = label;
        }


        @Override
        public void mouseDragged(MouseEvent e) {

            Point p = e.getPoint();
            currentLocation.x = (int) p.getX();
            currentLocation.y = (int) p.getY() - 250; // Height/2

            label.setLocation(currentLocation);
        }

        @Override
        public void mouseMoved(MouseEvent e) {

        }

}

And try to use arrays or Listto keep your labels.

R00t
  • 186
  • 1
  • 2
  • 14