2017-02-01 7 views
-1

StageでSplitPaneを使用するJavaFxには簡単なアプリケーションがあります。
SplitPaneには2つのStackPaneがあります。それぞれに1つのラベルがあります。
各ラベルのテキストを/秒ごとに更新します。どのように私は1つのスレッド(1つのタスク)でそれを行うことができますか?本当ですか?javafxのフォーム上にいくつかのラベルを更新する

たとえば、私はlabelUpのテキストを表示したい= Mapのキー、labelDw = Mapの値。

package com.javarush.test.MyTry; 
import javafx.application.*; 
import javafx.concurrent.Task; 
import javafx.geometry.Orientation; 
import javafx.scene.*; 
import javafx.scene.control.Label; 
import javafx.scene.control.SplitPane; 
import javafx.stage.*; 
import javafx.scene.layout.*; 
import java.io.*; 
import java.nio.charset.Charset; 
import java.util.HashMap; 
import java.util.Map; 


public class Main extends Application 
{ 

    Map<String, Integer> dictionary = new HashMap<>(); 

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

    public void init() 
    { 
     String filePath = "F:\\Java\\!Folder4Test\\test2.txt"; 
     try 
     { 
      BufferedReader reader = new BufferedReader(new FileReader(filePath)); 
      int cnt = 0; 
      while (reader.ready()) 
      { 
       String line = new String(reader.readLine().getBytes(), Charset.forName("UTF-8")); 
       dictionary.put(line, cnt++); 
      } 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("Не найден файл " + filePath); 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Ошибка чтения файла " + filePath); 
      e.printStackTrace(); 
     } 
    } 

    public void start(Stage myStage) 
    { 

     myStage.setTitle("JavaFXSkeleton."); 
     SplitPane sp = new SplitPane(); 
     sp.setOrientation(Orientation.VERTICAL); 
     Label labelUp = new Label("test"); 
     Label labelDw = new Label("тест"); 

     Scene scene = new Scene(sp, 800, 800); 
     myStage.setScene(scene); 

     final StackPane sp1 = new StackPane(); 
     sp1.getChildren().add(labelUp); 
     final StackPane sp2 = new StackPane(); 
     sp2.getChildren().add(labelDw); 

     sp.setOnMouseClicked(event -> { 

      Task<Void> task = new Task<Void>() 
      { 
       @Override 
       public Void call() throws Exception 
       { 
        for (Map.Entry<String, Integer> pair : dictionary.entrySet()) 
        { 
         updateMessage(pair.getKey()); 
         System.out.println(pair.getKey()); 
         Thread.sleep(250); 
        } 
        return null; 
       } 
      }; 
      task.messageProperty().addListener((obs, oldMessage, newMessage) -> labelUp.setText(newMessage)); 
      //there i want to update text on labelDw in same time 
      //task.messageProperty().addListener((a1,a2,a3)->labelDw.setText()); 
      new Thread(task).start(); 

     }); 

     sp.getItems().addAll(sp1, sp2); 
     sp.setDividerPositions(0.5f, 0.5f); 


     myStage.show(); 
    } 

    public void stop() 
    { 
     System.out.println("B теле метода stop()."); 
    } 
} 

(私の悪い英語のため申し訳ありませんが)

答えて

1

Animation APIを使用してください。例えば。タイムラインを使用して:

sp.setOnMouseClicked(event -> { 

    Task<Void> task = new Task<Void>() 
    { 
     @Override 
     public Void call() throws Exception 
     { 
      for (Map.Entry<String, Integer> pair : dictionary.entrySet()) 
      { 
       Platform.runLater(() -> { 
        labelUp.setText(pair.getKey()); 
        labelDw.setText(pair.getValue().toString()); 
       }); 
       System.out.println(pair.getKey()); 
       Thread.sleep(250); 
      } 
      return null; 
     } 
    }; 
    new Thread(task).start(); 

}); 

を、私はここでタイムラインを使用することをお勧めします:

sp.setOnMouseClicked(event -> { 

    Timeline timeline = new Timeline(); 
    int step = 250 ; 
    int millis = 0 ; 
    for (Map.Entry<String, Integer> entry : dictionary.entrySet()) { 
     timeline.getKeyFrames().add(new KeyFrame(Duration.millis(millis), e -> { 
      labelUp.setText(entry.getKey()); 
      labelDw.setText(entry.getValue().toString()); 
     })); 
     millis += step ; 
    } 
    timeline.play(); 
}); 

あなたがスレッドを使用する場合は、あなたがそうすることができます。

+0

ありがとうございます! –

+0

今私は新しい問題がある - どのように無限ループでアニメーションをするのですか?私は辞書(地図)から単語を更新したいです –

+0

マップはどのように無限にできますか? –

関連する問題