1

这是我的课程,我为我的 GWT 项目实现了一个简单的图片显示

public class PhotosSlideShow extends Composite {

private List<Image> photos;
private int curr;
private AbsolutePanel mainPnl;
private SimplePanel centerPnl;
private Image n;
private Image p;
private final int WIDTH = 800;

public PhotosSlideShow(final List<String> result) {

    curr = 0;
    photos = new ArrayList<Image>();
    mainPnl = new AbsolutePanel();
    this.initWidget(mainPnl);
    this.setStyleName("slideShow");
    centerPnl = new SimplePanel();

    String path = GWT.getModuleBaseURL() + "imageUpload?src=";
    for (String foto : result) {
        String url = path.concat(foto);
        Image.prefetch(url);
        final Image curr = new Image(url);
        double ratio = getRatio(curr);
        curr.setPixelSize(WIDTH, (int) (ratio * WIDTH));

        curr.addMouseOverHandler(new MouseOverHandler() {

            @Override
            public void onMouseOver(MouseOverEvent event) {
                n.setVisible(true);
                p.setVisible(true);
            }

        });
        curr.addMouseOutHandler(new MouseOutHandler() {

            @Override
            public void onMouseOut(MouseOutEvent event) {
                n.setVisible(false);
                p.setVisible(false);
            }

        });
        photos.add(curr);

    }

    n = new Image("images/next.png");
    n.setPixelSize(40, 50);
    n.setVisible(false);
    n.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            showNext();
        }

    });

    p = new Image("images/prev.png");
    p.setPixelSize(40, 50);
    p.setVisible(false);
    p.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            showPrev();
        }

    });
    centerPnl.setWidget(photos.get(curr));
    mainPnl.add(centerPnl);
    mainPnl.add(n);
    mainPnl.add(p);
    posizionaBottoni();

}

private void showNext() {
    if (curr == photos.size() - 1) {
        curr = 0;
        centerPnl.setWidget(photos.get(curr));
    } else {
        curr++;
        centerPnl.setWidget(photos.get(curr));
    }
    posizionaBottoni();
}

private void showPrev() {
    if (curr == 0) {
        curr = photos.size() - 1;
        centerPnl.setWidget(photos.get(curr));
    } else {
        curr--;
        centerPnl.setWidget(photos.get(curr));
    }
    posizionaBottoni();
}

private void posizionaBottoni() {
    mainPnl.setWidgetPosition(p, 0,
            (int) (photos.get(curr).getHeight() / 2 - p.getHeight() / 2));
    mainPnl.setWidgetPosition(n, (int) (WIDTH - n.getWidth()),
            (int) (photos.get(curr).getHeight() / 2 - n.getHeight() / 2));
}

private double getRatio(Image i) {

    System.out.println("DIMENSIONI ORIGINALI: " + i.getWidth() + " x "
            + i.getHeight());
    double res = (double) i.getHeight() / i.getWidth();
    System.out.println("RATIO: " + res);
    return res;
}

}

显然一切都很好,但有一个小问题:当我将指针悬停在图像上时,按钮正确显示,但是当我将鼠标移动到按钮上单击它们时,它们会闪烁。有谁知道这种奇怪行为的原因可能是什么?提前致谢。

4

0 回答 0