我有一个Spinner,我想用它来配置任务的时间。在这种情况下,我无法使用Spinner设置数值,使用Combobox设置以小时或分钟为单位转换值:
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class MainApp extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Spinner spinner = new Spinner();
spinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 10000));
spinner.setEditable(true);
ComboBox comb = new ComboBox();
comb.getItems().addAll(
"Milliseconds",
"Seconds",
"Minutes",
"Hours",
"Days"
);
comb.getSelectionModel().selectFirst();
spinner.valueProperty().addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val)
{
String value = comb.getSelectionModel().getSelectedItem().toString();
long convertedValue = 0;
switch (Integer.parseInt(spinner.getValue().toString()))
{
case 1:
value = "Milliseconds";
convertedValue = Integer.parseInt((String) spinner.getValue());
break;
case 2:
value = "Seconds";
convertedValue = TimeUnit.SECONDS.toMillis((long) spinner.getValue());
break;
case 3:
value = "Minutes";
convertedValue = TimeUnit.MINUTES.toMillis((long) spinner.getValue());
break;
case 4:
value = "Hours";
convertedValue = TimeUnit.HOURS.toMillis((long) spinner.getValue());
break;
case 5:
value = "Days";
convertedValue = TimeUnit.DAYS.toMillis((long) spinner.getValue());
break;
}
System.out.println(">>>>> " + convertedValue);
}
});
HBox hb = new HBox();
hb.getChildren().addAll(spinner, comb);
hb.setSpacing(50);
Scene scene = new Scene(hb);
stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Spinner将始终保持以毫秒为单位的值,但根据组合框选择,我需要将例如5分钟转换为毫秒。 我该如何实现这个监听器?当然,当我有5000毫秒时,我想将它们显示为几分钟。
答案 0 :(得分:2)
这个怎么样?我猜你的代码示例中的主要错误是在switch语句中,你在那里切换微调器值而不是组合框值。 此示例更正您的问题,并在最后的System.out.println(...)中,您可以看到原始值和要打印的转换值。您可以在应用程序中进一步使用它。
编辑:我在评论中添加了提示。现在你可以输入以毫秒,秒为单位的任何时间......(记住按Enter键 - 否则事件不会被触发)然后在组合框被更改后,你改变的值被写入微调器 - 你想要放大最大值微调器允许以毫秒为单位的更高条目。 它也基于整数计算。因此,对时间的舍入调整将会丢失。
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class MainApp extends Application {
long milliSeconds;
String unitAtLastChange;
int originalValue;
@Override
public void start(Stage stage) throws Exception {
Spinner<Integer> spinner = new Spinner<>();
spinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 10000));
spinner.setEditable(true);
ComboBox<String> comb = new ComboBox<>();
comb.getItems().addAll("Milliseconds", "Seconds", "Minutes", "Hours", "Days");
comb.getSelectionModel().selectFirst();
this.unitAtLastChange = comb.getSelectionModel().getSelectedItem();
spinner.valueProperty().addListener(new ChangeListener<Integer>() {
@Override
public void changed(ObservableValue<? extends Integer> ov, Integer old_val, Integer new_val) {
MainApp.this.unitAtLastChange = comb.getSelectionModel().getSelectedItem();
MainApp.this.originalValue = new_val.intValue();
switch (MainApp.this.unitAtLastChange) {
case "Milliseconds":
MainApp.this.milliSeconds = new_val.intValue();
break;
case "Seconds":
MainApp.this.milliSeconds = TimeUnit.SECONDS.toMillis(new_val.intValue());
break;
case "Minutes":
MainApp.this.milliSeconds = TimeUnit.MINUTES.toMillis(new_val.intValue());
break;
case "Hours":
MainApp.this.milliSeconds = TimeUnit.HOURS.toMillis(new_val.intValue());
break;
case "Days":
MainApp.this.milliSeconds = TimeUnit.DAYS.toMillis(new_val.intValue());
break;
default:
throw new RuntimeException("Error - wrong value");
}
System.out.println("Converted: " + MainApp.this.originalValue + " " + MainApp.this.unitAtLastChange + " Original: "
+ MainApp.this.milliSeconds + " Milliseconds");
}
});
comb.valueProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
switch (newValue) {
case "Milliseconds":
MainApp.this.originalValue = (int) MainApp.this.milliSeconds;
break;
case "Seconds":
MainApp.this.originalValue = (int) TimeUnit.MILLISECONDS.toSeconds(MainApp.this.milliSeconds);
break;
case "Minutes":
MainApp.this.originalValue = (int) TimeUnit.MILLISECONDS.toMinutes(MainApp.this.milliSeconds);
break;
case "Hours":
MainApp.this.originalValue = (int) TimeUnit.MILLISECONDS.toHours(MainApp.this.milliSeconds);
break;
case "Days":
MainApp.this.originalValue = (int) TimeUnit.MILLISECONDS.toDays(MainApp.this.milliSeconds);
break;
default:
throw new RuntimeException("Error - wrong value");
}
spinner.getValueFactory().setValue(Integer.valueOf(MainApp.this.originalValue));
}
});
HBox hb = new HBox();
hb.getChildren().addAll(spinner, comb);
hb.setSpacing(50);
Scene scene = new Scene(hb);
stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}