Java Code Examples for java.awt.Frame#setLocationRelativeTo()

The following examples show how to use java.awt.Frame#setLocationRelativeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NullActiveWindowOnFocusLost.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static Thread testThread(int index) {
    return new Thread(new ThreadGroup("TG " + index), () -> {
        SunToolkit.createNewAppContext();
        while (!isComplete()) {
            final Frame frame = new Frame();
            frame.setSize(300, 300);
            frame.add(new Button("Button"));
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            try {
                Thread.sleep(index); // increase probability of the failure
            } catch (InterruptedException ignored) {
            }
            frame.dispose();
        }
    });
}
 
Example 2
Source File: ScrollPanePreferredSize.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) {
    final Dimension expected = new Dimension(300, 300);
    final Frame frame = new Frame();
    final ScrollPane sp = new ScrollPane();
    sp.setSize(expected);
    frame.add(sp);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    sleep();
    final Dimension size = frame.getSize();
    if (size.width < expected.width || size.height < expected.height) {
        throw new RuntimeException(
                "Expected size: >= " + expected + ", actual size: " + size);
    }
    frame.dispose();
}
 
Example 3
Source File: NullActiveWindowOnFocusLost.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static Thread testThread(int index) {
    return new Thread(new ThreadGroup("TG " + index), () -> {
        SunToolkit.createNewAppContext();
        while (!isComplete()) {
            final Frame frame = new Frame();
            frame.setSize(300, 300);
            frame.add(new Button("Button"));
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            try {
                Thread.sleep(index); // increase probability of the failure
            } catch (InterruptedException ignored) {
            }
            frame.dispose();
        }
    });
}
 
Example 4
Source File: NullActiveWindowOnFocusLost.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static Thread testThread(int index) {
    return new Thread(new ThreadGroup("TG " + index), () -> {
        SunToolkit.createNewAppContext();
        while (!isComplete()) {
            final Frame frame = new Frame();
            frame.setSize(300, 300);
            frame.add(new Button("Button"));
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            try {
                Thread.sleep(index); // increase probability of the failure
            } catch (InterruptedException ignored) {
            }
            frame.dispose();
        }
    });
}
 
Example 5
Source File: ScrollPanePreferredSize.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) {
    final Dimension expected = new Dimension(300, 300);
    final Frame frame = new Frame();
    final ScrollPane sp = new ScrollPane();
    sp.setSize(expected);
    frame.add(sp);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    sleep();
    final Dimension size = frame.getSize();
    if (size.width < expected.width || size.height < expected.height) {
        throw new RuntimeException(
                "Expected size: >= " + expected + ", actual size: " + size);
    }
    frame.dispose();
}
 
Example 6
Source File: ScrollPanePreferredSize.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) {
    final Dimension expected = new Dimension(300, 300);
    final Frame frame = new Frame();
    final ScrollPane sp = new ScrollPane();
    sp.setSize(expected);
    frame.add(sp);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    sleep();
    final Dimension size = frame.getSize();
    if (size.width < expected.width || size.height < expected.height) {
        throw new RuntimeException(
                "Expected size: >= " + expected + ", actual size: " + size);
    }
    frame.dispose();
}
 
Example 7
Source File: FrameSetSizeStressTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) {
    final Frame frame = new Frame();
    frame.setSize(200, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    for (int i = 0; i < 1000; ++i) {
        frame.setSize(100, 100);
        frame.setSize(200, 200);
        frame.setSize(300, 300);
    }
    frame.dispose();
}
 
Example 8
Source File: LabelRepaint.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) {
    for (int i = 0; i < 10; ++i) {
        final Frame frame = new Frame();
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        LabelRepaint label = new LabelRepaint();
        frame.add(label);
        frame.setVisible(true);
        sleep();
        label.test();
        frame.dispose();
    }
}
 
Example 9
Source File: PaintNativeOnUpdate.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Frame frame = new Frame();
    final Component label = new PaintNativeOnUpdate();
    frame.setBackground(Color.RED);
    frame.add(label);
    frame.setSize(300, 300);
    frame.setUndecorated(true);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    sleep();
    label.repaint();// first paint
    sleep();
    label.repaint();// incremental paint
    sleep();

    Robot robot = new Robot();
    robot.setAutoDelay(50);
    Point point = label.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + label.getWidth() / 2,
                                      point.y + label.getHeight() / 2);
    if (!color.equals(Color.GREEN)) {
        System.err.println("Expected color = " + Color.GREEN);
        System.err.println("Actual color = " + color);
        throw new RuntimeException();
    }
    frame.dispose();
}
 
