2014-02-10 55 views
-2

该脚本可以很好地拖动一张图片,但是如果我试图让它们中的两个同时进行,就好像该类只能被调用一次一样?这里是我添加两个imageicons的代码,但只有一个正在显示:为什么我的代码不能同时显示frame.add?

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) { 
    } 
}} 
+2

您明白'JFrame'默认使用'BorderLayout',并且它只允许单个组件驻留在5个可用插槽中的任何一个中。这意味着通过添加第二张图片,您可以有效移除第一张图片。另外,在拖动标签之后尝试调整窗口大小... – MadProgrammer

回答

4

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

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

相关问题