显示阵列中的JOptionPane [英] Display array in JOptionPane
问题描述
我竭力要弄清楚如何显示我的数组的内容的JOptionPane。该阵列包括一个项目,价格,数量和priority.For比如我希望它看起来是这样的:
苹果2.99 2 1
谷物3.99 3 2
我现在有数组输出到控制台,因为我一直没能'显示'数组正确。下面是我到现在为止:我AP preciate所有和任何帮助
进口java.text.NumberFormat中;进口javax.swing.JOptionPane中;公共类ShopList {
公共静态无效的主要(字串[] args)
{
字符串enterName = JOptionPane.showInputDialog(NULL,用户名:);
对(INT I = 0; I&2;我++){
指数=(int)的(25 *的Math.random());
的String []选项= {苹果,苹果酱,谷物,退出};
字符串输入=(字符串)JOptionPane.showInputDialog(NULL,
选择一个项目,
欢迎+ enterName +,JOptionPane.QUESTION_MESSAGE,空,选择苹果)!; 字符串ITEMPRICE = JOptionPane.showInputDialog(输入价格);
双ITEMP = Double.parseDouble(ITEMPRICE); 串[] itemQuantity = {1,2,3,4,5};
字符串itemq =(字符串)JOptionPane.showInputDialog(NULL,输入数量,欢迎光临,JOptionPane.QUESTION_MESSAGE,空,itemQuantity,1); 串itemsPriority = JOptionPane.showInputDialog(输入优先级);
INT itempry =的Integer.parseInt(itemsPriority); ShoppingList shoppingList =新ShoppingList(输入,ITEMP,itemq,itempry);
shoppingList.show();
}}
类ShoppingList
{
串itemNames的;
双ITEMPRICE;
串itemQuantity;
INT itemsPriority;公共ShoppingList()
{
}
公共ShoppingList(字符串名称,双击价格,数量的字符串,诠释优先权)
{
如果itemNames =名称;
ITEMPRICE =价格;
itemQuantity =数量;
itemsPriority =优先;}
公共无效setitemNames(字符串名称)
{
如果itemNames =名称;
}
公共字符串getitemNames()
{
返回itemNames的;
}
公共无效setitemPrice(双价)
{
ITEMPRICE =价格;
}
公共双getitemPrice()
{
返回ITEMPRICE;
}
/ *公共无效setitemQuantity(INT数量)
{
itemQuantity =数量;
}
公众诠释getitemQuantity()
{
返回itemQuantity;
} * /
公共无效setitemsPriority(INT优先)
{
itemsPriority =优先;
}
公众诠释getitemsPriority()
{
返回itemsPriority;
}公共无效显示()
{
的System.out.println(itemNames的);
的System.out.println(ITEMPRICE);
的System.out.println(itemQuantity);
的System.out.println(itemsPriority);
}
}
您可以包装输出HTML和秋千让使其...
公共无效显示(){
StringBuilder的SB =新的StringBuilder(64);
sb.append(\"<html><table><tr><td>Item</td><td>Price</td><td>Quantity</td><td></td>Priority</tr>\");
sb.append(&所述; TR&gt;中);
sb.append(&LT; TD&gt;中)。追加(itemNames中).append(&LT; / TD&gt;中);
sb.append(&LT; TD&gt;中)。追加(ITEMPRICE).append(&LT; / TD&gt;中);
sb.append(&LT; TD&gt;中)。追加(itemQuantity).append(&LT; / TD&gt;中);
sb.append(&LT; TD&gt;中)。追加(itemsPriority).append(&LT; / TD&gt;中);
sb.append(&LT; / TR&GT;&LT; /表&gt;&LT; / HTML&gt;中); JOptionPane.showMessageDialog(NULL,某人);
}
现在,我想创造一种能够以一个或多个 ShoppingLists
,并使用类似的方法,遍历每个购物列表的方法,并从它创建一个新的行
I'm struggling to figure out how to show the contents of my array in a JOptionPane. The array includes an item, price, quantity and priority.For example I'd want it to look like this: "Apples 2.99 2 1" "Cereal 3.99 3 2" I currently have the array outputting to the console because I haven't been able to 'display' the array correctly. Here's what I have as of now: I appreciate all and any help!
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);
}
}
You could wrap the output in HTML and let Swing render it...
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);
}
Now, I would create a method that was capable of taking one or more ShoppingLists
and using a similar method, loop through each shopping list and create a new row from it.
这篇关于显示阵列中的JOptionPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!