Example 10
Source File: LabelRepaint.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) {
    for (int i = 0; i < 10; ++i) {
        final Frame frame = new Frame();
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        LabelRepaint label = new LabelRepaint();
        frame.add(label);
        frame.setVisible(true);
        sleep();
        label.test();
        frame.dispose();
    }
}
 
Example 11
Source File: FrameSetSizeStressTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) {
    final Frame frame = new Frame();
    frame.setSize(200, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    for (int i = 0; i < 1000; ++i) {
        frame.setSize(100, 100);
        frame.setSize(200, 200);
        frame.setSize(300, 300);
    }
    frame.dispose();
}
 
Example 12
Source File: SharedMemoryPixmapsTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** Creates a new instance of SharedMemoryPixmapsTest */
public SharedMemoryPixmapsTest() {
    testFrame = new Frame("SharedMemoryPixmapsTest");
    testFrame.add(new TestComponent());
    testFrame.setUndecorated(true);
    testFrame.setResizable(false);
    testFrame.pack();
    testFrame.setLocationRelativeTo(null);
    testFrame.setVisible(true);
    testFrame.toFront();
}
 
Example 13
Source File: SharedMemoryPixmapsTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/** Creates a new instance of SharedMemoryPixmapsTest */
public SharedMemoryPixmapsTest() {
    testFrame = new Frame("SharedMemoryPixmapsTest");
    testFrame.add(new TestComponent());
    testFrame.setUndecorated(true);
    testFrame.setResizable(false);
    testFrame.pack();
    testFrame.setLocationRelativeTo(null);
    testFrame.setVisible(true);
    testFrame.toFront();
}
 
Example 14
Source File: ApplicationThreadsStop.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void run() {
    contextToDispose = SunToolkit.createNewAppContext();
    Frame f = new Frame();
    f.setSize(300, 300);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    thread = new Thread(() -> {
        while(true);
    });
    thread.start();
    sync();
}
 
Example 15
Source File: PaintNativeOnUpdate.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Frame frame = new Frame();
    final Component label = new PaintNativeOnUpdate();
    frame.setBackground(Color.RED);
    frame.add(label);
    frame.setSize(300, 300);
    frame.setUndecorated(true);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    sleep();
    label.repaint();// first paint
    sleep();
    label.repaint();// incremental paint
    sleep();

    Robot robot = new Robot();
    robot.setAutoDelay(50);
    Point point = label.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + label.getWidth() / 2,
                                      point.y + label.getHeight() / 2);
    if (!color.equals(Color.GREEN)) {
        System.err.println("Expected color = " + Color.GREEN);
        System.err.println("Actual color = " + color);
        throw new RuntimeException();
    }
    frame.dispose();
}
 
Example 16
Source File: SharedMemoryPixmapsTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/** Creates a new instance of SharedMemoryPixmapsTest */
public SharedMemoryPixmapsTest() {
    testFrame = new Frame("SharedMemoryPixmapsTest");
    testFrame.add(new TestComponent());
    testFrame.setUndecorated(true);
    testFrame.setResizable(false);
    testFrame.pack();
    testFrame.setLocationRelativeTo(null);
    testFrame.setVisible(true);
    testFrame.toFront();
}
 
Example 17
Source File: LabelRepaint.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) {
    for (int i = 0; i < 10; ++i) {
        final Frame frame = new Frame();
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        LabelRepaint label = new LabelRepaint();
        frame.add(label);
        frame.setVisible(true);
        sleep();
        label.test();
        frame.dispose();
    }
}
 
Example 18
Source File: NormalToIconifiedTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    Robot robot = Util.createRobot();

    Frame testFrame = new Frame("Test Frame");
    testFrame.setSize(200, 200);
    testFrame.addWindowStateListener(new WindowStateListener() {
        @Override
        public void windowStateChanged(WindowEvent e) {
            listenerNotified.set(true);
            synchronized (listenerNotified) {
                listenerNotified.notifyAll();
            }
        }
    });
    testFrame.setVisible(true);

    Frame mainFrame = new Frame("Main Frame");
    mainFrame.setSize(200, 200);
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setVisible(true);

    Util.waitForIdle(robot);

    try {
        Util.clickOnComp(mainFrame, robot);
        Util.waitForIdle(robot);

        // NORMAL -> ICONIFIED
        listenerNotified.set(false);
        testFrame.setExtendedState(Frame.ICONIFIED);
        Util.waitForIdle(robot);

        Util.waitForCondition(listenerNotified, 2000);
        if (!listenerNotified.get()) {
            throw new RuntimeException("Test FAILED! Window state listener was not notified during NORMAL to" +
                    "ICONIFIED transition");
        }
        if (testFrame.getExtendedState() != Frame.ICONIFIED) {
            throw new RuntimeException("Test FAILED! Frame is not in ICONIFIED state");
        }

        // ICONIFIED -> NORMAL
        listenerNotified.set(false);
        testFrame.setExtendedState(Frame.NORMAL);
        Util.waitForIdle(robot);

        Util.waitForCondition(listenerNotified, 2000);
        if (!listenerNotified.get()) {
            throw new RuntimeException("Test FAILED! Window state listener was not notified during ICONIFIED to" +
                    "NORMAL transition");
        }
        if (testFrame.getExtendedState() != Frame.NORMAL) {
            throw new RuntimeException("Test FAILED! Frame is not in NORMAL state");
        }
    } finally {
        testFrame.dispose();
        mainFrame.dispose();
    }
}
 
