java 我如何在javafx中创建一个开关按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17467137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:04:49  来源:igfitidea点击:

How can i create a switch button in javafx?

javajavafx-2

提问by Rajesh

I want to create a switch button like aboveenter image description hereI am a swt developer where i used to get this widget Switchbutton. Can i have something similar in javafx

我想创建一个像上面那样的开关按钮在此处输入图片说明我是一个 swt 开发人员,我曾经在那里获得这个小部件Switchbutton。我可以在javafx中有类似的东西吗

回答by OttPrime

First inclination is to extend the JavaFX Labeland add a Buttonas a graphic and a SimpleBooleanPropertyfor listening. Set an ActionEventhandler on the button that toggles the Label's text, style, and graphic content alignment. The code below will get you started and you can play with styling and bounding.

第一个倾向是扩展JavaFXLabel并添加一个Button作为图形和一个SimpleBooleanProperty用于收听。ActionEvent在按钮上设置一个处理程序,用于切换Label的文本、样式和图形内容对齐方式。下面的代码将帮助您入门,您可以使用样式和边界。

package switchbutton;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;

public class SwitchButton extends Label
{
    private SimpleBooleanProperty switchedOn = new SimpleBooleanProperty(true);

    public SwitchButton()
    {
        Button switchBtn = new Button();
        switchBtn.setPrefWidth(40);
        switchBtn.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent t)
            {
                switchedOn.set(!switchedOn.get());
            }
        });

        setGraphic(switchBtn);

        switchedOn.addListener(new ChangeListener<Boolean>()
        {
            @Override
            public void changed(ObservableValue<? extends Boolean> ov,
                Boolean t, Boolean t1)
            {
                if (t1)
                {
                    setText("ON");
                    setStyle("-fx-background-color: green;-fx-text-fill:white;");
                    setContentDisplay(ContentDisplay.RIGHT);
                }
                else
                {
                    setText("OFF");
                    setStyle("-fx-background-color: grey;-fx-text-fill:black;");
                    setContentDisplay(ContentDisplay.LEFT);
                }
            }
        });

        switchedOn.set(false);
    }

    public SimpleBooleanProperty switchOnProperty() { return switchedOn; }
}

回答by user2683474

Couldn't this be done with two toggle buttons that are bound together (bind()) where each button get's it's own CSS styling? It actually seems like the CSS would be the tricky (but doable) part to get right.

这不能用两个绑定在一起的切换按钮 (bind()) 来完成,每个按钮都有自己的 CSS 样式吗?实际上,CSS 似乎是要正确处理的棘手(但可行)的部分。

Then you would just have your app listen to the toggle button of the two that actually does what you want?

然后你会让你的应用程序听两个实际上做你想要的切换按钮吗?

回答by user2683474

The controls FX project provides a switch button like you seek.

控制 FX 项目提供了一个您想要的开关按钮。