2017-09-26 36 views
2

当你犯了一个SpanButton主角为一个容器,它不接收的事件。我试过调试,但还没有设法理解它为什么不起作用。这似乎是一个CN1的错误,但我可能误解了一些东西。如何使SpanButton在制作铅时工作?

这是一个测试案例重现该问题:

Form hi = new Form("Welcome", BoxLayout.y()); 

    //test case shows that when you make a normal Button lead for a Container, 
    //it works as expected. 
    //Doing the same with a SpanButton doesn't work. 
    //When you click button it correctly hides the Label. 
    //Clicking spanButton doesn't hide the label. 
    // 
    //Test also shows a second issue: when adding a Command to a SpanButton, 
    //the Command text is shown in parallel with the SpanButton's text 


    //edit Button works normal 
    Button button = new Button("Normal Button works"); 
    Label hide2 = new Label("Now you see me, now you don't2"); 
    Command cmd2 = Command.create("CmdTxt2", null, (ev) -> { 
     hide2.setHidden(!hide2.isHidden()); 
     hi.revalidate(); 
    }); 
    button.setCommand(cmd2); 
    Label editButtonLabel2 = new Label(); 
    FontImage.setMaterialIcon(editButtonLabel2, FontImage.MATERIAL_CHEVRON_RIGHT); // [>] 

    Container cont2 = BorderLayout.centerEastWest(button, editButtonLabel2, null); 
    cont2.setLeadComponent(button); //for a normal button, the lead works correctly 
    hi.add(cont2); 
    hi.add(hide2); 

    //with SpanButton it doesn't work: 
    SpanButton spanButton = new SpanButton("SpanButton does not work"); 
    Label hide = new Label("Now you see me, now you don't"); 
    Command cmd = Command.create("CmdText", null, (ev) -> { 
     hide.setHidden(!hide.isHidden()); 
     hi.revalidate(); 
    }); 
    spanButton.setCommand(cmd); 
    Label editButtonLabel = new Label(); 
    FontImage.setMaterialIcon(editButtonLabel, FontImage.MATERIAL_CHEVRON_RIGHT); // [>] 

    Container cont = BorderLayout.centerEastWest(spanButton, editButtonLabel, null); 
    cont.setLeadComponent(spanButton); //spanButton made lead for cont, so should receive all events for cont, but doesn't 
    hi.add(cont); 
    hi.add(hide); 


    hi.show(); 

回答

1

难道这是由于SpanButton是一个容器本身,也有铅成分是一种Button

您可以创建一个正常Button并应用命令,然后容器的铅成分设置为此Button。您没有这个Button添加到容器中,加入您的SpanButton和容器仍将收到的所有事件。

Form hi = new Form("Welcome", BoxLayout.y()); 

/*test case shows that when you make a normal Button lead for a Container, 
it works as expected. 
Doing the same with a SpanButton doesn't work. 
When you click the button, it correctly hides the Label. 
Clicking spanButton doesn't hide the label. 

Test also shows a second issue: when adding a Command to a SpanButton, 
the Command text is shown in parallel with the SpanButton's text*/ 


//edit Button works normal 
Button button = new Button("Normal Button works"); 
Label hide2 = new Label("Now you see me, now you don't2"); 
Command cmd2 = Command.create("CmdTxt2", null, (ev) -> { 
    hide2.setHidden(!hide2.isHidden()); 
    hi.revalidate(); 
}); 
button.setCommand(cmd2); 
Label editButtonLabel2 = new Label(); 
FontImage.setMaterialIcon(editButtonLabel2, FontImage.MATERIAL_CHEVRON_RIGHT); // [>] 

Container cont2 = BorderLayout.centerEastWest(button, editButtonLabel2, null); 
cont2.setLeadComponent(button); //for a normal button, the lead works correctly 
hi.add(cont2); 
hi.add(hide2); 

//with SpanButton it doesn't work: 
SpanButton spanButton = new SpanButton("SpanButton does not work"); 
Label hide = new Label("Now you see me, now you don't"); 
Command cmd = Command.create("CmdText", null, (ev) -> { 
    hide.setHidden(!hide.isHidden()); 
    hi.revalidate(); 
}); 
Button btnHidden = new Button(cmd); 

Label editButtonLabel = new Label(); 
FontImage.setMaterialIcon(editButtonLabel, FontImage.MATERIAL_CHEVRON_RIGHT); // [>] 

Container cont = BorderLayout.centerEastWest(spanButton, editButtonLabel, null); 
cont.setLeadComponent(btnHidden); //spanButton made lead for cont, so should receive all events for cont, but doesn't 
hi.add(cont); 
hi.add(hide); 


hi.show(); 
+1

是。你不能让领导组件成为领导者,因为这会进入循环。 –

+1

谢谢你,你能记录下来吗(我浪费了一天的调试时间,因为你很容易认为SpanButton可以用来代替正常的Button)?并感谢钻石,我会尝试你认为我会工作的建议。 – user1246562

相关问题