2015-04-04 98 views
0

我已完成大部分程序,唯一的問題是讓數字顯示在TextArea。編寫一個程序,讓用戶從圖形用戶界面輸入數字並將其顯示在文本區域中。使用LinkedList來存儲號碼。不要重複數字。添加排序,隨機播放,反向排序列表。JavaFX,文本將不會顯示在TextArea

package storedInLinkedList; 

import java.util.Collections; 
import java.util.LinkedList; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.geometry.Orientation; 
import javafx.geometry.Pos; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextArea; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.control.ScrollPane; 

public class StoredInLinkedList extends Application{ 

    TextField txt = new TextField(); 
    TextArea tArea = new TextArea(); 
    Label message = new Label("Enter a Number: "); 
    Button sort = new Button("Sort"); 
    Button shuffle = new Button("Shuffle"); 
    Button reverse = new Button("Reverse"); 
    private LinkedList<Integer> list = new LinkedList<>(); 

    @Override 
    public void start(Stage primaryStage){ 
     BorderPane bPane = new BorderPane(); 


     txt.setAlignment(Pos.TOP_RIGHT); 
     bPane.setCenter(txt); 
     bPane.setBottom(tArea); 

     HBox hBox = new HBox(message, txt); 
      bPane.setTop(hBox); 

     HBox buttons = new HBox(10); 
      buttons.getChildren().addAll(sort, shuffle, reverse); 
     bPane.setBottom(buttons); 
      buttons.setAlignment(Pos.CENTER); 
     VBox vBox = new VBox(); 
      vBox.getChildren().addAll(hBox, tArea, buttons); 

     bPane.setCenter(new ScrollPane(tArea)); 

     Scene scene = new Scene(vBox, 300,250); 
     primaryStage.setTitle("20.2_DSemmes"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     txt.setOnAction(e -> { 
      if(! list.contains(new Integer(txt.getText()))){ 
       tArea.appendText(txt.getText() + " "); 
       list.add(new Integer(txt.getText())); 
      }//end if 
     });//end action 

     sort.setOnAction(e -> { 
      Collections.sort(list); 
      display(); 
     });//end action 

     shuffle.setOnAction(e -> { 
      Collections.shuffle(list); 
      display(); 
     });//end action 

     reverse.setOnAction(e -> { 
      Collections.reverse(list); 
      display(); 
     });//end action 

    }//end stage 

    private void display() { 

     for (Integer i: list){ 
      tArea.setText(null); 
      tArea.appendText(i + " "); 
     }//end for 
    }//end display 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     launch(args); 
    }//end main 

}//end class 

回答

0

將textarea清除代碼放在for循環之外。否則,您正在清除之前附加的文本,所以textarea只有最後一個元素list

private void display() { 
    tArea.setText(""); // clear text area 
    for (Integer i: list){ 
     tArea.appendText(i + " "); 
    }//end for 
}/