下面列出了java.awt.Window#setLocationRelativeTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.requestFocus();
final ExtendedRobot robot = new ExtendedRobot();
robot.setAutoDelay(200);
robot.waitForIdle(1000);
window.setBackground(Color.GREEN);
robot.waitForIdle(1000);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
/**
* Resizes a window by setting its bounds to maximum that fits inside the default screen having the specified margin around it, and centers the window on
* the screen.
*
* <p>
* The implementation takes the screen insets (for example space occupied by task bar) into account.
* </p>
*
* @param window window to be resized
* @param margin margin to leave around the window
* @param maxSize optional parameter defining a maximum size
*/
public static void maximizeWindowWithMargin( final Window window, final int margin, final Dimension maxSize ) {
// Maybe use window.getGraphicsConfiguration() (it probably accounts multi-screens!)
// Edit: it does, but setLocationRelativeTo() at the end will always use the default screen device!
GraphicsConfiguration gconfig = window.getGraphicsConfiguration();
if ( gconfig == null )
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
final Rectangle bounds = gconfig.getBounds();
final Insets insets = Toolkit.getDefaultToolkit().getScreenInsets( gconfig );
final int width = bounds.width - insets.left - insets.right - margin * 2;
final int height = bounds.height - insets.top - insets.bottom - margin * 2;
if ( maxSize == null )
window.setSize( width, height );
else
window.setSize( Math.min( width, maxSize.width ), Math.min( height, maxSize.height ) );
// And finally center on screen
// window.setLocationRelativeTo( null ) always centers on the main screen, so:
window.setLocationRelativeTo( window.getOwner() );
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
/**
* {@inheritDoc}
*/
@Override
public void actionPerformed(ActionEvent e) {
Window dlg = getTable().getEditor(getRow());
if (dlg != null) {
if (dlg instanceof Frame) {
((Frame) dlg).setState(Frame.NORMAL);
((Frame) dlg).requestFocus();
}
dlg.setLocationRelativeTo(null);
dlg.setAlwaysOnTop(true);
dlg.setVisible(true);
}
}
public static void main(final String[] args) throws AWTException {
final boolean dump = Boolean.parseBoolean(args[0]);
final Window w = new Frame() {
@Override
public void list(final PrintStream out, final int indent) {
super.list(out, indent);
dumped = true;
}
};
w.setSize(200, 200);
w.setLocationRelativeTo(null);
w.setVisible(true);
final Robot robot = new Robot();
robot.setAutoDelay(50);
robot.setAutoWaitForIdle(true);
robot.mouseMove(w.getX() + w.getWidth() / 2,
w.getY() + w.getHeight() / 2);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_F1);
robot.keyRelease(KeyEvent.VK_F1);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_CONTROL);
w.dispose();
if (dumped != dump) {
throw new RuntimeException("Exp:" + dump + ", actual:" + dumped);
}
}
/**
* Given a window object, and a proposed x and y position on the screen, perform any necessary
* correction to force it to remain on at least one of the available display devices. If the top
* right corner of the window is not within any display device, it will be moved to the default
* location.
*
* @param w
* A Window that is moving, or about to be moved.
* @param x
* The relative horizontal position to the display origin
* @param y
* The relative vertical position to the display origin
*/
public static void correctPosition(Window w, int x, int y, ConfigManager pm) {
boolean isMaximizedEither = false;
if (w != null && Frame.class.isInstance(w)) {
int state = ((Frame) w).getExtendedState();
boolean maximizedHoriz = (state & JFrame.MAXIMIZED_HORIZ) != 0;
boolean maximizedVert = (state & JFrame.MAXIMIZED_VERT) != 0;
isMaximizedEither = maximizedHoriz || maximizedVert;
}
if (pm.getBooleanValue(KEY_WINDOW_SAFETY_ENABLED, true) && !isLocationInScreenBounds(x, y)
&& !isMaximizedEither) {
w.setLocationRelativeTo(null);
}
}
/**
* Checks that the position is in bounds for the screen and places it in
* visible area if not
*
* @param w
*/
public static void checkPositionInBounds(Window w) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point location = w.getLocationOnScreen();
// if this would appear offscreen, simply place it in the center of the screen
if (screenSize.getWidth() < location.x || screenSize.getHeight() < location.y) {
w.setLocationRelativeTo(null);
}
}
public static void main(String[] args) {
try {
Robot robot = Util.createRobot();
robot.setAutoDelay(50);
embeddedFrame = createEmbeddedFrame();
textField = new TextField("");
window = new Window(embeddedFrame);
window.setSize(200, 200);
window.setLocationRelativeTo(null);
window.add(textField);
window.setVisible(true);
Util.waitForIdle(robot);
Util.clickOnComp(textField, robot);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
Util.waitForIdle(robot);
if ("".equals(textField.getText())) {
throw new RuntimeException("Keyboard input in text field isn't possible");
}
} finally {
if (embeddedFrame != null) {
embeddedFrame.dispose();
}
if (window != null) {
window.dispose();
}
}
}
public static void main(String[] args) {
try {
Robot robot = Util.createRobot();
robot.setAutoDelay(50);
embeddedFrame = createEmbeddedFrame();
textField = new TextField("");
window = new Window(embeddedFrame);
window.setSize(200, 200);
window.setLocationRelativeTo(null);
window.add(textField);
window.setVisible(true);
Util.waitForIdle(robot);
Util.clickOnComp(textField, robot);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
Util.waitForIdle(robot);
if ("".equals(textField.getText())) {
throw new RuntimeException("Keyboard input in text field isn't possible");
}
} finally {
if (embeddedFrame != null) {
embeddedFrame.dispose();
}
if (window != null) {
window.dispose();
}
}
}
public static void main(String[] args) {
try {
Robot robot = Util.createRobot();
robot.setAutoDelay(50);
embeddedFrame = createEmbeddedFrame();
textField = new TextField("");
window = new Window(embeddedFrame);
window.setSize(200, 200);
window.setLocationRelativeTo(null);
window.add(textField);
window.setVisible(true);
Util.waitForIdle(robot);
Util.clickOnComp(textField, robot);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
Util.waitForIdle(robot);
if ("".equals(textField.getText())) {
throw new RuntimeException("Keyboard input in text field isn't possible");
}
} finally {
if (embeddedFrame != null) {
embeddedFrame.dispose();
}
if (window != null) {
window.dispose();
}
}
}
public void setLocationRelativeToMainFrame(Window window) {
window.setLocationRelativeTo(mainFrame);
}
public static final void centerOnScreen(final Window frame) {
frame.setLocationRelativeTo(null);
}
/**
* Centers a window on its screen.
* @param window window to be centered
*/
public static void centerWindow( final Window window ) {
window.setLocationRelativeTo( null );
}