1.鼠标位置找控件
Component econt=e.getComponent();
2.根据控件找窗口
Window wind=SwingUtilities.getWindowAncestor(btn_sub);
注:此系列用与个人学习总结,当遇到新代码,还会在此基础上更新
3.判断鼠标位置是否在图形里
Rectangle cell=new Rectangle (0,0,width,height);
/*得到的是一个boolean类型*/
cell.contains(e.getPoint())
4.获取当前窗口根容器(Container)
Container root=this.getContentPane();
5.根据JPanel获取Layout(CardLayout )
CardLayout caedly=(CardLayout)myPanel.getLayout();
//控制卡片布局的当前显示:caedly.show(myPanel, "Mybtn");
6.获取JList的当前鼠标点击项(int)
leftMenu.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e)
{
//获取鼠标点击位置的坐标,将其转换为JList的item
int index = leftMenu.locationToIndex(e.getPoint());
if(index >=0) selectView(index);
}
});
7.获取JComboBox的选中项(int)
int seleindx=combox.getSelectedIndex();
8.获取鼠标拖动的起点Point ()
Point point=dge.getDragOrigin();
9.由鼠标坐标获取JList所选中的项(int)
int index=source2.locationToIndex(point);
10.由项获取Jlist所选中的矩形(Rectangle )
Rectangle rect=source2.getCellBounds(index, index);
11.获取当前鼠标停留位置(Point)
java.awt.MouseInfo.getPointerInfo().getLocation();
12.鼠标位置(Point)
Point startMousePos = e.getLocationOnScreen();
13.窗口的位置和大小
Window w = getWindow();
Point startWinPos = w.getLocation();
Dimension startWinSize = w.getSize();