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输入屏幕?

最佳答案

没有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

10-05 19:06