I want my JFrame to move despite disabling Decorrations.
i added a mouseListener after some googling but still didnt help.
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new FlatDarkLaf());
} catch (Exception errorDesign) {
logError(errorDesign);
}
JFrame frame = new JFrame();
frame.setBounds(1600, 400, 500, 800);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setVisible(true);
frame.addMouseListener(new MouseAdapter() {
private Point mouseOffset;
@Override
public void mousePressed(MouseEvent e) {
mouseOffset = e.getPoint();
}
@Override
public void mouseDragged(MouseEvent e) {
Point newLocation = e.getLocationOnScreen();
newLocation.translate(-mouseOffset.x, -mouseOffset.y);
frame.setLocation(newLocation);
}
});
}
someone who knows what i did wrong?
frame.addMouseMotionListener()must be used to track motion events such as mouse dragged. TheMouseAdapteralready extends bothMouseListenerMouseMotionListenerso we can save this into a variable and re-use it inframe.addMouseListener()andframe.addMouseMotionListener().See the Java Tutorial for more help: https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html