2017-02-23 37 views
0

我想從同一文本字段中檢索多個整數,但我無法弄清楚如何去做。任何人?如何從同一文本字段獲取多個整數?

+0

您應該添加一個小例子:你是怎麼嘗試,什麼是'TextField'的內容。 – DVarga

+0

我會嘗試重述自己。我想從一個文本字段中精確搜索10個整數,然後將它們加在一起。使用for循環在控制檯中執行此操作非常簡單。但我無法弄清楚如何在FX中解決它。 –

+0

...我不知道該把我的例子放在哪裏。通過回答自己? –

回答

1

這是一個更清晰的解決方案。這會導致錯誤輸入並導致錯誤。

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

/** 
* 
* @author blj0011 
*/ 
public class JavaFXApplication37 extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     Label info = new Label("Enter some numbers. Each seperated by one space"); 

     TextField tf = new TextField(); 
     Label label = new Label(); 

     Button btn = new Button(); 
     btn.setText("Sum Numbers"); 
     btn.setOnAction(new EventHandler<ActionEvent>() {    
      @Override 
      public void handle(ActionEvent event) { 
       if(tf.getText().length() > 0)//Only run if the text field is not empty 
       { 
        String[] stringOfNumbers = tf.getText().split(" ");//The numbers must be seperated by one space. This get the string of numbers and seperates them based on the space between each numbers 

        //sum up the numbers 
        double sum = 0; 
        for(int i = 0; i < stringOfNumbers.length; i++) 
        { 
         sum = sum + Double.parseDouble(stringOfNumbers[i]);//This sums the numbers and convert the strings to doubles     
        } 
        label.setText("The sum equals: " + sum);//This prints the sum of the doubles 
       } 
      } 
     }); 

     VBox root = new VBox(); 
     root.getChildren().addAll(info, tf, label, btn);//This adds all the nodes to the VBox 

     Scene scene = new Scene(root, 300, 250);//This adds the VBox to the Scene 

     primaryStage.setTitle("Sum Numbers"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    }  
} 

我注意到這個問題,要求處理整數。在此代碼中將double更改爲int,將Double更改爲Integer。

我加入這個解決方案,因爲您的解決方案不包含按鈕

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

/** 
* 
* @author blj0011 
*/ 
public class JavaFXApplication37 extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     Label info = new Label("Enter some numbers each seperated by one space"); 
     Label label = new Label(); 

     TextField tf = new TextField(); 
     tf.textProperty().addListener((observable, oldValue, newValue) -> { 
      try 
      { 
       if(newValue.length() > 0)//Only run if the text field is not empty 
       { 
        String[] stringOfNumbers = tf.getText().split(" ");//The numbers must be seperated by one space. This get the string of numbers and seperates them based on the space between each numbers 

        //sum up the numbers 
        int sum = 0; 
        for(int i = 0; i < stringOfNumbers.length; i++) 
        { 
         sum = sum + Integer.parseInt(stringOfNumbers[i]);//This sums the numbers and convert the strings to doubles     
        } 
        label.setText("The sum equals: " + sum);//This prints the sum of the doubles 
       }  
       else 
       { 
        label.setText("");//if no text in textfield set label to empty string 
       } 
      } 
      catch(NumberFormatException ex) 
      { 
       label.setText("");//any number format error set label to empty string 
      } 
     });  

     VBox root = new VBox(); 
     root.getChildren().addAll(info, tf, label);//This adds all the nodes to the VBox 

     Scene scene = new Scene(root, 300, 250);//This adds the VBox to the Scene 

     primaryStage.setTitle("Sum Numbers"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    }  
} 
+0

我把FX和Swing混在一起。它不以同樣的方式工作。 這正是我想要的。我修改了一下代碼來使用文本區域。非常感謝你! –

+0

如果答案是解決方案,您應該接受它。 – Sedrick

相關問題