Hi all…
Can someone tell me why how come when I move the panel it does not move with the cursor?
The sample code is below
import javax.swing.*;
public class testDrag extends JFrame {
private JPanel jContentPane = null;
private JLayeredPane lp;
private JPanel movePanel = null;
public static void main(String []args){
new testDrag();
}
public testDrag() {
super();
initialize();
}
private void initialize() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(1000,1000);
this.setContentPane(getJContentPane());
this.setTitle("Test Drag");
this.setVisible(true);
lp = getLayeredPane();
lp.add(getMovePanel(),JLayeredPane.MODAL_LAYER);
}
private javax.swing.JPanel getJContentPane() {
if(jContentPane == null) {
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(new java.awt.BorderLayout());
}
return jContentPane;
}
private JPanel getMovePanel() {
if (movePanel == null) {
movePanel = new JPanel();
movePanel.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red,5));
movePanel.setSize(100, 100);
movePanel.setLocation(10, 10);
movePanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent e) {
//int pX = getX();
//int pY = getY();
//int newX = e.getPoint().x;
//int newY = e.getPoint().y;
//int diffX = newX - pX;
//int diffY = newY - pY;
movePanel.setLocation(e.getPoint().x,e.getPoint().y);
e.consume();
}
});
}
return movePanel;
}
}
The panel always moves around else where… so I really dont understand why…
Can someone one explain why? ??? ???