2016-08-24 18 views
0

我想在javafx中創建一個cbt,如果時間流逝,我會遇到一個不知道如何自動提交表單的問題,並且可能是其中一個學生尚未完成測試。此外,我想知道如何禁用javafx中的表單如何在javafx中自動提交表單

+1

如果您向我們提供一些代碼,對我們來說更容易。 http://stackoverflow.com/help/how-to-ask –

回答

2

禁用Node可以通過簡單地調用node.setDisable(true)來完成。由於孩子也被自動禁用,只要沒有其他孩子不應該被禁用,您也可以爲要禁用的父母Node這樣做。

超時可以很容易地使用實施ScheduledExecutorService

private ScheduledExecutorService executorService; 

@Override 
public void start(Stage primaryStage) { 
    TextField tf = new TextField(); 
    Label label = new Label("Your Name: "); 
    Button submit = new Button("submit"); 
    GridPane root = new GridPane(); 
    label.setLabelFor(tf); 

    root.addRow(0, label, tf); 
    root.add(submit, 1, 1); 
    root.setPadding(new Insets(10)); 
    root.setVgap(5); 
    root.setHgap(5); 

    AtomicBoolean done = new AtomicBoolean(false); 

    executorService = Executors.newScheduledThreadPool(1); 

    // schedule timeout for execution in 10 sec 
    ScheduledFuture future = executorService.schedule(() -> { 
     if (!done.getAndSet(true)) { 
      System.out.println("timeout"); 
      Platform.runLater(() -> { 
       root.setDisable(true); 
      }); 
     } 
    }, 10, TimeUnit.SECONDS); 

    submit.setOnAction((ActionEvent event) -> { 
     if (!done.getAndSet(true)) { 
      // print result and stop timeout task 
      future.cancel(false); 
      System.out.println("Your name is " + tf.getText()); 
     } 
    }); 

    Scene scene = new Scene(root); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

@Override 
public void stop() throws Exception { 
    executorService.shutdown(); 
} 

如果你想顯示在用戶界面的時候,一個Timeline可能比不過一個ScheduledExecutorService更適合:

@Override 
public void start(Stage primaryStage) { 
    TextField tf = new TextField(); 
    Label label = new Label("Your Name:"); 
    Button submit = new Button("submit"); 
    GridPane root = new GridPane(); 
    label.setLabelFor(tf); 

    Label time = new Label("Time:"); 
    ProgressBar bar = new ProgressBar(); 
    time.setLabelFor(bar); 

    root.addRow(0, time, bar); 
    root.addRow(1, label, tf); 
    root.add(submit, 1, 2); 
    root.setPadding(new Insets(10)); 
    root.setVgap(5); 
    root.setHgap(5); 

    Timeline timeline = new Timeline(
      new KeyFrame(Duration.ZERO, new KeyValue(bar.progressProperty(), 0)), 
      new KeyFrame(Duration.seconds(10), evt -> { 
       // execute at the end of animation 
       System.out.println("timeout"); 
       root.setDisable(true); 
      }, new KeyValue(bar.progressProperty(), 1)) 
    ); 
    timeline.play(); 

    submit.setOnAction((ActionEvent event) -> { 
     // stop animation 
     timeline.pause(); 
     System.out.println("Your name is " + tf.getText()); 
    }); 

    Scene scene = new Scene(root); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
+0

如果你沒有顯示剩餘時間,'PauseTransition'會比'ScheduledExecutorService'簡單... –