java swing坐标系_Java Swing - 了解JViewport

你要找的方法是

getVisibleRect()

它在JComponent中定义,在您使用它的上下文中

table1.getVisibleRect().contains(rect)

编辑:刚才意识到你可能还在挠头 - 即使已经给出的所有答案在技术上都是正确的:-)

基本上,它都是关于坐标系,即相对于给定原点的位置 . 使用与位置相关的方法时,您必须知道该特定方法的坐标系,并且您不能混合使用不同的系统(至少在没有翻译的情况下) .

但混合你做了:

// cellRect is in table coordinates

Rectangle cellRect = table.getCellRect(...)

// WRONG!!! use table coordinates in parent sytem

table.getParent().contains(cellRect.getLocation());

解决方案是找到一个坐标系统,其中上面找到的单元格位置是有意义的(或手动将单元格位置转换为父系统,但这里不需要),有一些方法可以进行转换:

// returns the visible part of any component in its own coordinates

// available for all components

Rectangle visible = table.getVisibleRect();

// special service method in JViewport, returning the visible portion

// of its single child in the coordinates of the child

Rectangle viewRect = ((Viewport) (table.getParent()).getViewRect();

// both are the same

visible.equals(viewRect)

查询表本身(而不是查询其父级)是可取的,因为它不需要任何关于其父级的知识 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值