我制作了一个基本的Movie Rental模拟器应用程序,我目前在将TextFields和我的ComboBox的输入存储到变量时遇到了问题。我设法将大部分变量转换为字符串,但是当我尝试打印输出来测试它时,它总是返回" null。"
我需要基本弄清楚如何获取用户在组合框中所做的选择并将其存储为字符串,我需要弄清楚如何正确存储我的方法的结果。我之前从未遇到过这个问题,所以我不确定如何解决这个问题。我的代码如下:
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();
}}
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; }}
GameType和PlayerType的枚举类(控制台选择):
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; }}
游戏类型:
in
答案 0 :(得分:0)
唯一可以返回所选值的方法是getName()
您正在使用的其他方法不会返回任何内容
public void getGame() //Void return type
{
GameType gameChoice = cbGame.getSelectionModel().getSelectedItem();
}
所有这一切都是创建一个只能在该方法内部访问的GameType
对象。
相反它应该是
public GameType getGame() //Instead of void, the type you are trying to get
{
return cbGame.getSelectionModel().getSelectedItem();
}
这样,您就可以通过
访问结果了GameType selectedGame = getGame();
如果您想将其作为String
使用
public String getGame()
{
return cbGame.getSelectionModel().getSelectedItem().toString();
}