Java 如何将多个鼠标侦听器添加到单个JFrame?
我有两件事要做:鼠标悬停时突出显示一个JPanel,鼠标拖动时移动一个蓝色正方形。问题是,这需要我向不同的组件添加鼠标侦听器。当我这样做时,我只能使用一个功能-另一个被阻止。如何才能使这两个功能都工作 注意:有时JFrame不会显示任何内容-您只需一直运行它,直到它显示为止(通常需要2-3次尝试)。如果它做了任何其他奇怪的事情,只要继续运行它,直到它的工作。如果在超过5次尝试后仍无法正常工作,请发表评论 它应该是什么样子的: Main(创建JFrame、容器和子对象,并添加鼠标侦听器) 可移动对象(创建可移动对象) ,或使用其他图像。请记住将下面图像的名称更改为您的名称Java 如何将多个鼠标侦听器添加到单个JFrame?,java,swing,awt,mouseevent,mouselistener,Java,Swing,Awt,Mouseevent,Mouselistener,我有两件事要做:鼠标悬停时突出显示一个JPanel,鼠标拖动时移动一个蓝色正方形。问题是,这需要我向不同的组件添加鼠标侦听器。当我这样做时,我只能使用一个功能-另一个被阻止。如何才能使这两个功能都工作 注意:有时JFrame不会显示任何内容-您只需一直运行它,直到它显示为止(通常需要2-3次尝试)。如果它做了任何其他奇怪的事情,只要继续运行它,直到它的工作。如果在超过5次尝试后仍无法正常工作,请发表评论 它应该是什么样子的: Main(创建JFrame、容器和子对象,并添加鼠标侦听器) 可移动对
public class MovableObject {
public MovableObject() {
JPanel parent = (JPanel) Main.container.findComponentAt(10, 10);
ImageIcon icon = null;
//use any image you might have laying around your workspace - or download the one above
URL imgURL = MovableObject.class.getResource("/blueSquare.png");
if (imgURL != null){
icon = new ImageIcon(imgURL);
}else{
System.err.println("Couldn't find file");
}
JLabel movableObject = new JLabel(icon);
parent.add(movableObject);
parent.revalidate();
}
}
MovableObjectMouseListener(创建用于MovableObject的MouseListener)
没有详细查看您的代码,但我建议您需要将拖动鼠标侦听器添加到可移动对象(即JLabel),而不是面板。然后,标签将获得拖动事件,面板将获得鼠标插入/退出事件
这将导致当您移动到标签上时(您不希望发生这种情况)生成mouseExited事件的另一个问题。签出:获取此问题的解决方案。没有详细查看您的代码,但我建议您需要将拖动鼠标侦听器添加到可移动对象(即JLabel),而不是面板。然后,标签将获得拖动事件,面板将获得鼠标插入/退出事件
这将导致当您移动到标签上时(您不希望发生这种情况)生成mouseExited事件的另一个问题。签出:查看此问题的解决方案。
有时JFrame不显示任何内容
-应在框架可见之前将所有组件添加到框架。有时JFrame不显示任何内容
-应在框架可见之前将所有组件添加到框架中。
public class HighlightJPanelsMouseListener implements MouseListener{
private Border grayBorder = BorderFactory.createLineBorder(Color.DARK_GRAY);
public HighlightJPanelsMouseListener() {
}
public void mouseEntered(MouseEvent e) {
Component comp = (Component) e.getSource();
JPanel parent = (JPanel) comp;
parent.setBorder(grayBorder);
parent.revalidate();
}
public void mouseExited(MouseEvent e) {
Component comp = (Component) e.getSource();
JPanel parent = (JPanel) comp;
parent.setBorder(null);
parent.revalidate();
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e) {}
}
public class MovableObject {
public MovableObject() {
JPanel parent = (JPanel) Main.container.findComponentAt(10, 10);
ImageIcon icon = null;
//use any image you might have laying around your workspace - or download the one above
URL imgURL = MovableObject.class.getResource("/blueSquare.png");
if (imgURL != null){
icon = new ImageIcon(imgURL);
}else{
System.err.println("Couldn't find file");
}
JLabel movableObject = new JLabel(icon);
parent.add(movableObject);
parent.revalidate();
}
}
public class MovableObjectMouseListener implements MouseListener, MouseMotionListener{
private JLabel replacement;
private int xAdjustment, yAdjustment;
public void mousePressed(MouseEvent e){
replacement = null;
Component c = Main.container.findComponentAt(e.getX(), e.getY());
if (!(c instanceof JLabel)){
return;
}
Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
replacement = (JLabel)c;
replacement.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
Main.layeredPane.add(replacement, JLayeredPane.DRAG_LAYER);
Main.layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
public void mouseDragged(MouseEvent me){
if (replacement == null){
return;
}
int x = me.getX() + xAdjustment;
int xMax = Main.layeredPane.getWidth() - replacement.getWidth();
x = Math.min(x, xMax);
x = Math.max(x, 0);
int y = me.getY() + yAdjustment;
int yMax = Main.layeredPane.getHeight() - replacement.getHeight();
y = Math.min(y, yMax);
y = Math.max(y, 0);
replacement.setLocation(x, y);
}
public void mouseReleased(MouseEvent e){
Main.layeredPane.setCursor(null);
if (replacement == null){
return;
}
int xMax = Main.layeredPane.getWidth() - replacement.getWidth();
int x = Math.min(e.getX(), xMax);
x = Math.max(x, 0);
int yMax = Main.layeredPane.getHeight() - replacement.getHeight();
int y = Math.min(e.getY(), yMax);
y = Math.max(y, 0);
Component c = Main.container.findComponentAt(x, y);
Container parent = (Container) c;
parent.add(replacement);
parent.validate();
}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}