所以我将创建一个拖放程序,但在开始我的主要想法之前,我一直在练习我的代码。目前我只想在屏幕上移动 jlabel (包含图像)。我在常规 JAVA 文档中使用了此功能,但无法让代码在 netbeans 设计工具下正常工作(尝试让它以这种方式工作,因为我喜欢 GUI 编辑器)。
现在我遇到的问题是,虽然我可以移动图像,但它远非平滑,它非常容易跳跃,并且严重偏离实际的诅咒。我怎样才能解决这个问题?我真的希望这段代码能够通过设计工具工作,下面是我的错误代码:
package moodrag;
public class noLag extends javax.swing.JFrame {
int mouseX , mouseY ;
boolean mouseD;
public noLag() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ball = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(0, 153, 153));
ball.setIcon(new javax.swing.ImageIcon(getClass().getResource("/moodrag/ball.png"))); // NOI18N
ball.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
ballMouseDragged(evt);
}
public void mouseMoved(java.awt.event.MouseEvent evt) {
ballMouseMoved(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(ball)
.addContainerGap(70, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(ball)
.addContainerGap(61, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void ballMouseDragged(java.awt.event.MouseEvent evt) {
ball.setLocation(mouseX ,mouseY );
mouseX = evt.getX();
mouseY = evt.getY();
mouseD = true;
}
private void ballMouseMoved(java.awt.event.MouseEvent evt) {
}
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(noLag.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(noLag.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(noLag.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(noLag.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new noLag().setVisible(true);
}`enter code here`
});
}
// Variables declaration - do not modify
private javax.swing.JLabel ball;
// End of variables declaration
}
最佳答案
我立即想到了三个问题。
ball
所在的容器受布局管理器的控制,这意味着布局管理器可以选择随时重置组件的位置ballMouseDragged
中的逻辑似乎是倒退的。您将球
的位置设置为之前已知的鼠标位置,而不是当前位置MouseListener
附加到ball
。这意味着鼠标事件将与其上下文相关(即 0x0 将代表组件的左上角 (ball
))
例如:
关于JAVA拖放程序运行不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21319468/