0

私はこれを簡単にしていますArrayList

Image img = new Image("/Mobile 005.JPG");
Image img1 = new Image("/Mobile 006.JPG");
Image img2 = new Image("/Mobile 007.JPG");
Image img3 = new Image("/Mobile 008.JPG");
imgList.add(img);
imgList.add(img1);
imgList.add(img2);
imgList.add(img3);

HorizontPanel hpnl = new HorizontalPanel();
hpnl.add(imgList);

このイメージのリストはdbから取得され、任意の数にすることができます。

現時点では、これHorizontalPanelには4つの画像が含まれています(将来的には400の画像が含まれる可能性があります)。ユーザーが来て、2番目の画像をクリックすると、どの画像がユーザーによってクリックされたかをどのように知ることができますか?

どこにどのように置くのclickHandlerですか?

4

3 に答える 3

2

次のように getSource() API を使用して、どの画像がクリックされたかを知ることができます。

Image img = new Image("/Mobile 005.JPG");
img.addClickHandler( getClickHandler() ); 
Image img1 = new Image("/Mobile 006.JPG");
img1.addClickHandler( getClickHandler() ); 
Image img2 = new Image("/Mobile 007.JPG");
img2.addClickHandler( getClickHandler() ); 
Image img3 = new Image("/Mobile 008.JPG");
img3.addClickHandler( getClickHandler() ); 
imgList.add(img);
imgList.add(img1);
imgList.add(img2);
imgList.add(img3);

HorizontPanel hpnl = new HorizontalPanel();
hpnl.add(imgList);

ClickHandler imageClickHandler;

private ClickHandler getClickHandler()
{ 
     if( imageClickHandler != null)
     {
          return imageClickHandler;
     }
     imageClickHandler = new ClickHandler()
     {
            public void onClick( ClickEvent event )
            {
              Image source = (Image)event.getSource();
                  // This is the source that has caused the event.
            }
     };

     return imageClickHandler;
}
于 2013-03-19T03:35:19.500 に答える
0

次の方法で試すこともできます。

    HorizontalPanel hp = new HorizontalPanel();
    hp.add(getImage("/Mobile 005.JPG"));
    hp.add(getImage("/Mobile 006.JPG"));
    hp.add(getImage("/Mobile 007.JPG"));
    hp.add(getImage("/Mobile 008.JPG"));

    private Image getImage(String imagePath){
    Image image = new Image(imagePath);
    image.addClickHandler(new ClickHandler() {

     @Override
     public void onClick(ClickEvent event) {
       // write the on click code.
     }
     });

}

imagepath 変数から、どの onclick が呼び出されたかを知ることができます。

于 2013-03-19T11:15:54.607 に答える
-3

このようなものを探しています

List<Image> images = new ArrayList<Image>();
        images.add(new Image());
        images.add(new Image());//Into this list add your DB images list
        images.add(new Image());

        for (Image image : images) {
            image.addClickHandler(
           //singleton instance of clickhandler
            });
        }
于 2013-03-19T05:21:48.663 に答える