在Swing中添加另一个可移动图像

时间:2016-03-01 18:44:58

标签: swing

我弄错了一个问题。我试图做的是在JPanel中添加另一个可移动图像,可移动,因为可以用鼠标拖动它。此程序只能查看当时的一个图像并将其拖动到周围。那么我怎样才能在我的程序中拥有多张图片呢? =)

这是我的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
public class test2 {

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

public test2() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
            } catch (InstantiationException ex) {
            } catch (IllegalAccessException ex) {
            } catch (UnsupportedLookAndFeelException ex) {
            }
            JFrame frame = new JFrame();
            Bild bild = new Bild();
            frame.add(new DragMyIcon("katt.gif"));
            frame.setSize(640,460);
            frame.setVisible(true);

        }
   });
}

protected class DragMyIcon extends JPanel {

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

    public DragMyIcon(String path) {
        setLayout(null);
        ImageIcon icon = null;
        icon = new ImageIcon(path);
        label = new JLabel(icon);
        label.setBounds(0,0,icon.getIconWidth(), icon.getIconHeight());
        setBounds(0,0,icon.getIconWidth(), icon.getIconHeight());
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);
        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) {
    }
}}

1 个答案:

答案 0 :(得分:0)

  

我尝试做的是在JPanel中添加另一张可移动图像,

在哪儿?我没有看到任何代码?

您只需在面板中添加一个标签。如果要拖动多个,则需要在面板中添加多个标签。

  

此程序目前只能查看一张图像并将其拖动到周围。

好吧,也许你需要像addImage(Image image, int x, int y)这样的方法。然后当你调用这个方法时:

  1. 使用Icon
  2. 的图像创建JLabel
  3. 使用x / y和图像大小
  4. 设置标签的边界
  5. 将侦听器添加到标签
  6. 将标签添加到面板