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

坚武
this.setLocationRelativeTo(null);

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

Radiodef

您可以根据屏幕和框架的尺寸计算位置,自己动手做。

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);
}

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


这是演示所有四个边缘的简单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());
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章