java 如何在 GWT 中居中弹出/对话框窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14231788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to center a popup/dialog window in GWT?
提问by Dragon
I have a list of records, each record has a button that opens setting window. Also I have preferences with a lot of logical parts and each part has some records with a button.
我有一个记录列表,每条记录都有一个打开设置窗口的按钮。我也有很多逻辑部分的偏好,每个部分都有一些带按钮的记录。
So, when I click on a button, dialog window appears, but if this button is somewhere in the bottom the page (let it be 300 records with button), my dialog window appears on the top. As a result I have to scroll up in order to find this window (sometimes it appears behind browser borders).
因此,当我单击一个按钮时,会出现对话框窗口,但是如果此按钮位于页面底部的某个位置(让它成为带有按钮的 300 条记录),则我的对话框窗口会出现在顶部。因此,我必须向上滚动才能找到此窗口(有时它出现在浏览器边框后面)。
Or just imagine that you configure something and scroll page down filling each option, at the end you saves your configuration but some fields were filled incorrectly and warning popup window appears... somewhere on page, but not near your pointer or even not in the middle of your current page.
或者想象一下你配置了一些东西并向下滚动页面来填充每个选项,最后你保存了你的配置但是一些字段填写不正确并且警告弹出窗口出现......页面上的某个地方,但不在你的指针附近,甚至不在当前页面的中间。
All my dialog windows are inherited from DialogBox, so I use super.show() and super.center(), but it doesn't work as I want it to.
我所有的对话框窗口都是从 DialogBox 继承的,所以我使用了 super.show() 和 super.center(),但它不能像我想要的那样工作。
Small info addition: some dialog windows receive data from server and when all the data is received the rest of the window is displayed. I suspect this can cause sometimes window to appear in a wrong place.
添加小信息:一些对话窗口从服务器接收数据,当接收到所有数据时,将显示窗口的其余部分。我怀疑这有时会导致窗口出现在错误的位置。
回答by Riley Lark
You are right that editing the content of the dialog box after it is centered will cause it to appear uncentered. To solve this issue I called center after the content is loaded. You can display a loading spinner or something in the meantime.
你是对的,在对话框居中后编辑对话框的内容会导致它看起来不居中。为了解决这个问题,我在加载内容后调用了 center。您可以同时显示加载微调器或其他内容。
In some cases, the size of the widget you added cannot be calculated immediately. Unfortunately the best way I found to deal with this was to add the widget and then run
在某些情况下,您添加的小部件的大小无法立即计算。不幸的是,我发现处理这个问题的最好方法是添加小部件然后运行
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
public void execute() {
dialogBox.center();
}
});
(unchecked code)
(未经检查的代码)
This ended up being so common that I added a method called scheduleCenter
to my DialogBox class.
这最终变得如此普遍,以至于我添加了一个调用scheduleCenter
到我的 DialogBox 类的方法。
You can also call center more than once - each time will re-run the centering calculations. This causes a little bit of jerking. If you don't want any jerking around, then you'll have to set the size of the interior widget explicitly:
您还可以多次呼叫中心 - 每次都会重新运行居中计算。这会导致有点抽搐。如果您不想四处乱动,则必须明确设置内部小部件的大小:
DialogBox dialogBox = new DialogBox();
dialogBox.add(myWidgetWhichWillLoadDataLater);
myWidgetWhichWillLoadDataLater.addStyleName(styleNameWhichSetsSizeExplicitly)
dialogBox.center();
You could also call myWidgetWhichWillLoadDataLater.setHeight("300px"), etc.
您也可以调用 myWidgetWhichWillLoadDataLater.setHeight("300px") 等。
回答by Dipak
DialogBox box = new DialogBox();
RootLayoutPanel.get().add(box);
RootLayoutPanel.get().setWidgetBottomHeight(box, 0, null, 0, null);
box.center();
box.show();
setWidgetBottomHeight() method to position exactly.
setWidgetBottomHeight() 方法来精确定位。
回答by Abhijith Nagaraja
DialogBox box = new DialogBox();
//Populate the box
box.center()