1
拖動鼠標的對象開始,當我拖動鼠標在不同位置的對象開始,當我在不同位置
我想要開始兩個鼠標位置和對象位置ceter具有相同的位置 起動。
我嘗試了一些方法,並不能在谷歌找到解決方案。 所以我提出了問題。
package math;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class movingButton extends JFrame {
private JButton button;
public movingButton() {
super("Position helper");
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setSize(500, 520);
super.setVisible(true);
super.setLayout(null);
button = new JButton("drag me ");
add(button);
button.setBounds(100, 100, 150, 40);
button.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent E) {
int X = E.getX() + button.getX();
int Y = E.getY() + button.getY();
button.setBounds(X, Y, 150, 40);
}
});
}
}
public class Main {
public static void main(String args[]) {
new movingButton();
}
}
非常感謝你ArcticLord – user3662974