为什么我的价格字符串不会显示在应用程序中

时间:2013-04-15 02:21:37

标签: java swing

所以我正在制作一个简单的小程序,可以在订购时计算披萨的价格。披萨可以是小型,中型或大型,可以额外付2次配料(意大利辣香肠1.25美元和蘑菇1.10美元)。我创建了我的总变量并将成本计入要计算的程序中,但是我的程序中有一个空白,我预计总计是这样。有什么想法吗?

enter image description here

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

3 个答案:

答案 0 :(得分:2)

地球上任何地方都没有名为"$" + Double.toString(total)的字体......

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

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

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

更新了示例

enter image description here

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

旁注。每当您想要更新价格时,您都不需要这样做。在构造函数中设置一次字体...

答案 1 :(得分:2)

除了字体名称无效外,字体大小应大于零:

double size = 0; // hello?

答案 2 :(得分:1)

您应该使用NumberFormat's getCurrencyInstance method,如下面的代码段所示:

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