【问题标题】:Toggle button in menu button菜单按钮中的切换按钮
【发布时间】:2017-05-21 19:20:17
【问题描述】:

伙计们,

很快:我想用 CheckMenuItems(done) 创建 MenuButton。因为我想摆脱复选框,并且我希望 CheckMenuItem 在选中它们后切换(更改颜色),所以我尝试使用 ToggleButtons 进行操作,但我无法将 ToggleButtons/ToggleGroup 放在 MenuButton 内。

感谢您的任何想法。

【问题讨论】:

    标签: java javafx togglebutton


    【解决方案1】:

    使用RadioMenuItem

    即做吧

    MenuButton menuButton = new MenuButton("Choices");
    ToggleGroup toggleGroup = new ToggleGroup();
    RadioMenuItem choice1 = new RadioMenuItem("Choice 1");
    RadioMenuItem choice2 = new RadioMenuItem("Choice 2");
    choice1.setToggleGroup(toggleGroup);
    choice2.setToggleGroup(toggleGroup);
    menuButton.getItems().setAll(choice1, choice2);
    
    toggleGroup.selectedToggleProperty().addListener((obs, oldChoice, newChoice) -> {
        System.out.println("You chose "+((RadioMenuItem)newChoice).getText());
    });
    

    【讨论】:

    • 好的,谢谢,如果我想选择多个按钮?
    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多