ACM Java库(GraphicsProgram)中的getElementAt(double x,double y)等效于Java是什么? [英] What is java eqivalent for getElementAt(double x, double y) from acm java Libraries(GraphicsProgram);
本文介绍了ACM Java库(GraphicsProgram)中的getElementAt(double x,double y)等效于Java是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中,我尝试使用容器类中的getComponentAt(x,y)方法.但是对于给定的(x,y)坐标,即使(x,y)包含某些图像/JLabel/JButton,它也总是返回给我javax.swing.JRootPane类的对象.
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
推荐答案
查看全文