I have the following code:
panel.addMouseListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
frame.setLocation(e.getXOnScreen(), e.getYOnScreen());
}
});
panel
is a JPanel
, and all imports have been included. Now, when I drag the panel
, the frame
doesn't move a tiny bit. Why is this, and how should I go about fixing it?
EDIT: frame
is undecorated.
EDIT: mouseDragged
isn't getting triggered - that's the problem to fix!
EDIT: CODE!!!!!
package user_interface;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
public Test() {
this.setUndecorated(true);
this.setLayout(new GridLayout(3, 3, 3, 3));
for (int i = 0; i < 4; i++) {
this.add(new JPanel());
}
JPanel panel = new JPanel();
panel.setBackground(new Color(0));
panel.setToolTipText("Drag to move frame");
this.addMouseListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
Test.this.setLocation(e.getXOnScreen(), e.getYOnScreen());
System.out.println(e);
}
});
this.add(panel);
for (int i = 0; i < 4; i++) {
this.add(new JPanel());
}
}
public static void main(String... args) {
Test test = new Test();
test.setSize(750, 500);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 3
test.setVisible(true);
}
}