2013-09-10 35 views
1

如果一個jLabel在另一個之上,我想閱讀。我將jLabel1拖放到jLabel2的頂部,然後將它放在那裏,如果它位於頂部,則執行一些操作。代碼:Java jLabel在另一個聽衆之上

package javaapplication5; 

import java.awt.Component; 
import java.awt.Point; 

public class NewJFrame extends javax.swing.JFrame { 
    boolean OptionsDrag = false, OptionsDrop = false; 
    private Point initialLoc; 
    private Point initialLocOnScreen; 
    /** 
    * Creates new form NewJFrame 
    */ 
    public NewJFrame() { 
     initComponents(); 
    } 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jLabel3 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setMaximumSize(new java.awt.Dimension(400, 300)); 
     setMinimumSize(new java.awt.Dimension(400, 300)); 
     getContentPane().setLayout(null); 

     jLabel1.setBackground(new java.awt.Color(0, 0, 255)); 
     jLabel1.setOpaque(true); 
     jLabel1.addMouseListener(new java.awt.event.MouseAdapter() { 
     public void mousePressed(java.awt.event.MouseEvent evt) { 
      jLabel1MousePressed(evt); 
     } 
     public void mouseReleased(java.awt.event.MouseEvent evt) { 
      jLabel1MouseReleased(evt); 
     } 
    }); 
    jLabel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { 
     public void mouseDragged(java.awt.event.MouseEvent evt) { 
      jLabel1MouseDragged(evt); 
     } 
    }); 
    getContentPane().add(jLabel1); 
    jLabel1.setBounds(90, 130, 48, 48); 

    jLabel2.setBackground(new java.awt.Color(0, 255, 255)); 
    jLabel2.setOpaque(true); 
    jLabel2.addMouseListener(new java.awt.event.MouseAdapter() { 
     public void mouseEntered(java.awt.event.MouseEvent evt) { 
      jLabel2MouseEntered(evt); 
     } 
    }); 
    getContentPane().add(jLabel2); 
    jLabel2.setBounds(230, 80, 48, 48); 

    jLabel3.setText("showed"); 
    jLabel3.setVisible(false); 
    getContentPane().add(jLabel3); 
    jLabel3.setBounds(40, 30, 37, 20); 

    pack(); 
}// </editor-fold>       

private void jLabel1MousePressed(java.awt.event.MouseEvent evt) {          
    Component comp = (Component)evt.getSource(); 
    initialLoc = comp.getLocation(); 
    initialLocOnScreen = evt.getLocationOnScreen(); 
}          

private void jLabel1MouseReleased(java.awt.event.MouseEvent evt) {          
    Component comp = (Component)evt.getSource(); 
    Point locOnScreen = evt.getLocationOnScreen(); 

    int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x; 
    int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y; 
    comp.setLocation(x, y); 
    OptionsDrop = true; 
}          

private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) {          
    Component comp = (Component)evt.getSource(); 
    Point locOnScreen = evt.getLocationOnScreen(); 

    int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x; 
    int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y; 
    comp.setLocation(x, y); 
    OptionsDrag = true; 
}          

private void jLabel2MouseEntered(java.awt.event.MouseEvent evt) {          
    if(OptionsDrag == true && OptionsDrop == true){ 
     jLabel3.setVisible(true); 
    } 
}          

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 

    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.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    // End of variables declaration     
} 

我可以移動jLabel1,但是當它放在名爲Preview_1的其他標籤上時,它什麼都不做。 我看到,當我拖放它很好,但它只有當我用青色塊上的鼠標進入時才起作用。當藍色盒子變成青色而不是鼠標時,可以這樣做嗎?

+0

