JavaFX中的集合

时间:2015-01-26 19:29:53

标签: arraylist collections javafx

有人可以解释我如何在app运行期间维护数组列表中的数据。假设我们有三个班级。主类运行应用程序,按钮“保存”将数据添加到声明为私有数组列表的第二个类中。然后当我们在数组列表中添加数据时,我们单击“下一个场景”按钮。当打开第二个阶段,即第三个阶段时,有一个按钮“Show”,它应该显示在第一个类中添加的数据,但是它引发了数组列表为空的错误。

代码:

头等舱

 public void start(Stage primaryStage) {
       final Button btn = new Button();
            btn.setLayoutX(121);
        btn.setLayoutY(125);
        btn.setText("Save");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                array.insert(20);
            }
        });

        final Button btn_next = new Button();
        btn_next.setLayoutX(btn.getLayoutY());
        btn_next.setLayoutY(btn.getLayoutX() + 54);
        btn_next.setText("Next Scene");
        btn_next.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {


                secondStage.start();

            }
        });

        Pane root = new Pane();
        root.getChildren().add(btn);
        root.getChildren().add(btn_next);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Exercise");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

第二课

public class ArrayListClass {

    private ArrayList <Integer> array = new ArrayList<>();
    private ObservableList  name_obs = FXCollections.observableArrayList(array);



    public void insert(int maxBound) {

      for(int i = 0; i < maxBound; i++) {

          name_obs.add(i);
      }

    }


    public Integer show(int i) {


       return (Integer) name_obs.get(i);

    }

}

第三课

public class SecondStage {

    private Stage secondaryStage = new Stage();
    ArrayListClass array = new ArrayListClass();


    public void start() {


        Button btn= new Button();
        btn.setText("Show");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                System.out.println(array.show(5));
                }
            }
        );

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        secondaryStage.setTitle("Exercise");
        secondaryStage.setScene(scene);
        secondaryStage.show();
    }


}

ERROR:

Executing /Users/Jenda/NetBeansProjects/ExerciseArrayList/dist/run542796410/ExerciseArrayList.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:89)
    at exercisearraylist.ArrayListClass.show(ArrayListClass.java:36)
    at exercisearraylist.SecondStage$1.handle(SecondStage.java:36)
    at exercisearraylist.SecondStage$1.handle(SecondStage.java:31)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8216)
    at javafx.scene.control.Button.fire(Button.java:185)

0 个答案:

没有答案