2012-12-18 43 views
0

我正在努力弄清楚如何在JOptionPane中显示我的数组内容。该阵列包括一个项目,价格,数量和优先级。例如,我希望它看起来像这样: “苹果2.99 2 1” “谷物3.99 3 2” 我目前有阵列输出到控制台,因为我无法正确显示阵列。以下是我现在所拥有的内容:我非常感谢所有帮助!在JOptionPane中显示数组

import java.text.NumberFormat; 

import javax.swing.JOptionPane; 

public class ShopList { 


public static void main(String[] args) 
{ 


    String enterName = JOptionPane.showInputDialog(null, "Username: "); 


    for (int i = 0; i < 2; i++){ 
     index = (int) (25 * Math.random()); 
     String[] options = {"Apples", "Applesauce", "Cereal", "Exit"}; 
     String input = (String)JOptionPane.showInputDialog(null, 
       "Select an Item", 
       "Welcome " + enterName + "!",JOptionPane.QUESTION_MESSAGE,null,options,"Apples"); 

     String itemPrice = JOptionPane.showInputDialog("Enter Price"); 
     double itemp = Double.parseDouble(itemPrice); 



     String[] itemQuantity = {"1", "2", "3", "4", "5"}; 
     String itemq = (String)JOptionPane.showInputDialog(null,"Enter Quantity", "Welcome", JOptionPane.QUESTION_MESSAGE, null, itemQuantity, "1"); 

     String itemsPriority = JOptionPane.showInputDialog("Enter Priority"); 
     int itempry = Integer.parseInt(itemsPriority); 

     ShoppingList shoppingList = new ShoppingList(input,itemp, itemq,  itempry); 
     shoppingList.show(); 


} 

} 
class ShoppingList 
{ 
String itemNames; 
double itemPrice; 
String itemQuantity; 
int itemsPriority; 

public ShoppingList() 
{ 
} 
public ShoppingList (String name, double price, String quantity, int priority) 
{ 
    itemNames = name; 
    itemPrice = price; 
    itemQuantity = quantity; 
    itemsPriority = priority; 

} 
public void setitemNames(String name) 
{ 
    itemNames = name; 
} 
public String getitemNames() 
{ 
    return itemNames; 
} 
public void setitemPrice(double price) 
{ 
    itemPrice = price; 
} 
public double getitemPrice() 
{ 
    return itemPrice; 
} 
/*public void setitemQuantity(int quantity) 
{ 
    itemQuantity = quantity; 
} 
public int getitemQuantity() 
{ 
    return itemQuantity; 
}*/ 
public void setitemsPriority(int priority) 
{ 
    itemsPriority = priority; 
} 
public int getitemsPriority() 
{ 
    return itemsPriority; 
} 

public void show() 
{ 
    System.out.println(itemNames); 
    System.out.println(itemPrice); 
    System.out.println(itemQuantity); 
    System.out.println(itemsPriority); 
} 
} 
+0

你想如何显示它? –

+0

你想要显示的数组在哪里? –

+0

我想要显示shoppingList数组,如果这是有道理的? – user1785556

回答

1
import javax.swing.JOptionPane; 

public class ShopList { 

    private static String allItems = ""; 

    public static void main(String[] args) { 

     ShoppingList shoppingList = null; 
     String enterName = JOptionPane.showInputDialog(null, "Username: "); 

     for (int i = 0; i < 2; i++) { 
      int index = (int) (25 * Math.random()); 
      String[] options = { "Apples", "Applesauce", "Cereal", "Exit" }; 
      String input = (String) JOptionPane.showInputDialog(null, 
        "Select an Item", "Welcome " + enterName + "!", 
        JOptionPane.QUESTION_MESSAGE, null, options, "Apples"); 

      String itemPrice = JOptionPane.showInputDialog("Enter Price"); 
      double itemp = Double.parseDouble(itemPrice); 

      String[] itemQuantity = { "1", "2", "3", "4", "5" }; 
      String itemq = (String) JOptionPane.showInputDialog(null, 
        "Enter Quantity", "Welcome", 
        JOptionPane.QUESTION_MESSAGE, null, itemQuantity, "1"); 

      String itemsPriority = JOptionPane 
        .showInputDialog("Enter Priority"); 
      int itempry = Integer.parseInt(itemsPriority); 

      shoppingList = new ShoppingList(input, itemp, itemq, itempry); 
//   shoppingList.show(); 

     } 

     shoppingList.show(); 

    } 

