问题
in java i tried using getComponentAt(x, y) method from container class. but for given (x,y) co ordinates it is always returning me object of class javax.swing.JRootPane even if (x,y) contains some image/JLabel/JButton.
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class temp2 implements MouseListener{
public static void main(String[] arg){
frame = new JFrame("adsa");
frame.setBounds(0, 0, 1000, 1000);
frame.setVisible(true);
frame.addMouseListener(new temp2());
JButton l = new JButton("asdasd");
frame.add(l);
l.addMouseListener(new temp2());;
}
static JFrame frame;
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println(frame.getComponentAt(x, y).getClass());
System.out.println(frame.getComponentAt(x, y) instanceof JButton);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
this is the sample code i used for testing.. whenever i click on button i am getting following output:
class javax.swing.JRootPane
false
回答1:
you have to look for SwingUtilities#getDeepestComponentAt, example
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class TestMouseListener {
public static void main(String[] args) {
final JComboBox combo = new JComboBox();
combo.setEditable(true);
for (int i = 0; i < 10; i++) {
combo.addItem(i);
}
final JLabel tip = new JLabel();
tip.setText("Outside combobox");
JPanel panel = new JPanel();
panel.add(combo);
panel.add(tip);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
tip.setText("Outside combobox");
}
@Override
public void mouseExited(MouseEvent e) {
Component c = SwingUtilities.getDeepestComponentAt(
e.getComponent(), e.getX(), e.getY());
tip.setText(c != null && SwingUtilities.isDescendingFrom(
c, combo) ? "Inside combo box" : "Outside combobox");
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private TestMouseListener() {
}
}
回答2:
It depends on how you built your UI, but try Container#findCompoinentAt(int x, int y)
- it returns the top-most visible child.
回答3:
The problems come from the fact that you are adding your listener to several component but you always refer to the frame to retrieve the component at. What you should rather do, is:
Component c = ((Component)e.getSource()).getComponentAt(e.getX(), e.getY());
// Do some stuff about c.
Event coordinates are relative to the component that fired the event.
来源:https://stackoverflow.com/questions/9260919/what-is-java-eqivalent-for-getelementatdouble-x-double-y-from-acm-java-librar