下面列出了java.awt.Window#getHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void pack
(Window
window) {
Dimension dim = window.getPreferredSize();
int prefw = dim.width;
int w = window.getWidth();
if (w < prefw) {
w = prefw;
}
int prefh = dim.height;
int h = window.getHeight();
if (h < prefh) {
h = prefh;
}
window.setSize(w, h);
}
public static void centerScreen(Window f, int screen) {
GraphicsDevice[] allDevices = getEnv().getScreenDevices();
int topLeftX, topLeftY, screenX, screenY, windowPosX, windowPosY;
if (screen < allDevices.length && screen > -1) {
topLeftX = allDevices[screen].getDefaultConfiguration().getBounds().x;
topLeftY = allDevices[screen].getDefaultConfiguration().getBounds().y;
screenX = allDevices[screen].getDefaultConfiguration().getBounds().width;
screenY = allDevices[screen].getDefaultConfiguration().getBounds().height;
} else {
topLeftX = allDevices[0].getDefaultConfiguration().getBounds().x;
topLeftY = allDevices[0].getDefaultConfiguration().getBounds().y;
screenX = allDevices[0].getDefaultConfiguration().getBounds().width;
screenY = allDevices[0].getDefaultConfiguration().getBounds().height;
}
windowPosX = ((screenX - f.getWidth()) / 2) + topLeftX;
windowPosY = ((screenY - f.getHeight()) / 2) + topLeftY;
f.setLocation(windowPosX, windowPosY);
}
/**
* method to get to know whether the AbstractButton with the given key is on Screen
*
* @param dockableKey
* i18nKey of the wanted AbstractButton
* @return returns 0 if the AbstractButton is on the Screen, 1 if the AbstractButton is on
* Screen but the user can not see it with the current settings of the perspective and
* -1 if the AbstractButton is not on the Screen.
*/
public static int isButtonOnScreen(final String buttonKey) {
// find the Button and return -1 if we can not find it
Component onScreen;
try {
onScreen = BubbleWindow.findButton(buttonKey, RapidMinerGUI.getMainFrame());
} catch (NullPointerException e) {
return OBJECT_NOT_ON_SCREEN;
}
if (onScreen == null) {
return OBJECT_NOT_ON_SCREEN;
}
// detect whether the Button is viewable
int xposition = onScreen.getLocationOnScreen().x;
int yposition = onScreen.getLocationOnScreen().y;
int otherXposition = xposition + onScreen.getWidth();
int otherYposition = yposition + onScreen.getHeight();
Window frame = RapidMinerGUI.getMainFrame();
if (otherXposition <= frame.getWidth() && otherYposition <= frame.getHeight() && xposition > 0 && yposition > 0) {
return OBJECT_SHOWING_ON_SCREEN;
} else {
return OBJECT_NOT_SHOWING;
}
}
/** Centers the specified window on the screen. */
public static void center(final Window w) {
final Dimension screenSize = getScreenSize(w);
final int screenWidth = screenSize.width;
final int screenHeight = screenSize.height;
final int windowWidth = w.getWidth();
final int windowHeight = w.getHeight();
if (windowHeight > screenHeight) {
return;
}
if (windowWidth > screenWidth) {
return;
}
final int x = (screenWidth - windowWidth) / 2;
final int y = (screenHeight - windowHeight) / 2;
w.setLocation(x, y);
}
private static void testWindowBounds(final DisplayMode dm, final Window w) {
if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
System.err.println(" Wrong window bounds:" +
" Expected: width = " + dm.getWidth()
+ ", height = " + dm.getHeight() + " Actual: "
+ w.getSize());
passed = false;
}
}
private static void testWindowBounds(final DisplayMode dm, final Window w) {
if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
System.err.println(" Wrong window bounds:" +
" Expected: width = " + dm.getWidth()
+ ", height = " + dm.getHeight() + " Actual: "
+ w.getSize());
passed = false;
}
}
private static void testWindowBounds(final DisplayMode dm, final Window w) {
if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
System.err.println(" Wrong window bounds:" +
" Expected: width = " + dm.getWidth()
+ ", height = " + dm.getHeight() + " Actual: "
+ w.getSize());
passed = false;
}
}
private static void testWindowBounds(final DisplayMode dm, final Window w) {
if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
System.err.println(" Wrong window bounds:" +
" Expected: width = " + dm.getWidth()
+ ", height = " + dm.getHeight() + " Actual: "
+ w.getSize());
passed = false;
}
}
private static void testWindowBounds(final DisplayMode dm, final Window w) {
if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
System.err.println(" Wrong window bounds:" +
" Expected: width = " + dm.getWidth()
+ ", height = " + dm.getHeight() + " Actual: "
+ w.getSize());
passed = false;
}
}
private static void test(GraphicsConfiguration gc) throws AWTException {
final Window frame = new Frame(gc);
try {
frame.addMouseWheelListener(e -> {
wheelX = e.getXOnScreen();
wheelY = e.getYOnScreen();
done = true;
});
frame.setSize(300, 300);
frame.setVisible(true);
final Robot robot = new Robot();
robot.setAutoDelay(50);
robot.setAutoWaitForIdle(true);
mouseX = frame.getX() + frame.getWidth() / 2;
mouseY = frame.getY() + frame.getHeight() / 2;
robot.mouseMove(mouseX, mouseY);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseWheel(10);
validate();
} finally {
frame.dispose();
}
}
private static void testWindowBounds(final DisplayMode dm, final Window w) {
if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
System.err.println(" Wrong window bounds:" +
" Expected: width = " + dm.getWidth()
+ ", height = " + dm.getHeight() + " Actual: "
+ w.getSize());
passed = false;
}
}
private static void testWindowBounds(final DisplayMode dm, final Window w) {
if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
System.err.println(" Wrong window bounds:" +
" Expected: width = " + dm.getWidth()
+ ", height = " + dm.getHeight() + " Actual: "
+ w.getSize());
passed = false;
}
}
private static void testWindowBounds(final DisplayMode dm, final Window w) {
if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
System.err.println(" Wrong window bounds:" +
" Expected: width = " + dm.getWidth()
+ ", height = " + dm.getHeight() + " Actual: "
+ w.getSize());
passed = false;
}
}
/**
* v1.0.1:修复自定义拖拽区域BUG, 保存游标状态
*/
public void mouseMoved(MouseEvent e)
{
Window window = (Window)e.getSource();
int w = window.getWidth();
int h = window.getHeight();
Point point = e.getPoint();
JRootPane rootPane = LuckWindowUtil.getRootPane(window);
int cursor = 0;
if(rootPane != null && LuckWindowUtil.isResizable(window))
{
cursor = getCursor(w, h, point, rootPane.getInsets());
}
if(cursor != Cursor.DEFAULT_CURSOR)
{
window.setCursor(Cursor.getPredefinedCursor(cursor));
}
else
{
window.setCursor(lastCursor);
}
dragCursor = cursor;
}
public DownloadBlocksDialog(String text, Window parent) {
super(LSystem.applicationMain, "Download Bitcoin Blocks", Dialog.ModalityType.DOCUMENT_MODAL);
addWindowListener(HelperWindow.get());
setIconImage(UIRes.getIcon());
setResizable(false);
Dimension dim = new Dimension(parent.getWidth() - 150, parent.getHeight() - 100);
setPreferredSize(dim);
setSize(dim);
statusPanel = new StatusPanel();
setContentPane(statusPanel);
addWindowListener(new ApplicationWindowListener());
}
private static void testWindowBounds(final DisplayMode dm, final Window w) {
if (w.getWidth() != dm.getWidth() || w.getHeight() != dm.getHeight()) {
System.err.println(" Wrong window bounds:" +
" Expected: width = " + dm.getWidth()
+ ", height = " + dm.getHeight() + " Actual: "
+ w.getSize());
passed = false;
}
}
/**
* Shifts the focus in a certain direction.
*
* @param dx the amount to increment x.
* @param dy the amount to increment y.
* @param src the source to traverse from.
* @return true if another component requested the focus
* as a result of this method. This may return false if
* no suitable component was found to shift focus to.
* (If you press the right arrow key on the right-most
* component, for example.)
*/
public static boolean shiftFocus(int dx, int dy, Component src) {
if (dx == 0 && dy == 0) //this would result in an infinite loop
{
throw new IllegalArgumentException("dx (" + dx + ") and (" + dy + ") cannot both be zero");
}
Set<Component> focusableComponents = getFocusableComponents(src);
int x = src.getWidth() / 2;
int y = src.getHeight() / 2;
Window window = SwingUtilities.getWindowAncestor(src);
if (window == null) {
return false;
}
Point p = SwingUtilities.convertPoint(src, x, y, window);
Component comp = null;
int windowWidth = window.getWidth();
int windowHeight = window.getHeight();
while (p.x > 0 && p.x < windowWidth && p.y > 0 && p.y < windowHeight && (comp == null || comp == src || (comp instanceof JPanel))) {
p.x += dx;
p.y += dy;
comp = SwingUtilities.getDeepestComponentAt(window, p.x, p.y);
boolean canAcceptFocus = focusableComponents.contains(comp);
if (comp != null && !canAcceptFocus) {
comp = null;
}
}
//TODO: implement a more robust searching mechanism instead of the above
//If a component is below the src, but to the left or right of the center:
//it should still be detected when you press the down arrow key.
if (comp != null && comp != src && comp != window && !(comp instanceof JPanel)) {
comp.requestFocus();
return true;
}
return false;
}
/**
* Return the first component available from a collection of candidates that
* is generally in the direction provided.
*/
public static Component getComponent(int dx, int dy, Component src,
Collection<Component> candidates) {
if (dx == 0 && dy == 0) // this would result in an infinite loop
throw new IllegalArgumentException("dx (" + dx + ") and (" + dy
+ ") cannot both be zero");
int x = src.getWidth() / 2;
int y = src.getHeight() / 2;
Window window = SwingUtilities.getWindowAncestor(src);
if (window == null)
return null;
Point p = SwingUtilities.convertPoint(src, x, y, window);
Component comp = null;
int windowWidth = window.getWidth();
int windowHeight = window.getHeight();
Map<Component, Rectangle> candidateWindowBounds = new HashMap<>();
for (Component candidate : candidates) {
Point z = SwingUtilities.convertPoint(candidate, new Point(0, 0),
window);
Rectangle r = new Rectangle(z.x, z.y, candidate.getWidth(),
candidate.getHeight());
candidateWindowBounds.put(candidate, r);
}
while (p.x > 0 && p.x < windowWidth && p.y > 0 && p.y < windowHeight
&& (comp == null || comp == src || (comp instanceof JPanel))) {
p.x += dx;
p.y += dy;
// this mostly works, but it fails when, say, an overlay tooltip is
// covering up what we want to select:
// comp = SwingUtilities.getDeepestComponentAt(window, p.x, p.y);
comp = null;
for (Entry<Component, Rectangle> entry : candidateWindowBounds
.entrySet()) {
if (entry.getValue().contains(p)) {
comp = entry.getKey();
break;
}
}
boolean canAcceptFocus = candidates.contains(comp);
if (comp != null && canAcceptFocus == false)
comp = null;
}
// TODO: implement a more robust searching mechanism instead of the
// above. If a component is below the src, but to the left or right of
// the center: it should still be detected when you press the down arrow
// key.
return comp;
}
/**
* v1.0.1:修复自定义拖拽区域BUG, 增加边界判断
*/
public void mousePressed(MouseEvent e)
{
Window window = (Window) e.getSource();
JRootPane root = LuckWindowUtil.getRootPane(window);
// 不包含窗体装饰直接返回
if (root == null || root.getWindowDecorationStyle() == JRootPane.NONE)
{
return;
}
if (window != null)
{
window.toFront();
}
// 如果是单击标题栏, 则标记接下来的拖动事件为移动窗口, 判断当前鼠标是否超出边界
if (dragArea.contains(e.getPoint())
&& dragCursor == Cursor.DEFAULT_CURSOR)
{
if(window instanceof JFrame)
{
JFrame frame = (JFrame)window;
// 如果当前窗体是全屏状态则直接返回
if(frame.getExtendedState() == JFrame.MAXIMIZED_BOTH)
{
return;
}
}
// 设置为可以移动并记录当前坐标
isMovingWindow = true;
dragOffsetX = e.getPoint().x;
dragOffsetY = e.getPoint().y;
}
else if(LuckWindowUtil.isResizable(window))
{
dragOffsetX = e.getPoint().x;
dragOffsetY = e.getPoint().y;
dragWidth = window.getWidth();
dragHeight = window.getHeight();
JRootPane rootPane = LuckWindowUtil.getRootPane(window);
if(rootPane != null && LuckWindowUtil.isResizable(window))
{
dragCursor = getCursor(dragWidth, dragHeight, e.getPoint(), rootPane.getInsets());
}
}
}
/**
* Check if there is enough space for the Window
*
* @param w
* The window to insert
* @param yOffset
* The start offset of the Window
* @return true if it fits on the screen
*/
private boolean fitsScreen(Window w, int yOffset) {
return yOffset + w.getHeight() <= parent.getHeight() && parent.getWidth() >= w.getWidth() + DEFAULT_RIGHT_MARGIN;
}