使用javaFX在不同的控制器上传递arraylist的问题

您好,我是GUI开发/ JavaFX的新手,所以我对我的草率代码表示歉意,我的项目是一个神奇宝贝游戏的非常基本的仿真,将JavaFx与场景构建器配合使用,我正在尝试设计GUI,我制作了多个神奇宝贝对象,然后使用复选框将其添加到数组列表中,用户可以建立自己的团队,我在将数组列表从一个场景传递到另一个场景时遇到了麻烦。我一直在使用FXMLLoader来获取其他类的控制器,以这种方式访问​​列表,然后从那里我的想法是为该场景创建一个新列表,并使用以前的场景列表添加到新的场景列表中,但是当我尝试将其添加到新列表中,但不会添加任何内容,我们将不胜感激。

删除一些不必要的内容,例如某些复选框以及一些Pokemon和team2列表,因为它们可能是多余的。

建立团队的场景

public class ChoosePokemon {
    @FXML private CheckBox Check1;
    @FXML private CheckBox Check2;
    @FXML private CheckBox Check3;
    @FXML private CheckBox Check4;

public static List PokemonList1() throws IOException {
        List<Pokemon> pokemonList3 = new ArrayList<>();
        Pokemon pkmn1 = Pokemonobjects.bublbasaur();
        Pokemon pkmn2 = Pokemonobjects.squirtle();
        Pokemon pkmn3 = Pokemonobjects.charmander();
        Pokemon pkmn4 = Pokemonobjects.pikachu();
        Pokemon pkmn5 = Pokemonobjects.onix();
        pokemonList3.add(pkmn1);
        pokemonList3.add(pkmn2);
        pokemonList3.add(pkmn3);
        pokemonList3.add(pkmn4);
        pokemonList3.add(pkmn5);
}
public void Team3() throws IOException {
       //List<Pokemon> Team = new ArrayList<>();
        if(Check1.isSelected()){
            Team.add((Pokemon) PokemonList1().get(0));
        }
        if(Check2.isSelected()){
            Team.add((Pokemon) PokemonList1().get(1));
        }
        if(Check3.isSelected()){
            Team.add((Pokemon) PokemonList1().get(2));
        }
        if(Check4.isSelected()){
            Team.add((Pokemon) PokemonList1().get(3));
        }
        if(Check5.isSelected()){
            Team.add((Pokemon) PokemonList1().get(4));
        }
}
List<Pokemon> Team = new ArrayList<>();
public void BattleButtonPushed(actionEvent actionEvent) throws IOException {
        Team3();
        Team4();
        System.out.print(Team.size() + "\n");
        System.out.print(Team2.size());
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getclass().getResource("Options_Scene.fxml"));
        Parent settingsscene = loader.load();
        OptionsScene controller = loader.getcontroller();
        controller.Teams(Team,Team2);
        controller.getTeam1(Team);
        controller.getTeam2(Team2);
        Scene setwin = new Scene(settingsscene);
        Stage window = (Stage) ((Node) actionEvent.getsource()).getScene().getWindow();
        window.setScene(setwin);
        window.show();
    }
    public void Go_Back(actionEvent actionEvent) throws IOException {
            Parent settingsscene = FXMLLoader.load(Controller.class.getResource("sample.fxml"));
            Scene setwin = new Scene(settingsscene);
            Stage window = (Stage) ((Node)actionEvent.getsource()).getScene().getWindow();
            window.setScene(setwin);
            window.show();

    }
}

我正在尝试将原始数组列表移至的场景

public class OptionsScene implements Initializable {
   public ImageView image1;
   public ImageView image2;
   public List<Pokemon> selectedPokemonTeam;
   void Teams(List<Pokemon> name,List<Pokemon> name2) {
       selectedPokemonTeam = name;
      image1.setImage(name.get(0).getImagefile2());
      image2.setImage(name2.get(0).getImagefile1());
   }
   private List<Pokemon> Team3 = new ArrayList<>();
   private List<Pokemon> Team4 = new ArrayList<>();

   void getTeam1(List<Pokemon> Team) {
       //just testing to see sizes of all arrays
       System.out.print("\n"+Team.size());
       Team3 = Team;
       System.out.print("\n"+Team3.size());
       Team3.add(Team.get(0));
   }
   void getTeam2(List<Pokemon> Team2) {
       System.out.print("\n"+Team2.size());
       Team4 = Team2;
       System.out.print("\n"+Team4.size());
       Team4.add(Team2.get(0));

   }
   public void AttackButtonPushed(actionEvent actionEvent) throws IOException {
       FXMLLoader loader2 = new FXMLLoader();
       loader2.setLocation(getclass().getResource("Choose_Attack_Scene.fxml"));
       Parent settingsscene = loader2.load();
       ChooseAttackScene controller2 = loader2.getcontroller();
       controller2.Team2(Team3,Team4);
       controller2.getTeam1(Team3);
       controller2.getTeam2(Team4);
       Scene setwin2 = new Scene(settingsscene);
       Stage window2 = (Stage) ((Node)actionEvent.getsource()).getScene().getWindow();
       window2.setScene(setwin2);
       window2.show();
   }

   public void BagButtonPushed(actionEvent actionEvent) throws IOException { Main test = new Main();test.Bag();}

   public void PokemonButtonPushed(actionEvent actionEvent) throws IOException{}

   public void RunButtonPushed(actionEvent actionEvent) throws IOException{}

   @Override
   public void initialize(URL location,ResourceBundle resources){System.out.print("\n"+Team3.size());System.out.print("\n"+Team4.size());}
}
wssfeng8287 回答:使用javaFX在不同的控制器上传递arraylist的问题

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3000471.html

大家都在问