Java jLabel位于另一个侦听器之上
我想知道一个jLabel是否在另一个jLabel之上。我将jLabel1拖到jLabel2的顶部,然后将其放到那里,如果它在顶部,则执行一些操作。代码:Java jLabel位于另一个侦听器之上,java,swing,drag-and-drop,jlabel,mouse-listeners,Java,Swing,Drag And Drop,Jlabel,Mouse Listeners,我想知道一个jLabel是否在另一个jLabel之上。我将jLabel1拖到jLabel2的顶部,然后将其放到那里,如果它在顶部,则执行一些操作。代码: package javaapplication5; import java.awt.Component; import java.awt.Point; public class NewJFrame extends javax.swing.JFrame { boolean OptionsDrag = false, OptionsDro
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
}
PackageJavaApplication5;
导入java.awt.Component;
导入java.awt.Point;
公共类NewJFrame扩展了javax.swing.JFrame{
布尔选项Drag=false,选项Drop=false;
私人点;
屏幕上的私人点初始位置;
/**
*创建新表单NewJFrame
*/
公共NewJFrame(){
初始化组件();
}
@抑制警告(“未选中”)
//
私有组件(){
jLabel1=newjavax.swing.JLabel();
jLabel2=newjavax.swing.JLabel();
jLabel3=newjavax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMaximumSize(新java.awt.Dimension(400300));
setMinimumSize(新java.awt.Dimension(400300));
getContentPane().setLayout(null);
setBackground(新java.awt.Color(0,0255));
jLabel1.setOpaque(true);
jLabel1.addMouseListener(新java.awt.event.MouseAdapter(){
public void mousePressed(java.awt.event.MouseEvent evt){
jLabel1鼠标按下(evt);
}
public void mouseereleased(java.awt.event.MouseEvent evt){
jLabel1(evt);
}
});
jLabel1.addMouseMotionListener(新java.awt.event.MouseMotionAdapter(){
public void mouseDragged(java.awt.event.MouseEvent evt){
jLabel1鼠标标记(evt);
}
});
getContentPane().add(jLabel1);
jLabel1.立根(90,130,48,48);
setBackground(新java.awt.Color(0255255));
jLabel2.setOpaque(true);
jLabel2.addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseenterned(java.awt.event.MouseEvent evt){
jLabel2MouseEntered(evt);
}
});
getContentPane().add(jLabel2);
jLabel2.立根(230,80,48,48);
jLabel3.setText(“显示”);
jLabel3.setVisible(假);
getContentPane().add(jLabel3);
jLabel3.立根(40,30,37,20);
包装();
}//
私有void jLabelMousePressed(java.awt.event.MouseEvent evt){
组件comp=(组件)evt.getSource();
initialLoc=comp.getLocation();
InitialLocationScreen=evt.getLocationOnScreen();
}
私有void jLabelMouseReleased(java.awt.event.MouseEvent evt){
组件comp=(组件)evt.getSource();
Point LocationScreen=evt.getLocationOnScreen();
int x=locOnScreen.x-initialLocOnScreen.x+initialLoc.x;
int y=locOnScreen.y-initialLocOnScreen.y+initialLoc.y;
组件设置位置(x,y);
OptionsDrop=true;
}
私有void jlabelmousedragged(java.awt.event.MouseEvent evt){
组件comp=(组件)evt.getSource();
Point LocationScreen=evt.getLocationOnScreen();
int x=locOnScreen.x-initialLocOnScreen.x+initialLoc.x;
int y=locOnScreen.y-initialLocOnScreen.y+initialLoc.y;
组件设置位置(x,y);
选项拖动=真;
}
私有void jLabel2MouseEntered(java.awt.event.MouseEvent evt){
if(OptionsDrag==true&&OptionsDrop==true){
jLabel3.setVisible(真);
}
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
@凌驾
公开募捐{
新建NewJFrame().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JLabel jLabel1;
私有javax.swing.JLabel jLabel2;
私有javax.swing.JLabel jLabel3;
//变量结束声明
}
我可以移动jLabel1,但当它被放到另一个名为Preview_1的标签上时,它什么也不做。
我看到,当我拖放它的罚款,但它的工作只有当我进入青色块鼠标。当蓝色盒子放在青色的上面而不是鼠标上时,是否可以这样做?- 我首先要重命名变量,以便它们使代码能够自我注释。是的,这是一个简单的示例程序,但是这个小小的改变可以带来所有的不同李>
private javax.swing.JLabel mobileLabel;
private javax.swing.JLabel fixedLabel;
private javax.swing.JLabel notificationLabel;
- 我不会向fixedLabel添加鼠标侦听器,因为您并不真正关心鼠标是否在其上,而是关心拖动的组件是否在其上
- 相反,交叉测试应该在mobileLabel的鼠标侦听器中进行
- 测试相交的一种方法是提取两个组件的矩形(如果必须循环通过多个组件,则使用for循环,提取R)
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; }