java getwidth_Java Window.getWidth方法代码示例

import java.awt.Window; //导入方法依赖的package包/类

private Point calculatePosition(Component source) {

int xSource = source.getLocationOnScreen().x;

int ySource = source.getLocationOnScreen().y;

// get size of popup

Dimension popupSize = ((Component) popupComponent).getSize();

if (popupSize.width == 0) {

popupSize = ((Component) popupComponent).getPreferredSize();

}

int xPopup = 0;

int yPopup = 0;

// get max x and y window positions

Window focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();

int maxX = focusedWindow.getLocationOnScreen().x + focusedWindow.getWidth();

int maxY = focusedWindow.getLocationOnScreen().y + focusedWindow.getHeight();

switch (position) {

case VERTICAL:

// place popup at sources' x position

xPopup = xSource;

// check if popup is outside active window

if (xPopup + popupSize.width > maxX) {

// move popup x position to the left

// to fit inside the active window

xPopup = maxX - popupSize.width - BORDER_OFFSET;

}

// place popup always below source (to avoid overlapping)

yPopup = ySource + source.getHeight();

// if the popup now would be moved outside of RM Studio to the left it would look

// silly, so in that case just show it at its intended position and let it be cut

// off on the right side as we cannot do anything about it

if (xPopup < focusedWindow.getLocationOnScreen().x

|| (xPopup - focusedWindow.getLocationOnScreen().x) + popupSize.width > focusedWindow.getWidth()) {

xPopup = xSource;

}

break;

case HORIZONTAL:

// place popup always to the right side of the source (to avoid overlapping)

xPopup = xSource + source.getWidth();

yPopup = ySource;

// check if popup is outside active window

if (yPopup + popupSize.height > maxY) {

// move popup upwards to fit into active window

yPopup = maxY - popupSize.height - BORDER_OFFSET;

}

// if the popup now would be moved outside of RM Studio at the top it would look

// silly, so in that case just show it at its intended position and let it be cut

// off on the bottom side as we cannot do anything about it

if (yPopup < focusedWindow.getLocationOnScreen().y

|| (yPopup - focusedWindow.getLocationOnScreen().y) + popupSize.height > focusedWindow.getHeight()) {

yPopup = ySource;

}

break;

}

return new Point(xPopup, yPopup);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值