public Textures()
{
super(new GridBagLayout());
layout.add(l1);
layout.add(l2);
add(layout);
String[] T = filerw.fileRead2();
if(T[0].equals("11"))
{
rect1.setLocation(layout.getComponent(1).getBounds().getLocation());
}
if(T[0].equals("12"))
{
rect1.setLocation(layout.getComponent(2).getBounds().getLocation());
}
l1.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent arg0)
{
a = 11;
filerw.fileWrite();
rect1.setLocation(l1.getBounds().getLocation().x, l1.getBounds().getLocation().y);
}
});
l2.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent arg0)
{
a = 12;
filerw.fileWrite();
rect1.setLocation(l2.getBounds().getLocation().x, l2.getBounds().getLocation().y);
repaint();
}
});
我尝试了很多方法来确定组件的位置,但它们要么出错,要么显示为零,要么位置不正确。我尝试过 getX() 和 x、getBounds.getLocation、getLocation、getLocationOnScreen、layout.getComponent(1) 或 l1。如何找到组件的位置?我想得到一个矩形来包围图像,我该如何完成定位?