JFrame getLocation和setLocation处理系统边界的方式有所不同 [英] JFrame getLocation and setLocation deal with system border differently

查看:123
本文介绍了JFrame getLocation和setLocation处理系统边界的方式有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个多用户Swing GUI应用程序,并希望用户在注销并重新登录时保留其窗口位置和大小设置.我目前正在使用getLocation()当用户注销并将其保存到文件中,然后在用户重新登录时在父JFrame上使用方法,然后重新读取这些值并使用setLocation()setSize()设置窗口大小和位置.

我遇到的问题是getLocation()getSize()似乎正在减去系统边界(例如,如果将窗口放在左上角,则getLocation返回(1,54)而不是(0,0) ),但setLocation()setSize()则没有.结果是,每次我注销并重新登录时,该窗口看起来都比关闭时略微偏移且略小.

有人知道为什么会发生这种情况或我如何解决它?我还应该使用其他方法来获取和设置窗口的位置和大小吗?

如果有帮助,我正在Ubuntu 12.04上运行Java 1.7.0_45.

谢谢!

以下示例复制了我所看到的问题:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;


public class JFrameTest
{
  private JFrame frame; 
  private JButton button;

  private Point lastLocation;
  private Dimension lastSize;

  private void run()
  {
    button = new JButton("Test");
    button.addActionListener(listener);

    lastLocation = new Point(0, 0);
    lastSize = new Dimension(200, 200);

    initFrame();
  }

  private void initFrame()
  {
    frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    frame.getContentPane().add(button);

    frame.setLocation(lastLocation);
    frame.setPreferredSize(lastSize);

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

  private ActionListener listener = new ActionListener()
  {
    @Override
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() == button)
      {
        lastLocation = frame.getLocationOnScreen();
        lastSize = frame.getSize();

        frame.dispose();

        initFrame();
      }
    }
  };

  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable() {
      public void run()
      {
        new JFrameTest().run();
      }
    });
  }
}

此外,当我使用getLocationOnScreen()而不是getLocation()时,也会遇到相同的问题.

解决方案

您可以使用getLocationOnScreen()

以指定点的形式获取此组件的位置 屏幕坐标空间中组件的左上角.

I am creating a multi-user Swing GUI application, and want the user's window location and size settings to persist when they log out and back in. I am currently getting the window location and size using the getLocation() and getSize() methods on my parent JFrame when the user logs out and saving them to a file, and then when the user logs back in I read those values back in and set the window size and location using setLocation() and setSize().

The problem that I am having is that getLocation() and getSize() appear to be subtracting off the system border (e.g. if I put the window in the upper left corner getLocation returns (1,54) instead of (0,0)), but setLocation() and setSize() don't. The result is that every time I logout and log back in, the window appears slightly offset and slightly smaller than it did when I closed it.

Does anybody know why this might be happening or how I can get around it? Is there some other method I should be using to get and set the window location and size?

I'm running java 1.7.0_45 on Ubuntu 12.04, if that helps.

Thanks!

EDIT:

The following example replicates the issue I am seeing:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;


public class JFrameTest
{
  private JFrame frame; 
  private JButton button;

  private Point lastLocation;
  private Dimension lastSize;

  private void run()
  {
    button = new JButton("Test");
    button.addActionListener(listener);

    lastLocation = new Point(0, 0);
    lastSize = new Dimension(200, 200);

    initFrame();
  }

  private void initFrame()
  {
    frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    frame.getContentPane().add(button);

    frame.setLocation(lastLocation);
    frame.setPreferredSize(lastSize);

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

  private ActionListener listener = new ActionListener()
  {
    @Override
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() == button)
      {
        lastLocation = frame.getLocationOnScreen();
        lastSize = frame.getSize();

        frame.dispose();

        initFrame();
      }
    }
  };

  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable() {
      public void run()
      {
        new JFrameTest().run();
      }
    });
  }
}

Also, I see the same issue when I use getLocationOnScreen() instead of getLocation().

解决方案

You can use getLocationOnScreen()

Gets the location of this component in the form of a point specifying the component's top-left corner in the screen's coordinate space.

这篇关于JFrame getLocation和setLocation处理系统边界的方式有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