1

我是新来的,这是我的第一篇文章。

我非常需要编写这个非常简单的应用程序,只调用 isSelected() 方法来检查是否选择了 JRadioButton 或 JCheckBox。这意味着我不想遍历 ButtonGroup 并调用它的 getSelection() 方法。

我的代码也与 Tony Gaddis 编写的教科书中的代码相同,我目前正在从中学习:从 Java 开始从控制结构到对象第 4 版

这是一个逻辑问题,因为应用程序编译和运行没有错误。

这是发生了什么:

我有四个类:BagelsPanel、CoffeePanel、ToppingsPanel 和 GUI 类 BagelApp -除了扩展 JFrame 的 BagelApp 类之外,所有这些都扩展了 JPanel。

该应用程序的目的是让用户制作咖啡、百吉饼和配料选择,并返回他们所有选择的总价格。问题是它不断返回 0.00 美元。

我的怀疑是,由于某种原因, isSelected() 没有识别出某些东西被选中。

我将在下面发布涉及这些问题的 BagelsPanel 和 GUI 类代码:

public double calcBagel() {

    double total = 0.00;

    if(button1.isSelected())
        total = PLAIN;
    else if(button2.isSelected()) 
        total = WHOLE_WHEAT;
    else if(button3.isSelected()) 
        total = CINNA_RAISON;
    else if(button4.isSelected()) 
        total = EVERYTHING;

    return total;
}

以上是 BagelsPanel 类中的 calcBagel() 方法,该方法调用 isSelected() 方法来检查选择了哪个 JRadioButton,然后其价格分配给总计。下面是 GUI 类:

public void buildPanel() {

    panel = new JPanel();
    calc = new JButton("Calculate");
    calc.addActionListener(new ButtonListener());
    panel.add(calc);
}
private class ButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        double subtotal = 0.0;
        double total = 0.0;
        bagels = new BagelPanel();
        coffee = new CoffeePanel();
        toppings = new ToppingsPanel();

        subtotal = bagels.calcBagel() + coffee.calcCoffee() +
                    toppings.calcToppings();
        double tax = subtotal * TAX;
        total = subtotal + tax;

        DecimalFormat dollar = new DecimalFormat("$0.00");

        JOptionPane.showMessageDialog(null, "Your total is: " 
                    + dollar.format(total));    
    }
}

这里有一些见解:如果我在 BagelsPanel 类中的 calcBagel() 方法中将双变量总计更改为 1.0,然后运行应用程序并单击 JRadioButton“计算”,它会准确地支撑一个 JOptionPane,告诉我我的总计是$1.06(最终双变量 TAX 设置为 0.06)。

我真的很感激我能得到的任何帮助。我仍然是 Java 的初学者,不太明白为什么我的逻辑不正确。我很尴尬,这可能是非常微不足道的,但我已经检查了这本书并且代码是相同的。有什么建议么?

4

2 回答 2

2

您的问题就在这里,在您的actionPerformed方法中:

    double total = 0.0;
    bagels = new BagelPanel();
    coffee = new CoffeePanel();
    toppings = new ToppingsPanel();

您不是在显示的面板上调用方法,而是calcBagel(), calcCoffee(), calcToppings()在无处创建新面板并在它们上调用“计算方法”。当然,它们是用户在 UI 中操作的对象的不同对象。您应该保留对最初添加到 GUI 的面板的引用,并在这些面板上调用“计算方法”,而不是在新创建的对象上调用。

PS:您的代码确实混合了模型和视图。

于 2012-05-06T08:17:23.797 回答
1

如果没有真正的SSCCE,这有点难以回答,但我会这样做:使用我最喜欢的 IDE(在我的例子中是Netbeans),我会在开始时设置一个断点calcBagel()并逐行执行代码使用调试器确保设置了变量。

或者,您可以执行以下操作:

public double calcBagel() {
    System.out.println("In calcBagel()");
    double total = 0.00;

    if(button1.isSelected()) {
        System.out.println("button1 is selected! Setting total to " + PLAIN);
        total = PLAIN;
    }
    else if(button2.isSelected())  {
        System.out.println("button2 is selected! Setting total to " + WHOLE_WHEAT);
        total = WHOLE_WHEAT;
    }
    else if(button3.isSelected())  {
        System.out.println("button3 is selected! Setting total to " + CINNA_RAISON);
        total = CINNA_RAISON;
    }
    else if(button4.isSelected())  {
        System.out.println("button4 is selected! Setting total to " + EVERYTHING);
        total = EVERYTHING;
    } else {
        System.out.println("No buttons were selected!");
    }
    System.out.println("total = " + total);
    return total;
}

这也是了解您的方法发生了什么的好方法。您的问题也很容易根本不在此方法中。但这将是一种找出答案的方法。

于 2012-05-06T06:45:23.063 回答