使用 setLocation 在 Windows、Java 中移动 JFrame



我正在尝试使用 5 个按钮(北、东、南、西和中心)在 Windows 中移动JFrame 目前,所有当前代码都已到位,并且在使用时可以工作;

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==northButton)
    {
       setLocation(500,500);
    }
} //works
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()== northButton)
    {
       setLocation(north);
    }
} //doesn't work

但是,作为任务的一部分,我需要使用 Java 工具包来getScreenSize宽度和高度,并使用计算来计算屏幕的边界并将"北"发送到setLocation()(如上所述)。但是,使用此方法会引发错误"No suitable method found"我不确定如何解决此问题。目前,计算代码仅在以下,仅适用于北方。

int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int width = this.getWidth();
int height = this.getHeight();
int north = ((screenWidth - width)/2);

任何帮助将不胜感激。谢谢!

这里你传递两个参数,X 和 Y:

setLocation(500,500);

你在这里通过一个,但它是哪个? X 还是 Y? 如果是 X,则 Y 在哪里?

int north = ((screenWidth - width)/2);
setLocation(north);

编译器告诉您它没有采用一个参数的setLocation()方法。 它想要在2D空间中的位置:这是一个XY.可能你想要的是:

setLocation(north, 0);

Toolkit.getDefaultToolkit().getScreenSize()返回默认屏幕的完整大小,它不考虑任务栏或其他可能占用桌面空间的元素,窗口应避免放置在下方/上方。

更好的解决方案是使用Toolkit.getDefaultToolkit().getScreenInsets(GraphicsConfiguration)GraphicsConfiguration#getBounds

public static Rectangle getScreenViewableBounds(Window window) {
    return getScreenViewableBounds((Component) window);
}
public static Rectangle getScreenViewableBounds(Component comp) {
    return getScreenViewableBounds(getGraphicsDevice(comp));
}
public static Rectangle getScreenViewableBounds(GraphicsDevice gd) {
    Rectangle bounds = new Rectangle(0, 0, 0, 0);
    if (gd == null) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        gd = ge.getDefaultScreenDevice();
    }
    if (gd != null) {
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        bounds = gc.getBounds();
        Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= (insets.left + insets.right);
        bounds.height -= (insets.top + insets.bottom);
    }
    return bounds;
}
/**
 * Attempts to locate the graphics device that the component most likely is
 * on.
 *
 * This calculates the area that the window occupies on each screen deivce and
 * returns the one which it occupies the most.
 *
 * @param comp
 * @return
 */
public static GraphicsDevice getGraphicsDevice(Component comp) {
    GraphicsDevice device = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice lstGDs[] = ge.getScreenDevices();
    ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
    if (comp != null && comp.isVisible()) {
        Rectangle parentBounds = comp.getBounds();
        /*
         * If the component is not a window, we need to find its location on the
         * screen...
         */
        if (!(comp instanceof Window)) {
            Point p = new Point(0, 0);
            SwingUtilities.convertPointToScreen(p, comp);
            parentBounds.setLocation(p);
        }
        for (GraphicsDevice gd : lstGDs) {
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            Rectangle screenBounds = gc.getBounds();
            if (screenBounds.intersects(parentBounds)) {
                lstDevices.add(gd);
            }
        }
        if (lstDevices.size() == 1) {
            device = lstDevices.get(0);
        } else {
            GraphicsDevice gdMost = null;
            float maxArea = 0;
            for (GraphicsDevice gd : lstDevices) {
                int width = 0;
                int height = 0;
                GraphicsConfiguration gc = gd.getDefaultConfiguration();
                Rectangle bounds = gc.getBounds();
                Rectangle2D intBounds = bounds.createIntersection(parentBounds);
                float perArea = (float) ((intBounds.getWidth() * intBounds.getHeight()) / (parentBounds.width * parentBounds.height));
                if (perArea > maxArea) {
                    maxArea = perArea;
                    gdMost = gd;
                }
            }
            if (gdMost != null) {
                device = gdMost;
            }
        }
    }
    return device;
}

您遇到的主要问题是没有setLocation(int)这样的方法......int代表什么价值? x 还是 y 位置?

您需要将 x 和 y 位置传递给 setLocation 才能使其正常工作。

Rectangle bounds = getScreenViewableBounds(this); // where this is a reference to your window
int x = bounds.x + ((bounds.width - getWidth()) / 2;
int y = bounds.y;
setLocation(x, y);

例如。。。

最新更新