0

我有一个带有 4 个按钮的ribbonBar。现在,在单击的每个按钮上,我都想执行一些操作。但我遇到的问题是如何知道单击了哪个按钮。有人可以指导我。下面是我的代码和屏幕截图。顺便说一下,我使用的是 Button 而不是展示中所示的 IconButton。如果我使用这个按钮可以吗??

在此处输入图像描述

public class DBilling implements EntryPoint
{

@Override
public void onModuleLoad()
{
    VLayout vLayout = new VLayout();
    vLayout.setWidth100();

    RibbonBar ribbonBar = new RibbonBar();
    ribbonBar.setLeft(0);
    ribbonBar.setTop(0);
    ribbonBar.setWidth100();

    ribbonBar.setMembersMargin(2);
    ribbonBar.setLayoutMargin(2);

    RibbonGroup orderGroup = new RibbonGroup();
    orderGroup.setTitle("New Order");
    orderGroup.setRowHeight(60);
    orderGroup.addControl(getButton("Order", "order", false));

    RibbonGroup reportGroup = new RibbonGroup();
    reportGroup.setTitle("Report");
    reportGroup.setRowHeight(60);
    reportGroup.addControl(getButton("Report", "report", false));

    RibbonGroup productGroup = new RibbonGroup();
    productGroup.setTitle("New Product");
    productGroup.setRowHeight(60);
    productGroup.addControl(getButton("Product", "cookies", false));

    RibbonGroup systemGroup = new RibbonGroup();
    systemGroup.setTitle("System");
    systemGroup.setRowHeight(60);
    systemGroup.addControl(getButton("System", "system", false));

    ribbonBar.addMember(orderGroup);
    ribbonBar.addMember(reportGroup);
    ribbonBar.addMember(productGroup);
    ribbonBar.addMember(systemGroup);

    vLayout.addChild(ribbonBar);
    vLayout.draw();

}

private Button getButton(String title, String iconName, boolean vertical)
{
    final Button cssButton = new Button(title);  
    cssButton.setShowRollOver(true);  
    cssButton.setShowDisabled(true);  
    cssButton.setShowDown(true);  
    cssButton.setIcon(iconName + ".png"); 
    cssButton.setIconSize(32);
    cssButton.setWidth(120);
    cssButton.addClickHandler(new ClickHandler()
    {

        @Override
        public void onClick(ClickEvent event)
        {

            SC.say(event.getSource().toString());
        }
    });

    return cssButton;
}
4

1 回答 1

1

未经测试,但我认为这样的东西是你想要的:

public class DBilling implements EntryPoint
{

    private RibbonBar ribbonBar;

    @Override
    public void onModuleLoad()
    {
        VLayout vLayout = new VLayout();
        vLayout.setWidth100();

        this.ribbonBar = new RibbonBar();
        this.ribbonBar.setLeft(0);
        this.ribbonBar.setTop(0);
        this.ribbonBar.setWidth100();

        this.ribbonBar.setMembersMargin(2);
        this.ribbonBar.setLayoutMargin(2);

        RibbonGroup orderGroup = new RibbonGroup();
        orderGroup.setTitle("New Order");
        orderGroup.setRowHeight(60);
        orderGroup.addControl(getButton("Order", "order", false));

        RibbonGroup reportGroup = new RibbonGroup();
        reportGroup.setTitle("Report");
        reportGroup.setRowHeight(60);
        reportGroup.addControl(getButton("Report", "report", false));

        RibbonGroup productGroup = new RibbonGroup();
        productGroup.setTitle("New Product");
        productGroup.setRowHeight(60);
        productGroup.addControl(getButton("Product", "cookies", false));

        RibbonGroup systemGroup = new RibbonGroup();
        systemGroup.setTitle("System");
        systemGroup.setRowHeight(60);
        systemGroup.addControl(getButton("System", "system", false));

        this.ribbonBar.addMember(orderGroup);
        this.ribbonBar.addMember(reportGroup);
        this.ribbonBar.addMember(productGroup);
        this.ribbonBar.addMember(systemGroup);

        vLayout.addChild(this.ribbonBar);
        vLayout.draw();

    }

    private Button getButton(String title, String iconName, boolean vertical)
    {
        final Button cssButton = new Button(title);  
        cssButton.setShowRollOver(true);  
        cssButton.setShowDisabled(true);  
        cssButton.setShowDown(true);  
        cssButton.setIcon(iconName + ".png"); 
        cssButton.setIconSize(32);
        cssButton.setWidth(120);
        cssButton.addClickHandler(new ClickHandler()
        {

            @Override
            public void onClick(ClickEvent event)
            {
                RibbonBar a;
                for(Canvas b : a.getMembers()){
                    RibbonGroup c=(RibbonGroup)b;
                    for(Canvas d : c.getControls()){
                        Button e = (Button)d;
                        if(e.equals(event.getSource()))
                            SC.say(e.getTitle());
                    }
                }

                SC.say(event.getSource().toString());
            }
        });

        return cssButton;
    }

}

或者真的,如果你总是知道它Button只是event.getSource()转换为 a Button,那么你可以获得像这样的按钮标题 ((Button)event.getSource()).getTitle()

于 2015-03-05T22:04:12.227 回答