Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Thomas is correct, but two alternatives are worth noting:</p> <ul> <li><p>This <a href="https://stackoverflow.com/questions/2562685">example</a> shows how to drag a component using <code>JLayeredPane</code>; this <a href="https://stackoverflow.com/questions/2563350">variation</a> expands on the concept; this more recent <a href="https://stackoverflow.com/questions/4894516">example</a> uses a similar approach.</p></li> <li><p>The code below shows how to use a <code>MouseMotionListener</code>; this more complex <a href="https://stackoverflow.com/a/11944233/230513">example</a> uses the same principle.</p></li> </ul> <p>Code:</p> <pre><code>import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JFrame; import javax.swing.JPanel; /** @see https://stackoverflow.com/a/5312702/230513 */ public class MouseDragTest extends JPanel { private static final String TITLE = "Drag me!"; private static final int W = 640; private static final int H = 480; private Point textPt = new Point(W / 2, H / 2); private Point mousePt; public MouseDragTest() { this.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 32)); this.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { mousePt = e.getPoint(); repaint(); } }); this.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { int dx = e.getX() - mousePt.x; int dy = e.getY() - mousePt.y; textPt.setLocation(textPt.x + dx, textPt.y + dy); mousePt = e.getPoint(); repaint(); } }); } @Override public Dimension getPreferredSize() { return new Dimension(W, H); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); int w2 = g.getFontMetrics().stringWidth(TITLE) / 2; g.drawString(TITLE, textPt.x - w2, textPt.y); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame f = new JFrame(TITLE); f.add(new MouseDragTest()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload