【发布时间】:2015-03-13 00:49:38
【问题描述】:
例如,您可以想象您被要求重新设计下拉菜单。这样做的正确方法似乎是制作 JFrame 并在单击某个元素时显示它。这样的 JFrame 将是未装饰的。我正在做这个。我的程序希望允许用户单击图像(代表某物)并从其他可用图像中进行选择。
所以我做了这样的JFrame:
public class FrameSummonerSpells extends JFrame {
public FrameSummonerSpells() {
settings = set;
// Remove title bar, close buttons...
setUndecorated(true);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//Will populate the selection of objects in grid layout
createButtons();
//Will make the frame as large as the content
pack();
//Do not appear by default
setVisible(false);
}
}
我从JButton显示它:
private void showPopup() {
//Create the popup if needed
if(popup==null)
createPopup();
//Get location of this button
Point location = getLocation();
//Move the popup at the location of this button
//Also move vertically so it appears UNDER the button
popup.setLocation(location.x, location.y+this.getSize().height);
//Show the popup
popup.setVisible(true);
}
如您所见,我设置的位置不是相对于按钮或框架设置的:
然而,应该注意的是,我发送给setLocation 的参数是被接受的,但只是被错误地解释了。
因此,我的问题是:如何生成一个可以包含 JComponent 的新对象(JFrame 或其他东西),出现在窗口上方或 甚至扩展 窗口上方并保持与窗口的相对位置(或某些元素)?
这就是我要显示的内容:
【问题讨论】:
-
你有没有考虑尝试
JFrame#setLocationRelativeTo?组件坐标与它们所在的容器相关,它们不是屏幕坐标