【问题标题】:"String cannot be converted to Component" in JOptionPaneJOptionPane 中的“字符串无法转换为组件”
【发布时间】:2017-06-22 21:35:27
【问题描述】:

在最后一个 JOptionPane 中不断收到“字符串无法转换为组件” 我为糟糕的格式道歉。我知道使用 switch 要容易得多,这只是我的 Java 类的一项任务。欢迎任何和所有建议。谢谢你

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

【问题讨论】:

    标签: java string components joptionpane


    【解决方案1】:

    您的代码与showMethodDialogue(...) 函数的任何方法签名都不匹配。看看JOptionPaneJavadoc

    static void showMessageDialog(Component parentComponent, Object message):打开一个标题为“消息”的信息消息对话框。

    static void showMessageDialog(Component parentComponent, Object message, String title, int messageType): 弹出一个对话框 使用由 messageType 确定的默认图标显示消息 参数。

    static void showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon): 弹出一个对话框 显示一条消息,指定所有参数。

    parentComponent 定义为“确定显示对话框的 Frame;如果为 null,或者如果 parentComponent 没有 Frame,则使用默认 Frame”。

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多