java - 在 JOptionPane 中显示数组

标签 java arrays joptionpane

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

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);
}
}

最佳答案

您可以将输出包装在 HTML 中并让 Swing 呈现它...

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 并使用类似的方法,循环遍历每个购物列表并从中创建一个新行。

关于java - 在 JOptionPane 中显示数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13926545/

相关文章:

java - 排列/组合计算器上的 GUI 问题

java - JSONObject 无法转换为 java.lang.String

java - 拆分字符串 ('_' 作为分隔符)

java - 如何使用三个文本字段获取 JOptionPane

javascript - 带有简单数组的基本 ng-repeat

java - 你如何创建一个大数组?

java - 自定义 JOptionPane 覆盖 YES_NO_CANCEL 按钮时获取 "Cancel"操作

java - 实现定义为接口(interface)的泛型类

java - 如何在Java中以编程方式测量系统网络使用情况?

c++ - 返回具有常量成员数的数组