Java FX按钮高亮显示

Java FX按钮高亮显示,java,javafx,javafx-8,javafx-2,Java,Javafx,Javafx 8,Javafx 2,我想做的是,请看附加的屏幕截图。一旦我在按钮1-4之间单击,它应该以红色和红色突出显示 保持高亮显示,直到我选择按钮1和按钮4之间的任何其他按钮,然后高亮显示所选按钮应为 突出显示。我可以通过聚焦属性来实现这一点。但我的场景中还有其他按钮,比如按钮5、6和7。单击任何其他按钮或单击其他控件后 焦点和红色消失。但是我希望单击的按钮保持高亮显示,或者显示选中了哪个按钮(在按钮1和按钮4之间)。我建议使用ToggleGroup和ToggleButton。ToggleGroup允许用户一次只选择一个按钮

我想做的是,请看附加的屏幕截图。一旦我在按钮1-4之间单击,它应该以红色和红色突出显示 保持高亮显示,直到我选择按钮1和按钮4之间的任何其他按钮,然后高亮显示所选按钮应为 突出显示。我可以通过聚焦属性来实现这一点。但我的场景中还有其他按钮,比如按钮5、6和7。单击任何其他按钮或单击其他控件后
焦点和红色消失。但是我希望单击的按钮保持高亮显示,或者显示选中了哪个按钮(在按钮1和按钮4之间)。

我建议使用
ToggleGroup
ToggleButton
ToggleGroup
允许用户一次只选择一个按钮。选择按钮后,可以设置所需的样式

在下面的示例程序中,我在组中有6个切换按钮,在任何给定时间只能选择一个。所选按钮将具有红色背景(突出显示)。您创建的任何没有此样式的按钮将不受影响

下面的代码也有注释:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ButtonHighlights extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Create a ToggleGroup to hold the list of ToggleButtons. This will allow us to allow the selection of only one
        // ToggleButton at a time
        ToggleGroup toggleGroup = new ToggleGroup();

        // Create our 6 ToggleButtons. For this sample, I will use a for loop to add them to the ToggleGroup. This is
        // not necessary for the main functionality to work, but is used here to save time and space
        for (int i = 0; i < 6; i++) {
            ToggleButton button = new ToggleButton("Button #" + i);

            // If you want different styling for the button when it's selected other than the default, you can either
            // use an external CSS stylesheet, or apply the style in a listener like this:
            button.selectedProperty().addListener((observable, oldValue, newValue) -> {

                // If selected, color the background red
                if (newValue) {
                    button.setStyle(
                            "-fx-background-color: red;" + 
                            "-fx-text-fill: white");
                } else {
                    button.setStyle(null);
                }
            });

            // Add the button to our ToggleGroup
            toggleGroup.getToggles().add(button);
        }

        // Add all our buttons to the scene
        for (Toggle button :
                toggleGroup.getToggles()) {
            root.getChildren().add((ToggleButton) button);
        }

        // Show the Stage
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}
导入javafx.application.application;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Toggle;
导入javafx.scene.control.ToggleButton;
导入javafx.scene.control.ToggleGroup;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类按钮highlights扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
//简单接口
VBox根=新的VBox(5);
根。设置填充(新插图(10));
根部设置对齐(位置中心);
//创建一个ToggleGroup来保存ToggleButtons列表。这将允许我们只选择一个
//一次切换一个按钮
ToggleGroup ToggleGroup=新的ToggleGroup();
//创建我们的6个ToggleButtons。对于这个示例,我将使用For循环将它们添加到ToggleGroup。这是
//主功能不需要工作,但在此处用于节省时间和空间
对于(int i=0;i<6;i++){
ToggleButton=新的ToggleButton(“按钮#”+i);
//如果在选择按钮而不是默认按钮时,希望按钮具有不同的样式,则可以
//使用外部CSS样式表,或在侦听器中应用该样式,如下所示:
button.selectedProperty().addListener((可观察、旧值、新值)->{
//如果选中,将背景颜色设置为红色
如果(新值){
按钮。设置样式(
“-fx背景色:红色;”+
“-fx文本填充:白色”);
}否则{
按钮设置样式(空);
}
});
//将按钮添加到我们的切换组
toggleGroup.getToggles().add(按钮);
}
//将所有按钮添加到场景中
对于(切换按钮:
toggleGroup.getToggles()){
root.getChildren().add((ToggleButton)按钮);
}
//上台
初级阶段。设置宽度(300);
初生阶段:坐位高度(300);
primaryStage.setScene(新场景(根));
primaryStage.show();
}
}
结果:


你看了吗?是的,我试过切换按钮。但它没有成功。一旦我点击了我不想突出显示的任何其他按钮(按钮5、6、7),它就会失去聚焦属性,不再被突出显示。如果“突出显示”是指聚焦指示器,那么你不应该自己设置,因为这会混淆用户。切换按钮保持在按下状态,直到按下其组中的另一个按钮。您必须回答您的问题,以提供您正在尝试执行的操作的上下文以及您想要突出显示的内容。通常,
RadioButton
s(以及
ToggleGroup
)用于此类行为。很抱歉造成混淆。基本上,我想做的是,我想显示哪个菜单(按钮)被选中。我想把标签放在每个按钮旁边,当按钮被点击时,画那个特定的按钮。