JFrame getLocation和setLocation处理系统边界的方式有所不同 [英] JFrame getLocation and setLocation deal with system border differently
问题描述
我正在创建一个多用户Swing GUI应用程序,并希望用户在注销并重新登录时保留其窗口位置和大小设置.我目前正在使用 我遇到的问题是 有人知道为什么会发生这种情况或我如何解决它?我还应该使用其他方法来获取和设置窗口的位置和大小吗? 如果有帮助,我正在Ubuntu 12.04上运行Java 1.7.0_45. 谢谢! 以下示例复制了我所看到的问题: 此外,当我使用 您可以使用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 The problem that I am having is that 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: Also, I see the same issue when I use 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屋!getLocation()
和setLocation()
和setSize()
设置窗口大小和位置.>
getLocation()
和getSize()
似乎正在减去系统边界(例如,如果将窗口放在左上角,则getLocation
返回(1,54)
而不是(0,0)
),但setLocation()
和setSize()
则没有.结果是,每次我注销并重新登录时,该窗口看起来都比关闭时略微偏移且略小.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()
时,也会遇到相同的问题.
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()
.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.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()
instead of getLocation()
.