所以我正在制作一个简单的小程序,它会在订购时计算披萨的价格。披萨有小号、中号或大号可供选择,还可以提供 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;
}