如何在屏幕右侧设置 jFrame 位置?

How can I set jFrame Location at right of Screen?

this.setLocationRelativeTo(null);

jFrame 将显示在屏幕中央。但是我不知道如何在屏幕右侧设置jFrame。

如果组件不为 null 并且显示在屏幕上,则 window 的位置使得 window 的中心与组件的中心重合。

换句话说,您应该尝试 frame.setLocation(),然后尝试一些值 (x,y) 并查看合适的值

编辑:值 0,0 将给出右角 所以在你的情况下:this.setLocation(0,0);

您可以自己根据屏幕尺寸和边框计算位置。

static void setLocationToTopRight(JFrame frame) {
    GraphicsConfiguration config = frame.getGraphicsConfiguration();
    Rectangle bounds = config.getBounds();
    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);

    int x = bounds.x + bounds.width - insets.right - frame.getWidth();
    int y = bounds.y + insets.top;
    frame.setLocation(x, y);
}

屏幕插图包括 window 预计不会占据的位置,例如任务栏和 Mac 菜单栏。可能有一个 OS,您可以在屏幕右侧放置一些东西,如果您不减去插图,这会干扰 window 放置。 (我认为 Ubuntu 实际上可以做到这一点,但我不记得 window 是放在菜单的顶部还是后面。)


这是一个简单的 MCVE,演示了所有四个边。按下四个按钮之一会将 JFrame 锚定到该边缘。

package mcve;

import javax.swing.*;
import java.awt.*;

public class WindowPlacement {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Window Placement");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JButton top = new JButton("Top");
            JButton left = new JButton("Left");
            JButton bottom = new JButton("Bottom");
            JButton right = new JButton("Right");

            frame.add(top, BorderLayout.NORTH);
            frame.add(left, BorderLayout.WEST);
            frame.add(bottom, BorderLayout.SOUTH);
            frame.add(right, BorderLayout.EAST);

            top.addActionListener(e -> setLocationToTop(frame));
            left.addActionListener(e -> setLocationToLeft(frame));
            bottom.addActionListener(e -> setLocationToBottom(frame));
            right.addActionListener(e -> setLocationToRight(frame));

            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

    // Also see:
    // https://docs.oracle.com/javase/9/docs/api/java/awt/GraphicsEnvironment.html#getMaximumWindowBounds--
    static Rectangle getMaxWindowBounds(JFrame frame) {
        GraphicsConfiguration config = frame.getGraphicsConfiguration();
        Rectangle bounds = config.getBounds();
        Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);
        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= insets.left + insets.right;
        bounds.height -= insets.top + insets.bottom;
        return bounds;
    }

    static void setLocationToTop(JFrame frame) {
        frame.setLocation(frame.getX(), getMaxWindowBounds(frame).y);
    }

    static void setLocationToLeft(JFrame frame) {
        frame.setLocation(getMaxWindowBounds(frame).x, frame.getY());
    }

    static void setLocationToBottom(JFrame frame) {
        Rectangle bounds = getMaxWindowBounds(frame);
        frame.setLocation(frame.getX(), bounds.y + bounds.height - frame.getHeight());
    }

    static void setLocationToRight(JFrame frame) {
        Rectangle bounds = getMaxWindowBounds(frame);
        frame.setLocation(bounds.x + bounds.width - frame.getWidth(), frame.getY());
    }
}