2014-09-05 24 views
0

HiEveryone,如何使用Javafx在顶部设置TextField?

我是一个初学者在Javafx,试图把TextField顶部和我使用GridPane。所以我想这个代码,但是这给我的错误:

的源代码:

package sample; 

import javafx.application.Application; 
import javafx.geometry.HPos; 
import javafx.geometry.VPos; 
import javafx.scene.Scene; 
import javafx.scene.layout.GridPane; 
import javafx.scene.web.WebEngine; 
import javafx.scene.web.WebView; 
import javafx.stage.Stage; 
import javafx.scene.control.TextField; 
import javafx.geometry.HPos; 

public class Main extends Application { 
     @Override 
     public void start (Stage primaryStage){ 
     primaryStage.setTitle("Modern web browser made by Rajendra Arora using JavaFX."); 
     WebView wv = new WebView(); 
     WebEngine we = wv.getEngine(); 
     we.load("http://www.google.com/"); 

     TextField tf = new TextField("http://www.google.com/"); 
     tf.setMaxHeight(Double.MAX_VALUE); 
     tf.positionCaret(25); 
     GridPane sp = new GridPane(); 
     sp.getChildren().add(wv); 
     GridPane.setConstraints(tf, 1, 0, 0, 0, HPos.CENTER, VPos.CENTER); 

     Scene scene = new Scene(sp, 600, 500); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
     } 

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

但是,让这样的错误:

Exception in Application start method 
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894) 
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56) 
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158) 
    at java.lang.Thread.run(Thread.java:744) 
Caused by: java.lang.IllegalArgumentException: rowSpan must be greater or equal to 1, but was 0 
    at javafx.scene.layout.GridPane.setRowSpan(GridPane.java:346) 
    at javafx.scene.layout.GridPane.setConstraints(GridPane.java:591) 
    at sample.Main.start(Main.java:27) 
    at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837) 
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335) 
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301) 
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39) 
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112) 
    ... 1 more 

请帮助:(

帮助将不胜感激!

+0

请显示哪条线路正在产生异常(即哪条线路是27号线路)。 – 2014-09-05 15:33:38

+0

在'GridPane.setConstraints(tf,1,0,0,0,HPos.CENTER,VPos.CENTER)显示错误;' – 2014-09-05 15:36:01

回答

0

错误消息非常明显:您正在调用方法setConstraints(...)并在rowSpancolSpan之间传入0。错误消息表示您不能将rowSpan设置为零。 (你大概不能设置colSpan为零)。这些限制似乎是有道理的。

+0

好吧..你说得对:) :) ..但是我想知道更多事情..我无法看到帧内的TextField ..你能帮我吗.. – 2014-09-05 15:45:00

+0

你忘了将文本字段添加到网格窗格。 – 2014-09-05 15:52:59

+0

但我可以在这里看到'tf' ...'GridPane.setConstraints(tf,1,0,0,0,HPos.CENTER,VPos.CENTER);' – 2014-09-05 15:56:57