JavaFX保存实例状态

时间:2018-08-23 15:58:16

标签: java javafx

我正在使用JavaFX开发应用程序。在应用程序中,我通过单击另一个场景中的按钮来切换到另一个场景。我想做的是回到第一个场景时,希望它保留分配给控件的所有属性。是否可以保存实例状态,然后在需要时像在android中一样加载它?

这是Main Scene控制器:

/etc/apt/apt.conf.d/20auto-upgrades

}

这是第二个:

public class MainController {

@FXML
Button go;
@FXML
TextField summName;
@FXML
public ImageViewController champ1;
@FXML
public ImageViewController champ2;
@FXML
public ImageViewController champ3;
@FXML
public ImageViewController champ4;
@FXML
public ImageViewController champ5;
@FXML
public ImageViewController champ6;
@FXML
public ImageViewController champ7;
@FXML
public ImageViewController champ8;
@FXML
public ImageViewController champ9;
@FXML
public ImageViewController champ10;
@FXML
public GridPane gridPane;
@FXML
public ProgressBar progress;

public static Participant participant;
public static boolean isLoaded = false;
private CurrentGameInfo game;
public static DownloadTask downloadTask;

public void initialize() throws URISyntaxException {
    BackgroundImage backgroundImage = new BackgroundImage(new Image(MainController.class.getClassLoader().getResource("img/bg.png").toURI().toString()), null, null, null, new BackgroundSize(1920, 1080, false, false, false, false));
    Background background = new Background(backgroundImage);
    gridPane.setBackground(background);

}

@FXML
public void onAction() throws IOException {
    downloadTask = new DownloadTask();

    progress.progressProperty().unbind();
    progress.progressProperty().bind(downloadTask.progressProperty());
    new Thread(downloadTask).start();

    downloadTask.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, (event) -> {
        progress.progressProperty().unbind();
        progress.setProgress(100);
    });
}

private void changeScene() throws IOException {

    LolAppApplication.mainStage.getScene().setRoot(FXMLLoader.load(getClass().getClassLoader().getResource("secondscene.fxml")));
}

private class DownloadTask extends Task<Void> {

    @Override
    protected Void call() throws Exception {
        if (!isLoaded) {
            game = CurrentGameInfoUtils.getCurrentGameInfo(summName.getText(), Region.TR);
            isLoaded = true;
        }
        List<Participant> participants = game.getCurrentGameParticipants();
        List<Image> imagesTeam1 = new ArrayList<>();
        List<Image> imagesTeam2 = new ArrayList<>();
        List<Champion> champions = new ArrayList<>();
        List<String> participantNames = new ArrayList<>();

        for (Participant p1 : participants) {
            System.out.println(ChampionUtils.getChampion((int) p1.getChampionId()).getName());
            List<Long> perkIds = p1.getPerks().getPerkIds();
            perkIds.forEach((perkId) -> {
                try {
                    Rune rune = RuneUtils.getRune(Integer.valueOf(String.valueOf(perkId)));
                    System.out.println(rune.getName() + " " + rune.getRunePathName() + " " + rune.getShortDesc());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            System.out.println(p1.getPerks().getPerkStyle());
            System.out.println(p1.getPerks().getPerkSubStyle());
            System.out.println(SpellUtils.getSpell((int) p1.getSpell1Id()).getName());
            System.out.println(SpellUtils.getSpell((int) p1.getSpell2Id()).getName());
            System.out.println(p1.getSummonerName());

            System.out.println(ChampionUtils.getChampion((int) p1.getChampionId()).getSplash());
            File file = new File(ChampionUtils.getChampion((int) p1.getChampionId()).getSplash());
            Image image = new Image(file.toURI().toString());
            if (p1.getTeamId() == 100)
                imagesTeam1.add(image);
            else
                imagesTeam2.add(image);
            champions.add(ChampionUtils.getChampion((int) p1.getChampionId()));
            participantNames.add(p1.getSummonerName());
        }

        champ1.setImage(imagesTeam1.get(0));
        champ2.setImage(imagesTeam1.get(1));
        champ3.setImage(imagesTeam1.get(2));
        champ4.setImage(imagesTeam1.get(3));
        champ5.setImage(imagesTeam1.get(4));
        champ6.setImage(imagesTeam2.get(0));
        champ7.setImage(imagesTeam2.get(1));
        champ8.setImage(imagesTeam2.get(2));
        champ9.setImage(imagesTeam2.get(3));
        champ10.setImage(imagesTeam2.get(4));

        champ1.setParticipant(participants.get(0));
        champ2.setParticipant(participants.get(1));
        champ3.setParticipant(participants.get(2));
        champ4.setParticipant(participants.get(3));
        champ5.setParticipant(participants.get(4));
        champ6.setParticipant(participants.get(5));
        champ7.setParticipant(participants.get(6));
        champ8.setParticipant(participants.get(7));
        champ9.setParticipant(participants.get(8));
        champ10.setParticipant(participants.get(9));

        champ1.setOnMouseClicked((event -> {
            try {
                participant = champ1.getParticipant();
                changeScene();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }));
        champ2.setOnMouseClicked((event -> {

        }));
        champ3.setOnMouseClicked((event -> {

        }));
        champ4.setOnMouseClicked((event -> {

        }));
        champ5.setOnMouseClicked((event -> {

        }));
        champ6.setOnMouseClicked((event -> {

        }));
        champ7.setOnMouseClicked((event -> {

        }));
        champ8.setOnMouseClicked((event -> {

        }));
        champ9.setOnMouseClicked((event -> {

        }));
        champ10.setOnMouseClicked((event -> {

        }));


        return null;
    }
}

}

1 个答案:

答案 0 :(得分:0)

您可以将这些值存储在类似Bundle的对象中,您可以将其命名为MyBundle或其他名称,然后将其命名为public singleton,以便检查它是否具有每次访问MainController的值(如果不是null的值,则加载它们而不获取它们并将其存储在MyBundle
中) 我以前在一个项目中做过,这是一个好习惯