GWT 复合小部件

发布于 2024-10-20 08:25:23 字数 1588 浏览 3 评论 0原文

我尝试构建自己的 GWT 小部件。我选择复合方式来构建它。但我有问题,小部件没有在屏幕上显示。这是复合小部件代码:

public class LoginPanel extends Composite {

private static final String DEFAULT_STYLENAME = "LoginPanel";

private TextBox nameField;
private PasswordTextBox passField;
private Button loginButton;
private Label errorLbl;
private DecoratedPopupPanel decoratedPopupPanel;

public LoginPanel() {
    nameField = new TextBox();
    passField = new PasswordTextBox();
    loginButton = new Button("Login");
    loginButton.getElement().setId("loginButton");
    errorLbl = new Label();

    Grid grid = new Grid(4, 2);
    grid.setWidget(0,0, errorLbl);
    grid.setWidget(1,0, new Label("Name"));
    grid.setWidget(1,1, nameField);
    grid.setWidget(2,0, new Label("Password"));
    grid.setWidget(2,1, passField);
    grid.setWidget(3,1, loginButton);

    decoratedPopupPanel = new DecoratedPopupPanel();
    initWidget(decoratedPopupPanel);
    //decoratedPopupPanel.setAnimationEnabled(true);
    decoratedPopupPanel.setWidget(grid);
    //decoratedPopupPanel.setAnimationEnabled(true);
    decoratedPopupPanel.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
        public void setPosition(int i, int i1) {
            decoratedPopupPanel.setPopupPosition(Window.getClientWidth()/2, Window.getClientHeight()/2);
        }
    });
}

}

在入口类函数 onModuleLoad() 中,我有这样的代码:

LoginPanel lp = new LoginPanel();
RootPanel.get().add(lp);

当我在 LoginPanel 类中删除 initWidget() 后面的行时 函数,则一切正常,但小部件未居中。有人可以帮助我吗?

I try to build own GWT widget. I select the composite way of build it. But I have problem, the widget didn't see on screen. Here is the composite widget code:

public class LoginPanel extends Composite {

private static final String DEFAULT_STYLENAME = "LoginPanel";

private TextBox nameField;
private PasswordTextBox passField;
private Button loginButton;
private Label errorLbl;
private DecoratedPopupPanel decoratedPopupPanel;

public LoginPanel() {
    nameField = new TextBox();
    passField = new PasswordTextBox();
    loginButton = new Button("Login");
    loginButton.getElement().setId("loginButton");
    errorLbl = new Label();

    Grid grid = new Grid(4, 2);
    grid.setWidget(0,0, errorLbl);
    grid.setWidget(1,0, new Label("Name"));
    grid.setWidget(1,1, nameField);
    grid.setWidget(2,0, new Label("Password"));
    grid.setWidget(2,1, passField);
    grid.setWidget(3,1, loginButton);

    decoratedPopupPanel = new DecoratedPopupPanel();
    initWidget(decoratedPopupPanel);
    //decoratedPopupPanel.setAnimationEnabled(true);
    decoratedPopupPanel.setWidget(grid);
    //decoratedPopupPanel.setAnimationEnabled(true);
    decoratedPopupPanel.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
        public void setPosition(int i, int i1) {
            decoratedPopupPanel.setPopupPosition(Window.getClientWidth()/2, Window.getClientHeight()/2);
        }
    });
}

}

In entry class function onModuleLoad() I have this peace of code:

LoginPanel lp = new LoginPanel();
RootPanel.get().add(lp);

When I delete in LoginPanel class rows behind initWidget() function, then all is right, but widget is not centered. Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-10-27 08:25:23

问题是 PopupPanel 不能用作“普通”小部件,因为它不会附加到父级,但它会将自身附加到 dom 以确保它位于顶部。因此在弹出窗口上使用 initWidget 没有意义。

为了完成这项工作,您应该将 LoginWidget 放在 PopupPanel 中,而不是相反。您可以将 PopupPanel 设为成员字段,并向 LoginPanel 添加一个方法 show 来调用 PopupPanel 字段上的 show。

如果您不希望登录小部件独立于您使用登录小部件的父级或上下文而浮动,则不应使用 PopupPanel。

The problem is a PopupPanel can't be used as a 'normal' widget, because it won't be attached to a parent but it attaches itself to the dom to make guarantee it's on top. So using initWidget on a popup doesn't make sense.

To make this work you should put the LoginWidget in the PopupPanel instead of the other way around. You could make the PopupPanel a member field and add a method show to your LoginPanel that calls the show on the PopupPanel field.

If you don't want the login widget to float independent of a parent or context you're using the Login widget in, you shouldn't use a PopupPanel.

你好,陌生人 2024-10-27 08:25:23

我相信你必须把调用 initWidget 作为最后一件事。否则,所有“子”项目的处理都不会产生任何影响。

尝试一下,如果这不起作用,我会尝试给出更好的答案。

I believe you have to call initWidget as the last thing. Otherwise all the processing of 'children' items doesn't have anything to work against.

Try that, and if that doesn't work, I'll try and give a better answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文