    public static class ShoppingList { 
     String itemNames; 
     double itemPrice; 
     String itemQuantity; 
     int itemsPriority; 

     public ShoppingList(String name, double price, String quantity,int priority) { 
      itemNames = name; 
      itemPrice = price; 
      itemQuantity = quantity; 
      itemsPriority = priority; 
      allItems = allItems + "Name: " + itemNames + " Price: " + itemPrice + " Quantity: " + itemQuantity + " Priority: " + itemsPriority + "\n"; 

     } 

     public void setitemNames(String name) { 
      itemNames = name; 
     } 

     public String getitemNames() { 
      return itemNames; 
     } 

     public void setitemPrice(double price) { 
      itemPrice = price; 
     } 

     public double getitemPrice() { 
      return itemPrice; 
     } 

     /* 
     * public void setitemQuantity(int quantity) { itemQuantity = quantity; 
     * } public int getitemQuantity() { return itemQuantity; } 
     */ 
     public void setitemsPriority(int priority) { 
      itemsPriority = priority; 
     } 

     public int getitemsPriority() { 
      return itemsPriority; 
     } 

     public void show() { 
//   System.out.println(itemNames); 
//   System.out.println(itemPrice); 
//   System.out.println(itemQuantity); 
//   System.out.println(itemsPriority); 
      JOptionPane.showMessageDialog(null, allItems); 
     } 
    } 
} 
2

你可以换行输出HTML,让摇摆使其...

enter image description here

public void show() { 
    StringBuilder sb = new StringBuilder(64); 
    sb.append("<html><table><tr><td>Item</td><td>Price</td><td>Quantity</td><td></td>Priority</tr>"); 
    sb.append("<tr>"); 
    sb.append("<td>").append(itemNames).append("</td>"); 
    sb.append("<td>").append(itemPrice).append("</td>"); 
    sb.append("<td>").append(itemQuantity).append("</td>"); 
    sb.append("<td>").append(itemsPriority).append("</td>"); 
    sb.append("</tr></table></html>"); 

    JOptionPane.showMessageDialog(null, sb); 
} 

现在,我想创建一个有能力采取一个方法或更多ShoppingLists并使用类似的方法,遍历每个购物清单并从中创建一个新行。

0

使用JOption窗格的消息对话框ü可以告诉你想要的例如模式

公共无效秀() {

JOptionPane.showMessageDialog(null, itemNames + " " + itemPrice + " " + itemQuantity + " " + itemsPriority); 

}

0

试试这个代码:

public static void main(String[] args) 
{ 
    ShoppingList shoppingList = null; 
    StringBuffer output = new StringBuffer() ; 
    String enterName = JOptionPane.showInputDialog(null, "Username: "); 


    . 
    . 
     // shoppingList = new ShoppingList(input,itemp, itemq,itempry); 
     output.append("\""+input + " " + itemp +" " + itemq + " " + itempry+ "\"\n") ; 
     System.out.println(output.toString()); 
} 

    JOptionPane.showMessageDialog(null, output.toString()); 

} 

enter image description here

+0

谢谢,我给了这个尝试,它是成功的!我现在正在尝试做同样的事情,但随着项目优先显示,所以在你的谷物食品中会出现在苹果之前。我创建了一个arrayList ArrayList cart = new ArrayList ();举行所有的输入和进行收集排序,但我无法使其工作。 – user1785556