问题
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);
}
}
回答1:
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.
回答2:
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);
}
}
}
回答3:
Using message Dialog of JOption pane u can show the pattern you want for eg
public void show() {
JOptionPane.showMessageDialog(null, itemNames + " " + itemPrice + " " + itemQuantity + " " + itemsPriority);
}
回答4:
Try this code:
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());
}
来源:https://stackoverflow.com/questions/13926545/display-array-in-joptionpane