2015-10-06 26 views
1

我在多個系統配置(Windows 7,Windows 8和Windows 10)上測試我的應用程序(JavaFX Application with embedded JDK 8:8u66 ea-b02)。JavaFX 8:類微調不想實例化

在三臺PC的測試中使用Windows 7 64位,我遇到了一個很奇怪的問題。

我的應用程序在2臺電腦上正常工作,不在第三臺!

在追蹤問題(在第三臺PC上)後,當我嘗試實例化自定義的Spinner類時,應用程序停止運行。

我沒有日誌!應用程序會立即停止並退出。

這是(基於生成器模式)我的類的實例:

​​

和我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

將更新「8u40」添加到JavaFX中。如果您的設備上安裝的JavaFX版本比此舊,則IDE將無法識別JavaFX中的Spinner類。在這種情況下,您需要更新您的JDK。 –

+0

我的JavaFX應用程序中嵌入的JDK是8u66 JDK,所以沒問題! –

+0

具有嵌入式JDK的優勢在於,它不依賴於設備上安裝的任何JRE –

回答

0

我發現了這個問題。

事實上,我在這臺PC上安裝了兩個版本的應用程序。

我忘了卸載舊版本(非常舊的版本!)。這個舊版本嵌入了JDK 8u20(Spinner Class的< 8u40)。

我認爲這兩個應用程序(使用兩個不同版本的JDK)之間存在混淆的系統行爲。

所以,我卸載舊版本,然後我安裝了新版本:沒關係!