使用setLocation在Windows,Java上移动JFrame

时间:2014-02-20 22:31:59

标签: java swing jframe actionevent

我正在尝试使用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 Toolkit用于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);

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

2 个答案:

答案 0 :(得分:4)

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

setLocation(500,500);

在这里你传递一个,但是它是什么? X还是Y?如果是X,那么是Y?:

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

编译器告诉你它没有带有一个参数的setLocation()方法。它想要一个2D空间中的位置:那是一个X Y.可能你想要的是:

setLocation(north, 0);

答案 1 :(得分:2)

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

例如......