简体   繁体   English

java-FLowLayout()中的两个图像在移动时将消失

[英]java - two images in FLowLayout() will disappear when moved

I have the following code that produces two seperate .pngs on the screen. 我有以下代码在屏幕上生成两个单独的.png。 These .pngs are meant to be moved by the mouse and all is working well except for when they are dragged the are seemingly under some type of layer and the disappear when dragged more than an inch. 这些.png可以通过鼠标移动,并且所有工作都很好,除了它们被拖动时,它们似乎在某种类型的图层下,而在拖动超过一英寸时消失。 Any advice so appreciated. 任何建议,不胜感激。

 import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

public class TestMouseDrag {

public static void main(String[] args) {
    new TestMouseDrag();
}

public TestMouseDrag() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {


            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout());
            frame.add(new DragMyIcon("C:\\Users\\anon\\Desktop\\Hobbit.png"));
            frame.add(new DragMyIcon("C:\\Users\\anon\\Desktop\\alien.png"));

            frame.setSize(800,800);

            frame.setVisible(true);
        }
    });
}

public class DragMyIcon extends JPanel {

    public static final long serialVersionUID = 172L;
    private JLabel label;

    public DragMyIcon(String path) {


        ImageIcon icon = null;

        icon = new ImageIcon(path);

        label = new JLabel(icon);



        add(label);

        MouseHandler handler = new MouseHandler();
        label.addMouseListener(handler);
        label.addMouseMotionListener(handler);

    }

}

protected class MouseHandler extends MouseAdapter {

    private boolean active = false;
    private int xDisp;
    private int yDisp;

    @Override
    public void mousePressed(MouseEvent e) {
        active = true;
        JLabel label = (JLabel) e.getComponent();

        xDisp = e.getPoint().x - label.getLocation().x;
        yDisp = e.getPoint().y - label.getLocation().y;

        label.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        active = false;
        JLabel label = (JLabel) e.getComponent();
        label.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (active) {
            JLabel label = (JLabel) e.getComponent();
            Point point = e.getPoint();
            label.setLocation(point.x - xDisp, point.y - yDisp);
            label.invalidate();
            label.repaint();

        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }
}}

Just @Override getPreferredSize in you JPanel and just pack() your frame, instead of setting the size and it works fine. 在您的JPanel只需@Override getPreferredSize ,然后仅将框架pack() ,而不用设置大小,效果很好。

   frame.pack();
   ...

   public class DragMyIcon extends JPanel {
       ...
       @Override
       public Dimension getPreferredSize() {
           return new Dimension(400, 600);
       }
   }

The thing about this remember though, these are two separate panels (as you're creating two separate instances of DragMyIcon ), so you will only be able to move the label the extent of the containing panel. 不过请记住,这是两个单独的面板(当您创建两个单独的DragMyIcon实例时),因此您将只能在包含面板的范围内移动标签。 If you want to be able to move them all over the screen, you need to add them to the same panel. 如果要在整个屏幕上移动它们,则需要将它们添加到同一面板上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM