2

所以我正在制作一个简单的小程序,它会在订购时计算披萨的价格。披萨有小号、中号或大号可供选择,还可以提供 2 种配料,但需额外付费(意大利辣香肠 1.25 美元,蘑菇 1.10 美元)。我创建了我的总变量并将成本放入要计算的程序中,但我的程序在我预期的总数中出现了空白。有任何想法吗?

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;


public class Test extends JFrame
{
    double total = 0;
    String totalPrint="$" + Double.toString(total);

   public Test()
   {  



      sampleField = new JLabel(totalPrint);
      add(sampleField, BorderLayout.CENTER);


      class ChoiceListener implements ActionListener
      {  
         public void actionPerformed(ActionEvent event)
         {  
            setSampleFont();
         }
      }

      listener = new ChoiceListener();

      createControlPanel();
      setSampleFont();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }


   public void createControlPanel()
   {
      JPanel sizeGroupPanel = createCheckBoxes();
      JPanel styleGroupPanel = createRadioButtons();

      JPanel controlPanel = new JPanel();
      controlPanel.setLayout(new GridLayout(2, 1));

      controlPanel.add(sizeGroupPanel);
      controlPanel.add(styleGroupPanel);

      add(controlPanel, BorderLayout.SOUTH);
   }

   public JPanel createCheckBoxes()
   {
      pCheckBox = new JCheckBox("Pepperoni");
      pCheckBox.addActionListener(listener);

      mCheckBox = new JCheckBox("Mushrooms");
      mCheckBox.addActionListener(listener);

      JPanel panel = new JPanel();
      panel.add(pCheckBox);
      panel.add(mCheckBox);
      panel.setBorder
         (new TitledBorder(new EtchedBorder(), "Toppings"));

      return panel;
   }

   public JPanel createRadioButtons()
   {
      smallButton = new JRadioButton("Small");
      smallButton.addActionListener(listener);

      mediumButton = new JRadioButton("Medium");
      mediumButton.addActionListener(listener);

      largeButton = new JRadioButton("Large");
      largeButton.addActionListener(listener);
      largeButton.setSelected(true);

      ButtonGroup group = new ButtonGroup();
      group.add(smallButton);
      group.add(mediumButton);
      group.add(largeButton);

      JPanel panel = new JPanel();
      panel.add(smallButton);
      panel.add(mediumButton);
      panel.add(largeButton);
      panel.setBorder
            (new TitledBorder(new EtchedBorder(), "Size"));

      return panel;
   }

   public void setSampleFont()
   {  

      if (pCheckBox.isSelected()) 
         total = total + 1.25;
      if (mCheckBox.isSelected()) 
         total = total + 1.10;

      double size = 0;
      double style = 0;

      final double SMALL_SIZE = 5.25;
      final double MEDIUM_SIZE = 7.55;
      final double LARGE_SIZE = 9.35;

      if (smallButton.isSelected()) 
         total = total + SMALL_SIZE;
      else if (mediumButton.isSelected()) 
          total = total + MEDIUM_SIZE;
      else if (largeButton.isSelected()) 
          total = total + LARGE_SIZE;

    sampleField.setFont(new Font(totalPrint, (int) style, (int) size));      
      sampleField.repaint();
   }

   private JLabel sampleField;
   private JCheckBox pCheckBox;
   private JCheckBox mCheckBox;
   private JRadioButton smallButton;
   private JRadioButton mediumButton;
   private JRadioButton largeButton;
   private ActionListener listener;

   private static final int FRAME_WIDTH = 300;
   private static final int FRAME_HEIGHT = 400;
}
4

3 回答 3

2

在这个星球上的任何地方都没有一种名为"$" + Double.toString(total)...的字体...

new Font(totalPrint, (int) style, (int) size)

第一个参数应该是您要使用的字体的名称,而不是文本!

在修复此问题时,字体大小0将同时产生空白结果。

更新了示例

在此处输入图像描述

public void setSampleFont() {

    if (pCheckBox.isSelected()) {
        total = total + 1.25;
    }
    if (mCheckBox.isSelected()) {
        total = total + 1.10;
    }

    // !! Supply a positive font size //
    double size = 64;
    // A nice style //
    double style = Font.BOLD;

    final double SMALL_SIZE = 5.25;
    final double MEDIUM_SIZE = 7.55;
    final double LARGE_SIZE = 9.35;

    if (smallButton.isSelected()) {
        total = total + SMALL_SIZE;
    } else if (mediumButton.isSelected()) {
        total = total + MEDIUM_SIZE;
    } else if (largeButton.isSelected()) {
        total = total + LARGE_SIZE;
    }

    // Use the JLabel's default font...
    Font font = UIManager.getFont("Label.font");
    // Derive a new font at the required size and style...
    font = font.deriveFont((int)style, (int)size);

    sampleField.setText(totalPrint);
    sampleField.setFont(font);
//        sampleField.repaint();
}

在旁注中。您无需在每次想要更新价格时都执行此操作。在构造函数中设置一次字体...

于 2013-04-15T02:30:09.600 回答
2

随着字体名称无效,字体大小应大于零才能看到:

double size = 0; // hello?
于 2013-04-15T02:32:24.423 回答
1

您应该使用NumberFormat 的 getCurrencyInstance 方法,如以下代码片段所示:

    Double currencyAmount = new Double(9876543.21);
    Currency currentCurrency = Currency.getInstance(currentLocale);
    NumberFormat currencyFormatter = 
        NumberFormat.getCurrencyInstance(currentLocale);

    System.out.println(
        currentLocale.getDisplayName() + ", " +
        currentCurrency.getDisplayName() + ": " +
        currencyFormatter.format(currencyAmount));
于 2013-04-15T02:39:02.203 回答