Example 19
Source File: NormalToIconifiedTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    Robot robot = Util.createRobot();

    Frame testFrame = new Frame("Test Frame");
    testFrame.setSize(200, 200);
    testFrame.addWindowStateListener(new WindowStateListener() {
        @Override
        public void windowStateChanged(WindowEvent e) {
            listenerNotified.set(true);
            synchronized (listenerNotified) {
                listenerNotified.notifyAll();
            }
        }
    });
    testFrame.setVisible(true);

    Frame mainFrame = new Frame("Main Frame");
    mainFrame.setSize(200, 200);
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setVisible(true);

    Util.waitForIdle(robot);

    try {
        Util.clickOnComp(mainFrame, robot);
        Util.waitForIdle(robot);

        // NORMAL -> ICONIFIED
        listenerNotified.set(false);
        testFrame.setExtendedState(Frame.ICONIFIED);
        Util.waitForIdle(robot);

        Util.waitForCondition(listenerNotified, 2000);
        if (!listenerNotified.get()) {
            throw new RuntimeException("Test FAILED! Window state listener was not notified during NORMAL to" +
                    "ICONIFIED transition");
        }
        if (testFrame.getExtendedState() != Frame.ICONIFIED) {
            throw new RuntimeException("Test FAILED! Frame is not in ICONIFIED state");
        }

        // ICONIFIED -> NORMAL
        listenerNotified.set(false);
        testFrame.setExtendedState(Frame.NORMAL);
        Util.waitForIdle(robot);

        Util.waitForCondition(listenerNotified, 2000);
        if (!listenerNotified.get()) {
            throw new RuntimeException("Test FAILED! Window state listener was not notified during ICONIFIED to" +
                    "NORMAL transition");
        }
        if (testFrame.getExtendedState() != Frame.NORMAL) {
            throw new RuntimeException("Test FAILED! Frame is not in NORMAL state");
        }
    } finally {
        testFrame.dispose();
        mainFrame.dispose();
    }
}
 
Example 20
Source File: Button2DragTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void run() {
    frame = new Frame();

    final DragSourceListener dragSourceListener = new DragSourceAdapter() {
        public void dragDropEnd(DragSourceDropEvent e) {
            dropSuccess = e.getDropSuccess();
            System.err.println("Drop was successful: " + dropSuccess);
        }
    };
    DragGestureListener dragGestureListener = new DragGestureListener() {
        public void dragGestureRecognized(DragGestureEvent dge) {
            dge.startDrag(null, new StringSelection("OK"), dragSourceListener);
        }
    };
    new DragSource().createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_MOVE,
                                                        dragGestureListener);

    DropTargetAdapter dropTargetListener = new DropTargetAdapter() {
        public void drop(DropTargetDropEvent dtde) {
            dtde.acceptDrop(DnDConstants.ACTION_MOVE);
            dtde.dropComplete(true);
            System.err.println("Drop");
        }
    };
    new DropTarget(frame, dropTargetListener);

    //What would normally go into main() will probably go here.
    //Use System.out.println for diagnostic messages that you want
    //to read after the test is done.
    frame.setUndecorated(true);
    frame.setBounds(100, 100, 200, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    Robot robot = Util.createRobot();

    Util.waitForIdle(robot);

    Point startPoint = frame.getLocationOnScreen();
    Point endPoint = new Point(startPoint);
    startPoint.translate(50, 50);
    endPoint.translate(150, 150);

    Util.drag(robot, startPoint, endPoint, InputEvent.BUTTON2_MASK);

    Util.waitForIdle(robot);
    robot.delay(500);

    if (dropSuccess) {
        System.err.println("test passed");
    } else {
        throw new RuntimeException("test failed: drop was not successful");
    }
}