這是我在JAVAFX中的第一個項目,這是一個簡單的程序。我的問題是,當程序啓動時,FXMLLoadLogController的按鈕「loadLogButton」將收取正確的消息。但是當我在組合框中選擇另一種語言時,按鈕的設置器工作正常,但視圖不刷新。有什麼問題?查看不刷新按鈕 - Java FX
RPV:LoadLogController的函數chargeI18nValues可以正常工作,但從SolverManager調用它時,程序中不會刷新按鈕文本。
代碼:
SolverAssistant.java
public class SolverAssistant extends Application {
public static ResourceBundle messages;
public static Utils utils = new Utils();
public static SolverManager manager;
@Override
public void start(Stage stage) throws Exception {
// Load the resource bundle
this.chargeResourceBundleLanguage();
// Load Controllers
Scene scene = new Scene(new StackPane());
manager = new SolverManager(scene);
manager.showMainView();
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public void chargeResourceBundleLanguage() {
String defaultLanguage = utils.fileReader(new File("lang.txt"));
switch (defaultLanguage) {
case "en":
messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("en"));
break;
case "es":
messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("es"));
break;
case "cat":
messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("cat"));
break;
default:
messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("en"));
break;
}
}
}
SolverManager.java
public class SolverManager {
private final Scene scene;
private FXMLMainController mainController;
private FXMLLoadLogController logController;
public SolverManager(Scene scene) {
this.scene = scene;
}
public void showMainView() throws IOException {
FXMLLoader mainLoader = new FXMLLoader(getClass().getResource("FXMLMain.fxml"));
scene.setRoot((Parent) mainLoader.load());
mainController = mainLoader.<FXMLMainController>getController();
FXMLLoader loadLoader = new FXMLLoader(getClass().getResource("FXMLLoadLog.fxml"));
loadLoader.load();
logController = loadLoader.<FXMLLoadLogController>getController();
}
public void refreshI18nResources() {
mainController.chargeI18nValues();
logController.chargeI18nValues();
}
}
FXMLMainController.java
public class FXMLMainController implements Initializable {
@FXML
private ComboBox<String> comboLanguage;
@FXML
private Tab loadTab;
@FXML
private Tab editTab;
@Override
public void initialize(URL url, ResourceBundle rb) {
this.chargeI18nValues();
this.chargeLanguageComboBox(SolverAssistant.messages.getLocale());
comboLanguage.getSelectionModel().selectedItemProperty().addListener(this.languageComboBoxListener());
}
public void chargeI18nValues() {
loadTab.setText(SolverAssistant.messages.getString("LoadLog"));
editTab.setText(SolverAssistant.messages.getString("EditLog"));
}
private void chargeLanguageComboBox(Locale language) {
comboLanguage.getItems().addAll(
"Català",
"English",
"Español"
);
switch (language.getLanguage()) {
case "cat":
comboLanguage.setValue("Català");
break;
case "es":
comboLanguage.setValue("Español");
break;
case "en":
comboLanguage.setValue("English");
break;
}
}
// -------- Listeners
private ChangeListener languageComboBoxListener() {
return new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
}
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
switch ((String) newValue) {
case "Català":
SolverAssistant.messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("cat"));
SolverAssistant.utils.fileWriter("lang.txt", "cat");
break;
case "Español":
SolverAssistant.messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("es"));
SolverAssistant.utils.fileWriter("lang.txt", "es");
break;
case "English":
SolverAssistant.messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("en"));
SolverAssistant.utils.fileWriter("lang.txt", "en");
break;
}
SolverAssistant.manager.refreshI18nResources();
}
};
}
}
FXMLLoadLogController.java
public class FXMLLoadLogController implements Initializable {
@FXML
private Label logNameLabel;
@FXML
private TextArea logTextArea;
@FXML
private Button loadLogButton;
//private String logName;
private final String logName = "C:\\Users\\Daniel\\Desktop\\ahmaxsat-ls-ms_crafted-COMPLETE-1800-3500-2.log";
private String log;
@Override
public void initialize(URL url, ResourceBundle rb) {
this.chargeI18nValues();
}
public void chargeI18nValues() {
loadLogButton.setText(SolverAssistant.messages.getString("OpenNewLog"));
}
// -------- Actions
@FXML
private void openLog(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle(SolverAssistant.messages.getString("Open"));
fileChooser.setCurrentDirectory(new File("."));
// Selecting File
//if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
if (true) {
//File file = fileChooser.getSelectedFile();
log = SolverAssistant.utils.fileReader(new File(logName));
logNameLabel.setText(logName);
logTextArea.setText(log);
}
}
}
檢查'chargeI18nValues()'方法中的'SolverAssistant.messages.getString(...)'是否返回帶有正確語言的正確字符串,用於更改組合框項目。 – 2014-09-23 05:02:26
是的它是正確的,如果我把一個System.out.println(loadLogButton.getText());在設置後的LoadLogController的chargeI18nValues函數中,向我顯示該按鈕可以更改文本,但它不會顯示在視圖上。這就像視圖不刷新... – 2014-09-23 08:44:51
哪個版本的API和操作系統?嘗試創建新的演示項目,您可以在單擊按鈕時將標籤的文本設置爲任意值。通過這樣做,將確定特定的prbolem環境或特定的代碼。 – 2014-09-23 09:15:23