JAVA拖放程序运行不顺畅

时间:2014-01-23 21:15:01

标签: java swing netbeans drag-and-drop

所以我将创建一个拖放程序,但在我开始我的主要想法之前,我一直在练习我的代码。目前我只是想在屏幕周围移动一个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                   
}

1 个答案:

答案 0 :(得分:3)

立即跳出了三个问题。

  1. ball所在的容器受布局管理器控制,这意味着布局管理器可以随时选择重置组件的位置
  2. ballMouseDragged中的逻辑似乎倒退了。您将ball的位置设置为之前已知的鼠标位置,而不是当前位置
  3. MouseListener附加到ball。这意味着鼠标事件将是它的上下文(即,0x0将代表组件的顶部/左角(ball))
  4. 例如: