だから私はここでこのコードを見つけました、それは機能します、それはただかなり多くのジャダリングを引き起こします
それなら、うまくいかないと思います。また、投稿を注意深く読むと、OPはそれが機能しないことも示しています。
さらに、ボタンは厳密にはマウスに追従しないので、私の意見では恐ろしいだけです。
ここで、考慮すべき2つのことがあります。
- 使用している
evt.getPoint()
:そのメソッドの値は。に相対的JButton
です。移動するときJButton
に、そのメソッドの値を前のメソッドと比較することはできません(ボタンが移動しているため)。簡単な解決策の1つは、これらのポイントを、移動していない親などの固定パネルに相対的に変換することです。Tadaam:スムーズに動作し、ボタンはマウスに完全に追従します。
- LayoutManagerを使用する場合、これはLayoutManagerの仕事であるため、呼び出すことはできません
setLocation
(setBoundsまたはsetSize()も)。コンテナが再レイアウトされるとすぐに、ボタンは元の場所に戻されます(私はあなたが望んでいないと思います)。これを解決する方法はいくつかありますが、通常、最も簡単な方法は絶対配置を使用することです(つまり、レイアウトをに設定します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
}