将调整大小的侦听器/处理程序添加到 GWT 中的 VerticalPanel [英] Adding resize listener/handler to VerticalPanel in GWT

查看:22
本文介绍了将调整大小的侦听器/处理程序添加到 GWT 中的 VerticalPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将侦听器/处理程序添加到 VerticalPanel,使其成为当 VerticalPanel 改变大小时触发?

How do I add a listener/handler to a VerticalPanel such that it is triggered when the VerticalPanel changes in size?

我基本上尝试了以下方法:

I've basically tried the following:

VerticalPanel myPanel = new VerticalPanel();
//... code to add child widgets that may change the size of the parent vertical panel ...

myPanel.addHandler(new ResizeHandler() {
  public void onResize(final ResizeEvent event) {
    //... code to handle resize of vertical panel ...
  }

}, ResizeEvent.getType() );

但是处理程序没有被触发.

but the handler doesn't get triggered.

推荐答案

可靠工作的是窗口级调整大小侦听器:

What works reliably is the window-level resize listener:

Window.addResizeHandler(new WindowResizeHandler())

当用户调整浏览器窗口大小时,将调用您的调整大小处理程序:

Your resize handler will be called when the user resizes the browser window:

public void onResize(ResizeEvent event)

您可以使用 event.getHeight()event.getWidth() 来获取更新后的窗口尺寸.

You can use event.getHeight() and event.getWidth() to get the updated window dimensions.

由于您只获取整个窗口的事件,因此您必须自己传播到要布局的组件.我通常只调用我自己的 doLayout() 方法.这种方法甚至不需要调整大小事件的高度和宽度,因为正如 DLH 已经评论的那样,您可以在收到通知时询问大多数组件的尺寸,并用它进行计算.

Since you're only getting the event for the window as a whole, you'll have to do your own propagation to the components you want to lay out. I usually just call a doLayout() method of my own. This method doesn't even need the resize event's height and width because as DLH already comments, you can ask most components for their dimensions when you get notified, and do your calculations with that.

不幸的是,有些浏览器并不经常执行 onResize 操作.为了不让更新变得疯狂,他们有时可能会坚持事件几秒钟,使您的应用程序在等待正确"布局时明显丑陋.因此,在可能的情况下,您应该尝试将布局留给 CSS 并可能,这取决于您在表格与 CSS"之战中的立场,表格.

Unfortunately, some browsers don't do the onResize thing very often. In trying not to go crazy with updates, they may sometimes hold on to the event for some seconds, leaving your application visibly ugly while waiting for "proper" layout. Thus, where possible you should try to leave layout to CSS and possibly, depending on your stance in the "tables vs. CSS" war, tables.

这篇关于将调整大小的侦听器/处理程序添加到 GWT 中的 VerticalPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