そこで、注文時にピザの価格を計算する簡単な小さなプログラムを作成しています。ピザは小、中、大から選べ、追加料金で 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;
}