2016-12-27 70 views
0

我想按下按钮时更新线程(ScheduledService)中文本的值。我可以在GUI中更新文本的值,但该值无法在线程(ScheduledService)中更新。如何更新线程中事件处理程序的值?

我该如何更新线程(ScheduledService)区域中的文本值?

[Procesure]

(1)当我输入文本的值和按下按钮时,该值显示在GUI 通过下面的代码(在事件处理程序)

label.setText(text); 
    Value = Integer.parseInt(text); 

(2 )我想通过“值”的值由下面的

recieve(Value); 

(3)中的“值”是由下面的代码所示值线程

System.out.println(a); 

但是,“值”的值不更新,它仍然是“0”。 “0”的值是“值”的初始值。

public Integer Value = 0; 

我的代码如下:

package javafxapplication16; 

import java.io.IOException; 
import javafx.application.Application; 
import javafx.concurrent.ScheduledService; 
import javafx.concurrent.Task; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ButtonBuilder; 
import javafx.scene.control.Label; 
import javafx.scene.control.LabelBuilder; 
import javafx.scene.control.TextField; 
import javafx.scene.control.TextFieldBuilder; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.HBoxBuilder; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontPosture; 
import javafx.stage.Stage; 
import javafx.stage.StageBuilder; 


public class JavaFXApplication16 extends Application { 

    public Label  label; 
    public Integer Value = 0; 
    public Button bt_co; 
    public TextField tx; 

    @Override 
    public void start(Stage stage) throws Exception{ 


     tx = TextFieldBuilder.create().text("").build(); 

     Font font = Font.font("Arial",FontPosture.ITALIC,20); 
       label = LabelBuilder.create().text("value") 
          .alignment(Pos.CENTER) 
          .font(font) 
          .prefWidth(200).build(); 


     bt_co = ButtonBuilder.create().text("") 
       .prefWidth(200) 
       .alignment(Pos.CENTER) 
       .id("") 
       .build(); 
     HBox root = HBoxBuilder.create().spacing(100).children(tx,label,bt_co).build(); 

     Scene scene = new Scene(root); 

     scene.addEventHandler(ActionEvent.ACTION,actionHandler); 

     recieve(Value); // pass the value of "Value" to thread 

     stage = StageBuilder.create().width(640).height(640).scene(scene).title(" ").build(); 
     stage.show(); 
    } 

    /* thread */ 
    private void recieve(int a) throws IOException { 

     ScheduledService<Boolean> ss = new ScheduledService<Boolean>() 
     { 
      @Override 
      protected Task<Boolean> createTask() 
      { 

       Task<Boolean> task = new Task<Boolean>() 
       { 
        @Override 
        protected Boolean call() throws Exception 
        { 
         System.out.println(a); 
         return true; 
        }; 
       }; 

       return task;   
      } 

     }; 
     ss.start(); 
    } 

/* Event Handler */ 
    EventHandler<ActionEvent> actionHandler = new EventHandler<ActionEvent>(){ 
     public void handle (ActionEvent e){ 


      Button src =(Button)e.getTarget(); 

       String text = tx.getText(); 

       label.setText(text); 

       Value = Integer.parseInt(text); 

     } 
    }; 


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


} 

回答

1

参数值计算方法被调用之前的权利。这是通过的值,而不是某些可能写入表达式中作为函数参数的字段中的值。

这意味着什么情况是

  1. UI是构建。
  2. 事件处理被添加
  3. receive被执行并当前存储在Value字段中的值作为参数,这是0此时通过。
  4. ScheduledService打印0反复)
  5. 在一段时间内被执行写一个新值Value
  6. 事件处理(ScheduledService继续打印0,因为这是a值)

对于任何不同的行为,您需要在任务内使用实际更改其值的表达式,例如

volatile Integer Value; 

... 

receive(); 

... 

private void recieve() /* throws IOException */ { 

    ScheduledService<Boolean> ss = new ScheduledService<Boolean>() 
    { 
     @Override 
     protected Task<Boolean> createTask() 
     { 

      Task<Boolean> task = new Task<Boolean>() 
      { 
       @Override 
       protected Boolean call() throws Exception 
       { 
        System.out.println(a); 
        return true; 
       }; 
      }; 

      return task;   
     } 

    }; 
    ss.start(); 
} 

BTW:你应该摆脱建设者的那些用途。它们都在JavaFX 8中被弃用,甚至没有记录在javadoc中,并且可能会在JavaFX 9中被删除。

+0

非常感谢! – kkj