2014-05-06 90 views
1

我创建文本区文本区域setScrollTop

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.TextArea; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 

public class MainApp extends Application 
{ 

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

     HBox hbox = new HBox(); 
     // Text Area 
     TextArea dataPane = new TextArea(); 
     dataPane.setScrollTop(0); 
     dataPane.setEditable(false); 
     dataPane.prefWidthProperty().bind(hbox.widthProperty()); 

     dataPane.setWrapText(true);  // New line of the text exceeds the text area 
     dataPane.setPrefRowCount(10); 
     dataPane.appendText("Computer software, or simply software, also known as computer programs"); 
     dataPane.appendText("\nis the non-tangible component of computers."); 
     dataPane.appendText("\nComputer software contrasts with computer hardware, which"); 
     dataPane.appendText("\nis the physical component of computers."); 
     dataPane.appendText("Computer hardware and software require each"); 
     dataPane.appendText("\nother and neither can be"); 
     dataPane.appendText("\nrealistically used without the other."); 
     dataPane.appendText("\nComputer software"); 

     hbox.setAlignment(Pos.CENTER); 
     hbox.setSpacing(1); 
     hbox.setPadding(new Insets(10, 0, 10, 0)); 
     hbox.getChildren().add(dataPane); 

     Scene scene = new Scene(hbox, 800, 90); 

     stage.setTitle("JavaFX and Maven"); 
     stage.setScene(scene); 
     stage.show(); 
    } 

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

} 

我想在默认情况下滑块的位置来设定在TextArea的顶部的这个简单的例子。 你能帮我解决这个问题吗?

回答

2

如果设置dataPane.setScrollTop(0);显示你的舞台后,它会工作:)

所以这样的:

@Override 
public void start(Stage stage) { 
    HBox hbox = new HBox(); 
    // Text Area 
    TextArea dataPane = new TextArea(); 

    dataPane.setEditable(false); 
    dataPane.prefWidthProperty().bind(hbox.widthProperty()); 

    dataPane.setWrapText(true);  // New line of the text exceeds the text area 
    dataPane.setPrefRowCount(10); 
    dataPane.appendText("Computer software, or simply software, also known as computer programs"); 
    dataPane.appendText("\nis the non-tangible component of computers."); 
    dataPane.appendText("\nComputer software contrasts with computer hardware, which"); 
    dataPane.appendText("\nis the physical component of computers."); 
    dataPane.appendText("Computer hardware and software require each"); 
    dataPane.appendText("\nother and neither can be"); 
    dataPane.appendText("\nrealistically used without the other."); 
    dataPane.appendText("\nComputer software"); 

    hbox.setAlignment(Pos.CENTER); 
    hbox.setSpacing(1); 
    hbox.setPadding(new Insets(10, 0, 10, 0)); 
    hbox.getChildren().add(dataPane); 

    Scene scene = new Scene(hbox, 800, 90); 

    stage.setTitle("JavaFX and Maven"); 
    stage.setScene(scene); 
    stage.show(); 

    dataPane.setScrollTop(0.0); 
}