あるjLabelが別のjLabelの上にあるかどうかを読みたいです。jLabel1 を jLabel2 の上にドラッグしてそこにドロップし、それが上にある場合は何かを行います。コード:
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 という名前の別のラベルにドロップすると、何もしません。ドラッグアンドドロップすると問題ないことがわかりましたが、シアンブロックにマウスを置いて入力した場合にのみ機能します。青いボックスがマウスの代わりにシアンの上にあるときにそれを行うことは可能ですか?