私は基本的なMovie Rentalシミュレータアプリケーションを作成しています。現在、TextFieldsとComboBoxの入力を変数に格納する際に問題が発生しています。ほとんどの変数をStringに変換することができましたが、テストするために出力を試してみると、常にnullが返されます。JavaFXのTextFieldsとComboBoxからの入力を格納していますか?
ユーザーがコンボボックスで選択したものをGETして文字列として保存する方法を理解する必要があり、私のメソッドの結果を適切に保存する方法を理解する必要があります。私はこれまでにこの問題に遭遇したことはありません。だから私はそれにどのように取り組むべきか本当に分かりません。次のように私のコードは次のとおりです。
public class RentGameDialogController extends RentalStoreGUIController
implements Initializable{
/** TextField Objects **/
@FXML private TextField nameField, rentedOnField, dueBackField;
/** String for NameField **/
String name, rentedOn, dueBack;
/** Game ComboBox ID's **/
@FXML private ObservableList<GameType> cbGameOptions;
@FXML private ComboBox<GameType> cbGame;
/** Console ComboBox ID's **/
@FXML private ObservableList<PlayerType> cbConsoleOptions;
@FXML private ComboBox<PlayerType> cbConsole;
/** GameType object **/
private GameType game;
/** PlayerType Object **/
private PlayerType console;
/** Button ID's **/
@FXML Button cancel, addToCart;
/** Counter for calculating total **/
int gameCounter;
/** Stage for closing GUI **/
private Stage currentStage;
@Override
public void initialize(URL location, ResourceBundle resources) {
/** Select Console **/
cbConsoleOptions = FXCollections.observableArrayList();
for (PlayerType p : PlayerType.values()) { cbConsoleOptions.addAll(p); }
cbConsole.getItems().addAll(cbConsoleOptions);
/** Select Game **/
cbGameOptions = FXCollections.observableArrayList();
for (GameType g : GameType.values()){ cbGameOptions.addAll(g); }
cbGame.getItems().addAll(cbGameOptions);
}
public String getName(){
name = nameField.getText();
try {
String[] firstLast = name.split(" ");
String firstName = firstLast[0];
String lastName = firstLast[1];
} catch (Exception e){
e.printStackTrace();
}
return name;
}
public void getGame() {
GameType gameChoice = cbGame.getSelectionModel().getSelectedItem();
}
public void getConsole() {
PlayerType player = cbConsole.getSelectionModel().getSelectedItem();
}
public String getRentedOn() throws ParseException {
rentedOn = rentedOnField.getText();
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date rentedOnDate = format.parse(rentedOn);
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(rentedOnDate);
try {
cal.getTime();
} catch (Exception e) {
System.exit(0);
}
return rentedOn;
}
public String getDueBack() throws ParseException {
dueBack = dueBackField.getText();
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date dueBackDate = format.parse(dueBack);
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(dueBackDate);
try {
cal.getTime();
} catch (Exception e) {
System.exit(0);
}
return dueBack;
}
/*************************************
* This is the method to call the other
* String methods so their output can be
* put into my main GUI
*
* Current problem: game.toString() and console.toString() throw an InvocationTargetException
* @return
* @throws ParseException
*************************************/
public String storePurchaseData() throws ParseException {
gameCounter++; //Problem //Problem
String toList = getName() + " " + game.toString() + " " + console.toString() + " " +
getRentedOn() + " " + getDueBack();
return toList; //Returns "null null null"
}
@FXML
public void handleCancelButtonAction (ActionEvent event) {
currentStage = (Stage) cancel.getScene().getWindow();
currentStage.close();
}
@FXML
public void addToCartButton (ActionEvent event) throws ParseException {
System.out.println(storePurchaseData());
currentStage = (Stage) cancel.getScene().getWindow();
currentStage.close();
}}
列挙型ゲームタイプとPlayerType(コンソール選択)のクラス:
public enum PlayerType {
Xbox360("Xbox 360"),
PS4("Playstation 4"),
XBoxOne("Xbox One"),
WiiU("Wii - U"),
PS3("Playstation 3"),
Wii("Nintendo Wii");
private String console;
PlayerType(String console) { this.console = console; }
public String PlayerType() { return console; }
@Override public String toString() { return console; }}
ゲームタイプ:
public enum GameType {
THE_WITCHER("The Witcher 3"),
CALL_OF_DUTY_AW("Call of Duty: Advanced Warfare"),
CALL_DUTY_BLOP3("Call of Duty: Black Ops 3"),
CALL_OF_DUTY_IW("Call of Duty: Infinite Warfare"),
THE_ELDER_SCROLLS("The Elder Scrolls IV: Skyrim");
private String game;
GameType(String game) {
this.game = game;
}
public String GameType() { return game; }
@Override public String toString() { return game; }}
選択された値を返します
としてそれをしたい場合は代わりにそれはそのように
でなければなりません、あなたがして
により結果にアクセスすることができますあなたはどのように知っているだろうそれを文字列に変換するには?私は場所に文字列の値を持っていますが、タイプGameTypeとして返す場合、それは明らかに正しく動作しません。 –
@JarredParr、更新された回答を参照してください。さもなければ、 'GameType' enumに' toString() 'メソッドが実装されています... – Jonah