如何从if语句更新标签currentPlayerFileLabel
的文本,以便从文件中获取路径(如果它在那里);否则获取默认的字符串?
截至目前的代码:
package sample;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import sample.controllers.Util;
import sample.model.Config;
import java.io.File;
import java.net.URL;
import java.security.spec.ECField;
import java.util.ResourceBundle;
public class Main extends Application implements Initializable {
private Stage primaryStage = new Stage();
private BorderPane rootLayout;
private AnchorPane anchorPanePlayer;
private BorderPane borderPaneGame;
private Config config = new Config();
private StringProperty isPlayerFileThere = new SimpleStringProperty("No playerfile was fund! Add new one in \"File\"");
@FXML
private Button playersButton;
@FXML
private Button gamesButton;
@FXML
private Button quitButton;
@FXML
private Label currentPlayerFileLabel = new Label();
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Main window");
initLayout();
initConfig();
}
//----------- MY QUESTION IS ABOUT THIS METHODE -------------------------------
public void initConfig() {
File configFile = new File(System.getProperty("user.dir") + "/Config/config.txt");
if (configFile.exists()) { // Returns true as of now, so the "true" statement of the if statement will be called
config = Util.initConfigFile();
isPlayerFileThere.setValue(config.getPlayerFileLocation().toString());
currentPlayerFileLabel.setText(getIsPlayerFileThere());
} else {
currentPlayerFileLabel.setText(getIsPlayerFileThere());
}
}
//----------- MY QUESTION IS ABOUT THIS METHODE -------------------------------
public void initLayout() {
try {
//Load root layout from fxml
FXMLLoader loader = new FXMLLoader(); //Makes a new FXMLLoader
loader.setLocation(Main.class.getResource("view/mainView.fxml")); //sets the location of the main fxml file
rootLayout = loader.load(); //Loads the anchorpane from the loader, (AnchorPane) is redundent.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.getStackTrace();
}
}
public void initPlayerLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/playerEdit.fxml")); // Gets the new layout.
anchorPanePlayer = loader.load(); //Loades the new layout
Scene playerScene = new Scene(anchorPanePlayer); //adds a new scene with the loaded layout
Stage prevStage = (Stage) playersButton.getScene().getWindow(); //Get the stage from where we come from.
prevStage.close(); //Closes the prev stage
primaryStage.setScene(playerScene); //Sets new stage with the new layout
primaryStage.show(); //Shows new stage
} catch (Exception e) {
e.printStackTrace();
}
}
public void initGameLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/editGame.fxml"));
borderPaneGame = loader.load();
Scene gameScene = new Scene(borderPaneGame);
Stage prevStage = (Stage) gamesButton.getScene().getWindow();
prevStage.close();
primaryStage.setScene(gameScene);
primaryStage.show();
} catch (Exception e) {
e.getStackTrace();
}
}
public void quitProgram() {
Stage stageToQuit = (Stage) quitButton.getScene().getWindow();
stageToQuit.close();
}
public Stage getPrimaryStage() {
return primaryStage;
}
public AnchorPane getBorderPanePlayer() {
return anchorPanePlayer;
}
public Config getConfig() {
return config;
}
public void addPlayerFile() {
config.setPlayerFileLocation(Util.addPlayerFile());
}
public String getIsPlayerFileThere() {
return isPlayerFileThere.get();
}
public StringProperty isPlayerFileThereProperty() {
return isPlayerFileThere;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
currentPlayerFileLabel.setText(getIsPlayerFileThere());
}
public static void main(String[] args) {
launch(args);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Text?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">
<bottom>
<HBox prefHeight="100.0" prefWidth="200.0" spacing="40.0" BorderPane.alignment="CENTER">
<children>
<Region HBox.hgrow="ALWAYS" />
<Button fx:id="playersButton" mnemonicParsing="false" onAction="#initPlayerLayout" text="Players" />
<Button fx:id="gamesButton" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#initGameLayout" text="Games" />
<Button fx:id="quitButton" layoutX="69.0" layoutY="10.0" mnemonicParsing="false" onAction="#quitProgram" text="Quit" />
<Region layoutX="10.0" layoutY="10.0" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</bottom>
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="addPlayerFileMenu" mnemonicParsing="false" onAction="#addPlayerFile" text="Add new player file" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER_RIGHT">
<children>
<VBox alignment="CENTER" HBox.hgrow="ALWAYS">
<children>
<Label text="Welcom to the main menu!" />
<Label fx:id="currentPlayerFileLabel" text="Label" />
</children>
</VBox>
</children>
</HBox>
</center>
</BorderPane>
更新在此问题的评论的帮助下,现在已经知道如何做到这一点。对于有同样问题的其他人,这是我开始工作的代码:
主要课程:
package sample;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import sample.controllers.MainController;
import sample.controllers.Util;
import sample.model.Config;
import java.io.File;
import java.net.URL;
import java.security.spec.ECField;
import java.util.ResourceBundle;
public class Main extends Application {
MainController mainController = new MainController();
Stage primaryStage;
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/mainView.fxml"));
loader.setController(mainController);
Parent root = loader.load();
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
主窗口布局的控制器和有趣的部分:
...
private Stage primaryStage = new Stage();
private AnchorPane anchorPanePlayer;
private BorderPane borderPaneGame;
private Config config = new Config();
private StringProperty isPlayerFileThere = new SimpleStringProperty("No playerfile was fund! Add new one in \"File\"");
@FXML
private Button playersButton;
@FXML
private Button gamesButton;
@FXML
private Button quitButton;
@FXML
private Label currentPlayerFileLabel;
...
@Override
public void initialize(URL location, ResourceBundle resources) {
File configFile = new File(System.getProperty("user.dir") + "/Config/config.txt");
if (configFile.exists()) { // Returns true as of now, so the "true" statement of the if statement will be called
config = Util.initConfigFile();
isPlayerFileThere.setValue(config.getPlayerFileLocation().toString());
currentPlayerFileLabel.setText(getIsPlayerFileThere());
} else {
currentPlayerFileLabel.setText(getIsPlayerFileThere());
}
}
...
已更新的FXML行:
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" >
答案 0 :(得分:1)
您正在覆盖标签,因此您不再使用FXML中定义的实例:
@FXML
private Label currentPlayerFileLabel = new Label();
删除新标签的分配。