我在多个系统配置(Windows 7,Windows 8和Windows 10)上测试我的应用程序(带有嵌入式JDK 8的JavaFX应用程序:8u66 ea-b02)。
在使用 Windows 7 64位的三台PC上进行的测试中,我遇到了一个非常奇怪的问题。
我的应用程序在2台PC上正常工作,而不是第三台!
在跟踪问题后(在第三台PC上),当我尝试实例化我自定义的Spinner类时,应用程序停止运行。
我没有日志!应用程序停止并立即退出。
这是我的类的实例化(基于构建器模式):
CustomSpinner defaultSpinner1 = new CustomSpinner.CustomSpinnerBuilder(
"[Compteur]", 1, CustomSpinner.MAX_VALUE).titleWidth(110)
.height(30).build();
和我的CustomSpinner类的声明:
public class CustomSpinner extends javafx.scene.control.Spinner<Integer> {
private static Logger logger = Logger.getLogger(CustomSpinner.class);
public static final int MAX_VALUE = 999999;
private String title; // obligatoire
private int minValue; // obligatoire
private int maxValue; // obligatoire
private int titleWidth; // optionnel
private int width; // optionnel
private LocalTime previousTime;
private LocalTime previousTimeBS;
private LocalTime previousTimeD;
private double height; // optionnel
public CustomSpinner(CustomSpinnerBuilder builder) {
super();
previousTime = null;
previousTimeBS = null;
previousTimeD = null;
title = builder.title;
minValue = builder.minValue;
maxValue = builder.maxValue;
titleWidth = builder.titleWidth;
width = builder.width;
height = builder.height;
this.setMinWidth(width);
this.setMaxWidth(width);
this.setPrefWidth(width);
this.setMinHeight(height);
this.setMaxHeight(height);
this.setPrefHeight(height);
this.setValueFactory(new javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory(
minValue, maxValue));
this.setEditable(true);
DropShadow ds = new DropShadow();
ds.setOffsetY(3.0);
ds.setOffsetX(3.0);
ds.setColor(Color.GRAY);
this.setEffect(ds);
// filtre numérique
this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (!(event.getCharacter().charAt(0) >= '0' && event
.getCharacter().charAt(0) <= '9'))
event.consume();
else {
// utilisé pour contourner bug lorsqu'on utilise d'abord les
// arrows + saisie dans zone -> la valeur saisie est doublée
// solution : il faut au moins 1 seconde entre 2 appels de
// handle()
LocalTime currentTime = LocalTime.now();
if (previousTime == null
|| currentTime.toSecondOfDay() != previousTime
.toSecondOfDay()) {
// prise en compte valeur saisie
javafx.scene.control.SpinnerValueFactory<Integer> valueFactory = getValueFactory();
String cpt = valueFactory.getValue().toString()
+ event.getCharacter().charAt(0);
event.consume();
valueFactory.setValue(Integer.parseInt(cpt));
// positionner le curseur à la dernière position de la
// zone
getEditor().positionCaret(
valueFactory.getValue().toString().length());
previousTime = currentTime;
} else
event.consume();
}
}
});
// touches DELETE, BACKSPACE
this.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.DELETE) {
LocalTime currentTime = LocalTime.now();
if (previousTimeD != null)
logger.debug("delete = " + currentTime.toSecondOfDay()
+ " " + previousTimeD.toSecondOfDay());
else
logger.debug("delete = " + currentTime.toSecondOfDay());
if (previousTimeD == null
|| currentTime.toSecondOfDay() != previousTimeD
.toSecondOfDay()) {
// supprimer caractère
int pCaret = getEditor().caretPositionProperty()
.intValue();
javafx.scene.control.SpinnerValueFactory<Integer> valueFactory = getValueFactory();
String newCpt = null;
try {
newCpt = valueFactory.getValue().toString()
.substring(0, pCaret)
+ valueFactory.getValue().toString()
.substring(pCaret + 1);
} catch (StringIndexOutOfBoundsException exception) {
newCpt = valueFactory.getValue().toString()
.substring(0, pCaret);
}
if (newCpt.equals(""))
newCpt = minValue + "";
if (Integer.parseInt(newCpt) > maxValue)
newCpt = maxValue + "";
valueFactory.setValue(Integer.parseInt(newCpt));
event.consume();
// positionner le curseur
getEditor().positionCaret(pCaret);
previousTimeD = currentTime;
} else
event.consume();
}
if (event.getCode() == KeyCode.BACK_SPACE) {
LocalTime currentTime = LocalTime.now();
if (previousTimeBS != null)
logger.debug("backspace = "
+ currentTime.toSecondOfDay() + " "
+ previousTimeBS.toSecondOfDay());
else
logger.debug("backspace = "
+ currentTime.toSecondOfDay());
if (previousTimeBS == null
|| currentTime.toSecondOfDay() != previousTimeBS
.toSecondOfDay()) {
// supprimer caractère
int pCaret = getEditor().caretPositionProperty()
.intValue();
if ((pCaret - 1) < 0)
pCaret = 0;
else
pCaret--;
javafx.scene.control.SpinnerValueFactory<Integer> valueFactory = getValueFactory();
String newCpt = valueFactory.getValue().toString()
.substring(0, pCaret)
+ valueFactory
.getValue()
.toString()
.substring(
getEditor()
.caretPositionProperty()
.intValue());
if (newCpt.equals(""))
newCpt = minValue + "";
if (Integer.parseInt(newCpt) > maxValue)
newCpt = maxValue + "";
valueFactory.setValue(Integer.parseInt(newCpt));
event.consume();
// positionner le curseur
getEditor().positionCaret(pCaret);
previousTimeBS = currentTime;
} else
event.consume();
}
}
});
}
public static class CustomSpinnerBuilder {
private String title; // obligatoire
private int minValue = 1; // obligatoire
private int maxValue = MAX_VALUE; // obligatoire
private int titleWidth = 100; // optionnel
private int width = 110; // optionnel
private double height = TextField.USE_PREF_SIZE; // optionnel
public CustomSpinnerBuilder(String title, int minValue, int maxValue) {
this.title = title;
this.minValue = minValue;
this.maxValue = maxValue;
}
public CustomSpinnerBuilder height(double height) {
this.height = height;
return this;
}
public CustomSpinnerBuilder titleWidth(int width) {
this.titleWidth = width;
return this;
}
public CustomSpinnerBuilder width(int width) {
this.width = width;
return this;
}
public CustomSpinner build() {
return new CustomSpinner(this);
}
}
}
对这种奇怪行为的任何想法?
运行时没有实例化类的原因是什么(在我的JavaFX应用程序中)?
是否可以获取系统日志(试图了解问题的根源)?
提前谢谢
答案 0 :(得分:0)
我发现了问题。
事实上我在这台PC上安装了两个版本的应用程序。
我忘了卸载旧版本(非常旧的版本!)。这个旧版本嵌入了JDK 8u20(Spinner类的&lt; 8u40)。
我认为两个应用程序之间存在混淆系统行为(使用两个不同版本的JDK)。
所以,我卸载旧版本然后我安装了新版本:没关系!