为什么我的代码不显示frame.add?

时间:2014-02-10 00:45:20

标签: java swing

该脚本适用于拖动一个图像,但如果我试图让其中两个一次进行,它就好像该类只能被调用一次?这里是我添加两个图像的代码,但只有一个显示:

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() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
            } catch (InstantiationException ex) {
            } catch (IllegalAccessException ex) {
            } catch (UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            frame.add(new DragMyIcon("C:\\Users\\anon\\Desktop\\Hobbit.png")).setLocation(100, 100);
            frame.add(new DragMyIcon("C:\\Users\\anon\\Desktop\\alien.png")).setLocation(100, 100)
            frame.pack();
            frame.setSize(700,700);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

public 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,100, 100);

        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 :(得分:4)

您的代码不尊重它正在使用的布局管理器 - BorderLayout。使用容器在没有指定位置的情况下将组件添加到BorderLayout时,默认情况下会将其放置在BorderLayout.CENTER中,并涵盖之前添加的任何内容。

解决方案:阅读包括BorderLayout在内的布局管理器,了解如何使用它们。 此外,您可能最好不要添加两个DragMyIcon对象,而是更改DragMyIcon以便它允许多个JLabel。