"String cannot be converted to Component" 在 JOptionPane 中
"String cannot be converted to Component" in JOptionPane
在最后一个 JOptionPane 中继续获取 "String cannot be converted to Component"
对于糟糕的格式,我深表歉意。我知道使用 switch 更容易,这只是我 Java class 的作业。欢迎提出任何建议。谢谢
import javax.swing.JOptionPane;
import java.util.*;
import java.text.*;
import java.lang.String;
public class Project4A
{
public static void main(String[] args)
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
Scanner sc = new Scanner(System.in);
double dcostBagel = 2.00; //Variables
double dcostDonut = 1.50;
double dcostCrois = 3.00;
double dcostLatte = 1.50;
double dcostCoffee = 1.25;
double dcostMilk = 1.00;
double dcostTea = 0.50;
double dfoodChoiceCost;
double dbevChoiceCost;
double dtotDue;
double dtotCost;
int ichoiceFood;
int ichoiceBev;
int inumOrdered;
String choiceFood;
String choiceBev;
String finChoiceFood;
String finChoiceBev;
//Prompts
choiceFood = JOptionPane.showInputDialog("Welcome to BeBe's Best BreakfastnChoose a Breakfast Item:n1: Bagel @ "+formatter.format(dcostBagel) + "n2: Donut @ "+formatter.format(dcostDonut) + "n3: Croissant @ "+formatter.format(dcostCrois));
ichoiceFood = Integer.parseInt(choiceFood);
choiceBev = JOptionPane.showInputDialog("Choose one of the following beverages:nEnter:n1: Latte @ "+formatter.format(dcostLatte)+"n2: Coffee @ "+formatter.format(dcostCoffee)+"n3: Milk @ "+formatter.format(dcostMilk)+"n4: Tea @ "+formatter.format(dcostTea));
ichoiceBev = Integer.parseInt(choiceBev);
//If Elses for Food
if(ichoiceFood == 1)
{
finChoiceFood = "Bagel";
dfoodChoiceCost = dcostBagel;
}
else if(ichoiceFood == 2)
{
finChoiceFood = "Donut";
dfoodChoiceCost = dcostDonut;
}
else
{
finChoiceFood = "Croissant";
dfoodChoiceCost = dcostCrois;
}
//If Elses for Beverages
if(ichoiceBev == 1)
{
finChoiceBev = "Latte";
dbevChoiceCost = dcostLatte;
}
else if(ichoiceBev == 2)
{
finChoiceBev = "Coffee";
dbevChoiceCost = dcostCoffee;
}
else if(ichoiceBev == 3)
{
finChoiceBev = "Milk";
dbevChoiceCost = dcostMilk;
}
else
{
finChoiceBev = "Tea";
dbevChoiceCost = dcostTea;
}
/
//Retreive num ordered
System.out.println("How many items would you like?(1 to 20");
inumOrdered = sc.nextInt();
//Calculations
dtotCost = dbevChoiceCost + dfoodChoiceCost;
dtotDue = dtotCost * inumOrdered;
//Integer.toString(inumOrdered);
//Final Output
JOptionPane.showMessageDialog("Breakfast ordered:n"+finChoiceFood+" @ "+formatter.format(dfoodChoiceCost)+"nnBeverage ordered:n"+finChoiceBev+" @ "+formatter.format(dbevChoiceCost)+"nnTotal cost: "+formatter.format(dtotCost)+"nNumber ordered: "+inumOrdered,"Your Bill");
}
}
您的代码不匹配 showMethodDialogue(...)
函数的任何方法签名。看看 JOptionPane
Javadoc:
static void showMessageDialog(Component parentComponent, Object
message)
: Brings up an information-message dialog titled "Message".
static void showMessageDialog(Component parentComponent, Object
message, String title, int messageType)
: Brings up a dialog that
displays a message using a default icon determined by the messageType
parameter.
static void showMessageDialog(Component parentComponent, Object
message, String title, int messageType, Icon icon)
: Brings up a dialog
displaying a message, specifying all parameters.
parentComponent
定义为"determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used"。
在最后一个 JOptionPane 中继续获取 "String cannot be converted to Component" 对于糟糕的格式,我深表歉意。我知道使用 switch 更容易,这只是我 Java class 的作业。欢迎提出任何建议。谢谢
import javax.swing.JOptionPane;
import java.util.*;
import java.text.*;
import java.lang.String;
public class Project4A
{
public static void main(String[] args)
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
Scanner sc = new Scanner(System.in);
double dcostBagel = 2.00; //Variables
double dcostDonut = 1.50;
double dcostCrois = 3.00;
double dcostLatte = 1.50;
double dcostCoffee = 1.25;
double dcostMilk = 1.00;
double dcostTea = 0.50;
double dfoodChoiceCost;
double dbevChoiceCost;
double dtotDue;
double dtotCost;
int ichoiceFood;
int ichoiceBev;
int inumOrdered;
String choiceFood;
String choiceBev;
String finChoiceFood;
String finChoiceBev;
//Prompts
choiceFood = JOptionPane.showInputDialog("Welcome to BeBe's Best BreakfastnChoose a Breakfast Item:n1: Bagel @ "+formatter.format(dcostBagel) + "n2: Donut @ "+formatter.format(dcostDonut) + "n3: Croissant @ "+formatter.format(dcostCrois));
ichoiceFood = Integer.parseInt(choiceFood);
choiceBev = JOptionPane.showInputDialog("Choose one of the following beverages:nEnter:n1: Latte @ "+formatter.format(dcostLatte)+"n2: Coffee @ "+formatter.format(dcostCoffee)+"n3: Milk @ "+formatter.format(dcostMilk)+"n4: Tea @ "+formatter.format(dcostTea));
ichoiceBev = Integer.parseInt(choiceBev);
//If Elses for Food
if(ichoiceFood == 1)
{
finChoiceFood = "Bagel";
dfoodChoiceCost = dcostBagel;
}
else if(ichoiceFood == 2)
{
finChoiceFood = "Donut";
dfoodChoiceCost = dcostDonut;
}
else
{
finChoiceFood = "Croissant";
dfoodChoiceCost = dcostCrois;
}
//If Elses for Beverages
if(ichoiceBev == 1)
{
finChoiceBev = "Latte";
dbevChoiceCost = dcostLatte;
}
else if(ichoiceBev == 2)
{
finChoiceBev = "Coffee";
dbevChoiceCost = dcostCoffee;
}
else if(ichoiceBev == 3)
{
finChoiceBev = "Milk";
dbevChoiceCost = dcostMilk;
}
else
{
finChoiceBev = "Tea";
dbevChoiceCost = dcostTea;
}
/
//Retreive num ordered
System.out.println("How many items would you like?(1 to 20");
inumOrdered = sc.nextInt();
//Calculations
dtotCost = dbevChoiceCost + dfoodChoiceCost;
dtotDue = dtotCost * inumOrdered;
//Integer.toString(inumOrdered);
//Final Output
JOptionPane.showMessageDialog("Breakfast ordered:n"+finChoiceFood+" @ "+formatter.format(dfoodChoiceCost)+"nnBeverage ordered:n"+finChoiceBev+" @ "+formatter.format(dbevChoiceCost)+"nnTotal cost: "+formatter.format(dtotCost)+"nNumber ordered: "+inumOrdered,"Your Bill");
}
}
您的代码不匹配 showMethodDialogue(...)
函数的任何方法签名。看看 JOptionPane
Javadoc:
static void showMessageDialog(Component parentComponent, Object message)
: Brings up an information-message dialog titled "Message".
static void showMessageDialog(Component parentComponent, Object message, String title, int messageType)
: Brings up a dialog that displays a message using a default icon determined by the messageType parameter.
static void showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)
: Brings up a dialog displaying a message, specifying all parameters.
parentComponent
定义为"determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used"。