我正在使用JavaFX开发应用程序。在应用程序中,我通过单击另一个场景中的按钮来切换到另一个场景。我想要做的是回到第一个场景时希望它保留所有分配给控件的属性。是否可以保存实例状态,然后在需要时像在android中一样加载它?
这是Main Scene控制器:
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;
}
}
}
这是第二个:
public class ScreenController {
private Participant participant;
@FXML
public ImageView profileIcon;
@FXML
public Label summonerName;
@FXML
public Label level;
@FXML
public ImageView champIcon;
@FXML
public Label masteryPoints;
@FXML
public Label masteryLevel;
@FXML
public Button goBack;
public void initialize() throws IOException, URISyntaxException {
participant = MainController.participant;
System.out.println(participant.getSummonerName());
Summoner summoner = SummonerUtils.getSummoner(participant.getSummonerName(), Region.TR);
Champion champion = ChampionUtils.getChampion((int) participant.getChampionId());
Mastery mastery = MasteryUtils.getMastery(summoner.getSummId(), champion.getId(), Region.TR);
Image profileImage = new Image(new File(summoner.getProfileIconId()).toURI().toString());
Image champImage = new Image(new File(champion.getIcon()).toURI().toString());
profileIcon.setImage(profileImage);
summonerName.setText(summoner.getSummName());
level.setText(String.valueOf(summoner.getLevel()) + " Level");
champIcon.setImage(champImage);
masteryPoints.setText(String.valueOf(mastery.getChampPoints()) + " Points");
masteryLevel.setText(String .valueOf(mastery.getChampLevel()) + " Level");
goBack.setOnMouseClicked((event -> {
try {
LolAppApplication.mainStage.getScene().setRoot(FXMLLoader.load(getClass().getClassLoader().getResource("main.fxml")));
new Thread(MainController.downloadTask).start();
} catch (IOException e) {
e.printStackTrace();
}
}));
}
}
最佳答案
您可以将这些值存储在类似Bundle
的对象中,可以将其命名为MyBundle
或其他名称,也可以将其命名为public
singleton
,这样,每次访问MainController
时,都可以检查其是否具有值。不是null
装入它们,否则将它们存储在MyBundle
中
我以前在一个项目中做过,这是一个好习惯