简体   繁体   English

Java FX按钮突出显示

[英]Java FX button highlight

在此处输入图片说明

What I want to do is, Please see attached screen shot. 我想做的是,请参阅所附的屏幕截图。 Once I click on between buttons 1-4 it should be highlighted with red and stay as highlighted until I select any other button between button 1 and button 4 and then highlight the selected button should be highlighted. 一旦我在按钮1-4之间单击,它应该以红色突出显示并保持突出显示状态,直到我在按钮1和按钮4之间选择任何其他按钮,然后突出显示所选按钮。 I can do this with focused property. 我可以通过集中属性来做到这一点。 But I have other buttons on my scene such that button 5,6 and 7. Once I click on any other button or click on another control focus and red color goes away. 但是我的场景中还有其他按钮,例如按钮5,6和7。一旦我单击任何其他按钮或单击另一个控件焦点,红色就会消失。 But I want the clicked button stay as highlighted, or a sign that will show which button(between button 1 and button 4) is selected. 但是我希望单击的按钮保持突出显示状态,或者要显示一个标志,以显示选择了哪个按钮(在按钮1和按钮4之间)。

I recommend using a ToggleGroup and ToggleButton for this. 我建议ToggleGroup使用ToggleGroupToggleButton The ToggleGroup allows your user to only select one button at a time. ToggleGroup允许您的用户一次只选择一个按钮。 When the button is selected, you can then set the style you want. 选择按钮后,即可设置所需的样式。

In the sample program below, I've got 6 ToggleButtons in the group and only one may be selected at any given time. 在下面的示例程序中,我在组中有6个ToggleButton,并且在任何给定时间只能选择一个。 The selected button will have a red background (highlight). 所选按钮将具有红色背景(突出显示)。 Any buttons you create that do not have this styling will be unaffected. 您创建的任何没有这种样式的按钮都不会受到影响。

The code below is commented as well: 下面的代码也被注释:

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();
    }
}

The Result: 结果:

屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM