Here's an example code that creates a JFrame with a JLabel on top of it that you can drag around:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DragLabelExample extends JFrame {
private JLabel label;
private int mouseX, mouseY;
private int labelX, labelY;
private boolean dragging = false;
public DragLabelExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Drag Label Example");
// Create a panel to hold the label and add it to the frame
JPanel panel = new JPanel();
panel.setLayout(null); // This allows us to position components manually
add(panel);
// Create a label to drag around and add it to the panel
label = new JLabel("Drag me!");
label.setBounds(200, 200, 100, 30);
panel.add(label);
// Add a mouse listener to the label to handle drag events
label.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
// Remember where the mouse was clicked relative to the label
mouseX = e.getX();
mouseY = e.getY();
// Remember the label's current position
labelX = label.getX();
labelY = label.getY();
// Start dragging
dragging = true;
}
public void mouseReleased(MouseEvent e) {
// Stop dragging
dragging = false;
}
});
// Add a mouse motion listener to the label to handle drag events
label.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (dragging) {
// Calculate the new position of the label based on the mouse movement
int dx = e.getX() - mouseX;
int dy = e.getY() - mouseY;
int newX = labelX + dx;
int newY = labelY + dy;
// Constrain the label's position to within the panel
newX = Math.max(0, Math.min(panel.getWidth() - label.getWidth(), newX));
newY = Math.max(0, Math.min(panel.getHeight() - label.getHeight(), newY));
// Move the label to its new position
label.setLocation(newX, newY);
}
}
});
// Set the size of the frame and make it visible
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new DragLabelExample();
}
}
This code creates a JPanel to hold the label, sets its layout to null (so we can position components manually), and adds it to the JFrame. It then creates a JLabel, positions it in the center of the panel, and adds it to the panel. It adds a mouse listener and a mouse motion listener to the label to handle drag events.
In the mousePressed event handler, we remember the position of the mouse when it was clicked and the current position of the label. We also set a dragging flag to true to indicate that we are currently dragging the label.
In the mouseReleased event handler, we set the dragging flag to false to indicate that we have stopped dragging the label.
In the mouseDragged event handler, we check if the dragging flag is set and, if so, we calculate the new position of the label based on the mouse movement. We also constrain the label's position to within the panel so that it cannot be dragged offscreen. Finally, we move the label to its new position using the setLocation method.
The main method creates an instance of the DragLabelExample class and shows it onscreen.