如何将JavaFX Slider设置为格式化时间? [英] How can I set the JavaFX Slider to format for time?

查看:219
本文介绍了如何将JavaFX Slider设置为格式化时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JavaFX创建了一个 Slider 。我试图设置它以便以分钟显示时间。



我能够将分钟设置为0到60之间。我的问题是我的分钟显示正确,但我的秒数在0到100之间。



我该如何解决这个问题?



以下是我创建滑块的方法

 < ScrollPane fx:id =RulerScroll1hbarPolicy = NEVERmaxHeight =40minHeight =40pannable =trueGridPane.columnIndex =1GridPane.hgrow =ALWAYSGridPane.rowIndex =2> 
< Slider fx:id =Ruler1majorTickUnit =10maxHeight =35min =0minHeight =35minorTickCount =4showTickLabels =trueshowTickMarks =truesnapToPixel =true/>
< / ScrollPane>

如何格式化它给我的值,使它们可以显示为分钟和秒?

解决方案

您可以使用



它将 Slider 的范围设置为0-3600并显示15:00中每15分钟的刻度RMAT。 Slider 的值以相同的格式显示在 Text 控件的每一秒。

 公共类SliderTime扩展Application {
@Override
public void start(Stage primaryStage){
try {
HBox root = new HBox();
场景场景=新场景(root,400,400);

Slider sl = new Slider(0,3600,20);
sl.setMajorTickUnit(450);
sl.setShowTickLabels(true);
StringConverter< Double> stringConverter = new StringConverter<>(){

@Override
public String toString(Double object){
long seconds = object.longValue();
long minutes = TimeUnit.SECONDS.toMinutes(seconds);
long remainingseconds = seconds - TimeUnit.MINUTES.toSeconds(minutes);
返回String.format(%02d,分钟)+:+ String.format(%02d,remainingseconds);
}

@Override
public Double fromString(String string){
return null;
}
};

sl.setLabelFormatter(stringConverter);

Text text = new Text();

sl.valueProperty()。addListener((observable,oldValue,newValue) - >
text.setText(stringConverter.toString(newValue.doubleValue())));

root.getChildren()。addAll(sl,text);

primaryStage.setScene(scene);
primaryStage.show();
} catch(例外e){
e.printStackTrace();
}
}

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


I have created a Slider using JavaFX. I`m trying to set it up so that it shows time in minutes.

I have been able to setup the the minutes to range from 0 - 60. My problem is my minutes are showing correctly but my seconds are between 0 - 100.

How can I fix this?

Here is how I created the slider

 <ScrollPane fx:id="RulerScroll1" hbarPolicy="NEVER" maxHeight="40" minHeight="40" pannable="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2">    
     <Slider fx:id="Ruler1" majorTickUnit="10" maxHeight="35" min="0" minHeight="35" minorTickCount="4" showTickLabels="true" showTickMarks="true" snapToPixel="true" />  
 </ScrollPane>

How can I format the values it gives me so that they can appear as minutes and seconds?

解决方案

You could use a StringConverter to display the value of the Slider on another control, and also you can use this converter for the labelFormatterProperty of the Slider.

In the Example

It sets the range of the Slider from 0-3600 and shows the ticks for every 15 minutes in "15:00" format. The value of the Slider is displayed on a Text control for every second in the same format.

public class SliderTime extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            HBox root = new HBox();
            Scene scene = new Scene(root,400,400);

            Slider sl = new Slider(0, 3600, 20);
            sl.setMajorTickUnit(450);
            sl.setShowTickLabels(true);
            StringConverter<Double> stringConverter = new StringConverter<>() {

                @Override
                public String toString(Double object) {
                    long seconds = object.longValue();
                    long minutes = TimeUnit.SECONDS.toMinutes(seconds);
                    long remainingseconds = seconds - TimeUnit.MINUTES.toSeconds(minutes);
                    return String.format("%02d", minutes) + ":" + String.format("%02d", remainingseconds);
                }

                @Override
                public Double fromString(String string) {
                    return null;
                }
            };

            sl.setLabelFormatter(stringConverter);

            Text text = new Text();

            sl.valueProperty().addListener((observable, oldValue, newValue) ->
                    text.setText(stringConverter.toString(newValue.doubleValue())));

            root.getChildren().addAll(sl, text);

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

这篇关于如何将JavaFX Slider设置为格式化时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