正如前面已經提到您,請*** ***創建和發佈的[SSCCE(http://sscce.org),所以你不」迫使我們做出一個。 –

+0

Ok編輯。我把代碼鏈接到應用程序。 –

+0

請重新閱讀鏈接:[sscce](http://sscce.org)。是的,我要求你們付出一些努力來創造這一點,但它只不過是你已經要求我們做的。努力的責任應該主要是你的,因爲這是你的問題。 –

回答

2
  • 我首先要重命名變量,以便它們使代碼自行評論。是的,這是一個簡單的示例程序,但這個小小的改變可以使所有的不同。

例如:

private javax.swing.JLabel mobileLabel; 
private javax.swing.JLabel fixedLabel; 
private javax.swing.JLabel notificationLabel; 
  • 我不會一個的MouseListener添加到fixedLabel,因爲你真的不關心,如果鼠標是超過它,而如果拖組件是。
  • 相交測試應該在mobileLabel的MouseListener中。
  • 測試交集的一種方法是提取兩個組件的Rectangles(或者如果您必須遍歷多個組件,則需要循環使用for循環,並隨時提取Rectangles),然後測試一個Rectangle是否與另一個Rectangle相交。
  • 我不確定是否要將mobileLabel居中在固定標籤上,如果有重疊的話。如果是這樣,你可以得到固定位置,並用它來設置移動位置。
  • 另一種可能的解決方案是使用Java的拖放功能。

例如:

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Point; 
import java.awt.Rectangle; 

@SuppressWarnings("serial") 
public class NewJFrame2 extends javax.swing.JFrame { 
    private static final String OVERLAID = "Overlaid!"; 
    boolean OptionsDrag = false, OptionsDrop = false; 
    private Point initialLoc; 
    private Point initialLocOnScreen; 

    public NewJFrame2() { 
     initComponents(); 
    } 

    private void initComponents() { 
     mobileLabel = new javax.swing.JLabel("Mobile"); 
     mobileLabel.setForeground(Color.white); 
     fixedLabel = new javax.swing.JLabel("Fixed"); 
     notificationLabel = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setMaximumSize(new java.awt.Dimension(400, 300)); 
     setMinimumSize(new java.awt.Dimension(400, 300)); 
     getContentPane().setLayout(null); 

     mobileLabel.setBackground(new java.awt.Color(0, 0, 255)); 
     mobileLabel.setOpaque(true); 
     mobileLabel.addMouseListener(new java.awt.event.MouseAdapter() { 
     public void mousePressed(java.awt.event.MouseEvent evt) { 
      jLabel1MousePressed(evt); 
     } 

     public void mouseReleased(java.awt.event.MouseEvent evt) { 
      jLabel1MouseReleased(evt); 
     } 
     }); 
     mobileLabel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { 
     public void mouseDragged(java.awt.event.MouseEvent evt) { 
      jLabel1MouseDragged(evt); 
     } 
     }); 
     getContentPane().add(mobileLabel); 
     mobileLabel.setBounds(90, 130, 48, 48); 

     fixedLabel.setBackground(new java.awt.Color(0, 255, 255)); 
     fixedLabel.setOpaque(true); 
// !!  fixedLabel.addMouseListener(new java.awt.event.MouseAdapter() { 
//   public void mouseEntered(java.awt.event.MouseEvent evt) { 
//   jLabel2MouseEntered(evt); 
//   } 
//  }); 
     getContentPane().add(fixedLabel); 
     fixedLabel.setBounds(230, 80, 48, 48); 

     //!! notificationLabel.setText("showed"); 
     // !! notificationLabel.setVisible(false); 
     getContentPane().add(notificationLabel); 
     // notificationLabel.setBounds(40, 30, 37, 20); //!! 
     notificationLabel.setLocation(40, 30); //!! 
     notificationLabel.setSize(notificationLabel.getPreferredSize()); //!! 

     pack(); 
    } 

    private void jLabel1MousePressed(java.awt.event.MouseEvent evt) { 
     Component comp = (Component) evt.getSource(); 
     initialLoc = comp.getLocation(); 
     initialLocOnScreen = evt.getLocationOnScreen(); 
    } 

    private void jLabel1MouseReleased(java.awt.event.MouseEvent evt) { 
     Component comp = (Component) evt.getSource(); 
     Point locOnScreen = evt.getLocationOnScreen(); 

     int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x; 
     int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y; 
     comp.setLocation(x, y); 
     OptionsDrop = true; 

     if (compIntersectsFixed(comp)) { 

     // if you want to place the mobileLabel directly on top 
     // of the fixedLabel 
     comp.setLocation(fixedLabel.getLocation()); 
     repaint(); 
     notificationLabel.setText(OVERLAID); 
     notificationLabel.setSize(notificationLabel.getPreferredSize()); 
     } else { 
     notificationLabel.setText(""); 
     } 
     revalidate(); 
     repaint(); 
    } 

    private boolean compIntersectsFixed(Component comp) { 
     Rectangle compRect = comp.getBounds(); 
     Rectangle fixedRect = fixedLabel.getBounds(); 
     if (compRect.intersects(fixedRect)) { 
     return true; 
     } 
     return false; 
    } 

    private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) { 
     Component comp = (Component) evt.getSource(); 
     Point locOnScreen = evt.getLocationOnScreen(); 

     int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x; 
     int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y; 
     comp.setLocation(x, y); 
     OptionsDrag = true; 
    } 

// !! private void jLabel2MouseEntered(java.awt.event.MouseEvent evt) { 
//  if (OptionsDrag == true && OptionsDrop == true) { 
//   notificationLabel.setVisible(true); 
//  } 
// } 

    public static void main(String args[]) { 

     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(NewJFrame2.class.getName()).log(
       java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(
       java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(
       java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(
       java.util.logging.Level.SEVERE, null, ex); 
     } 

     java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      new NewJFrame2().setVisible(true); 
     } 
     }); 
    } 

    // !! name changes 
    private javax.swing.JLabel mobileLabel; 
    private javax.swing.JLabel fixedLabel; 
    private javax.swing.JLabel notificationLabel; 
}