如何将newItem设置为JOptionPane?

时间:2015-10-19 21:20:23

标签: java ios swing

public static InventoryItem addNewItem(){

    InventoryItem newItem;
    JOptionPane.showInputDialog(null," Enter new product name.",
                    " by Marquis Watkins", JOptionPane.QUESTION_MESSAGE) 
    JOptionPane.showInputDialog(null," Enter product price." ,
                    " by Marquis Watkins", JOptionPane.QUESTION_MESSAGE);
    JOptionPane.showInputDialog(null,"Enter quantity of product.",
                    " by Marquis Watkins", JOptionPane.QUESTION_MESSAGE);

    return newItem;
}

此方法使用JOptionPane.showInputDialog()

从用户处获得三个输入

然后使用输入的值构造一个新的InventoryItem对象,并返回到该新InventoryItem的调用者和对象引用。

大约10-12行。如何设置newItem以返回我的JOptionPane输入屏幕?

1 个答案:

答案 0 :(得分:1)

如果没有InventoryItem的代码,我们无法知道,但是这样的事情会让你走上正轨。如@Cinnam的评论中所述,您需要存储返回值:

public static InventoryItem addNewItem() {

    String name = JOptionPane.showInputDialog(null," Enter new product name."," by Marquis Watkins", JOptionPane.QUESTION_MESSAGE);
    String price = JOptionPane.showInputDialog(null," Enter product price." ," by Marquis  Watkins", JOptionPane.QUESTION_MESSAGE);
    String quantity = JOptionPane.showInputDialog(null,"Enter quantity of product."," by Marquis Watkins", JOptionPane.QUESTION_MESSAGE);

    return new InventoryItem(name, price, quantity);
}

这里我假设您可以从3个字符串中构造InventoryItem