大家好,
我是 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
请帮忙:(
帮助将不胜感激!
最佳答案
错误消息非常清楚:您正在调用方法 setConstraints(...)
并为 rowSpan
和 colSpan
传入 0。错误消息表明您无法将 rowSpan 设置为零。 (您大概也不能将 colSpan
设置为零。)这些限制似乎是有道理的。
关于java - 如何使用Javafx将TextField设置在顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25689277/