我无法找到此错误的解决方案,但我可能发现原因是什么。 我想在VBox中添加多个项目:
VBox vbox = new VBox();
vbox.getChildren().addAll(ohlcBtn, labelOHLC, tradesBtn, labelTRADES);
return vbox;
如果我只添加前两项,一切正常,但java无法访问第三项 (可能这就是为什么有java.lang.IndexOutOfBoundsException:索引:3,大小:2)
发生错误primaryStage.show();
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.javafx.main.Main.launchApp(Main.java:642)
at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:208)
at javafx.scene.Parent.impl_processCSS(Parent.java:1151)
at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
at javafx.scene.Node.processCSS(Node.java:7383)
at javafx.scene.Scene.doCSSPass(Scene.java:446)
at javafx.scene.Scene.preferredSize(Scene.java:1449)
at javafx.scene.Scene.impl_preferredSize(Scene.java:1516)
at javafx.stage.Window$9.invalidated(Window.java:716)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161)
at javafx.stage.Window.setShowing(Window.java:779)
at javafx.stage.Window.show(Window.java:794)
at javafx.stage.Stage.show(Stage.java:229)
at javamt4interface.JavaMT4Interface.start(JavaMT4Interface.java:58)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
... 1 more
Java Result: 1
我已将相关方法放在此处:
public class JavaMT4Interface extends Application {
final InputOrganizer io = new InputOrganizer();
private Scene scene;
@Override
public void start(Stage primaryStage) {
Group root = new Group();
primaryStage.setTitle("The Forex Project");
scene = new Scene(root, 500, 250);
primaryStage.setScene(scene);
BorderPane borderPane = new BorderPane();
borderPane.setTop(getTopContent());
borderPane.setLeft(getLeftContent());
root.getChildren().add(borderPane);
primaryStage.show();
}
private Label labelOHLC;
private Label labelTRADES;
public Node getTopContent() {
Button ohlcBtn = new Button();
ohlcBtn.setId("ohlcFileChooser");
ohlcBtn.setText("Choose OHLC File");
labelOHLC = new Label("", ohlcBtn);
labelOHLC.setId("ohlcFileChosen");
labelOHLC.setContentDisplay(ContentDisplay.LEFT);
ohlcBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileChooser fc = new FileChooser();
File userDirectory = getThisDirectory();
fc.setInitialDirectory(userDirectory);
fc.setTitle("ohlcFile");
File ohlcFile = fc.showOpenDialog(new Popup());
if (ohlcFile != null) {
io.setOhlcFile(ohlcFile);
labelOHLC.setText(ohlcFile.getAbsolutePath());
} else {
showDialog("No file selected.");
}
}
});
Button tradesBtn = new Button();
tradesBtn.setId("tradesFileChooser");
tradesBtn.setText("Choose Trade File");
labelTRADES = new Label("", tradesBtn);
labelTRADES.setId("ohlcFileChosen");
labelTRADES.setContentDisplay(ContentDisplay.LEFT);
tradesBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileChooser fc = new FileChooser();
File userDirectory = getThisDirectory();
if (!userDirectory.canRead()) {
userDirectory = new File("c:/");
}
fc.setInitialDirectory(userDirectory);
fc.setTitle("Trade File");
File tradeFile = fc.showOpenDialog(new Popup());
if (tradeFile != null) {
io.setTradesFile(tradeFile);
labelTRADES.setText(tradeFile.getAbsolutePath());
} else {
showDialog("No file selected.");
}
}
});
VBox vbox = new VBox();
vbox.getChildren().addAll(ohlcBtn, labelOHLC, tradesBtn, labelTRADES);
return vbox;
}
}
感谢您的帮助! :)
答案 0 :(得分:0)
您正在使用标签和按钮做一些奇怪的事情。您将按钮设置为标签的图形(因此它们将显示在标签中),然后将它们添加到场景图中,标签为:
labelOHLC = new Label("", ohlcBtn);
和
labelTRADES = new Label("", tradesBtn);
以后
vbox.getChildren().addAll(ohlcBtn, labelOHLC, tradesBtn, labelTRADES);
这种方式将按钮添加到场景图中两次,我认为这会让布局混乱。 (它确实应该生成IllegalStateException
或IllegalArgumentException
,但我想您已经找到了未正确检测到的错误配置。)
尝试制作它
labelOHLC = new Label("");
和
labelTRADES = new Label("");