JFrame getLocation and setLocation deal with system border differently

☆樱花仙子☆ 提交于 2019-12-11 03:26:46

问题


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().


回答1:


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.



来源:https://stackoverflow.com/questions/23074312/jframe-getlocation-and-setlocation-deal-with-system-border-differently

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!