所以我在这里找到了这段代码,它有效,只是引起了很多颤抖
然后我会说它不起作用。而且,如果您仔细阅读该帖子,则 OP 还声明它不起作用。
此外,按钮并没有严格跟随鼠标,所以在我看来这很可怕。
现在,需要考虑两件事:
- 您正在使用
evt.getPoint()
:该方法的值是相对于JButton
. 当您四处移动JButton
时,您无法将该方法的值与前一个方法的值进行比较(因为您的按钮正在移动)。一种简单的解决方案是将这些点相对于固定面板转换为固定面板,例如不移动的父面板。Tadaam:它现在运行流畅,按钮完美地跟随你的鼠标。
- 当您使用 LayoutManager 时,您不能调用
setLocation
(也不能调用 setBounds 或 setSize()),因为这是 LayoutManager 的工作,一旦他们重新布局您的容器,按钮就会被设置回其原始位置(即我猜你不想要)。有几种方法可以解决这个问题,但通常最简单的一种是使用绝对定位(即,将布局设置为null
)。最后,这意味着您必须执行 LayoutManager 之前所做的任何事情。
这是一个小演示(有缺陷,但演示了基本原理):
import java.awt.Component;
import java.awt.Point;
import javax.swing.SwingUtilities;
/**
*
* @author Stuart.Bradley
*/
public class NewJFrame extends javax.swing.JFrame {
private Point mouseLocation;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this
* method is always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton1MousePressed(evt);
}
});
jButton1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
jButton1MouseDragged(evt);
}
});
setLayout(null);
jButton1.setSize(jButton1.getPreferredSize());
add(jButton1);
setSize(300, 300);
}// </editor-fold>
private void jButton1MouseDragged(java.awt.event.MouseEvent evt) {
// current mouse location
Point newLoc = SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), jButton1.getParent());
// deltas
int deltaX = newLoc.x - mouseLocation.x;
int deltaY = newLoc.y - mouseLocation.y;
jButton1.setLocation(jButton1.getX() + deltaX, jButton1.getY() + deltaY);
// move the reference point to the current location
this.mouseLocation = newLoc; // TODO add your handling code here:
}
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
mouseLocation = SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), jButton1.getParent()); // TODO add your
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
// <editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// </editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}