My program compiles and runs, but here is my problem. I have a checkbox set up for each item, but I only need it to total the items that are checked and not all of the items. Instead its totalling all items regardless of whether or not they are checked.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.text.*;
public class StudentShopping extends JFrame
{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(550, 400); //Sets size of the window
frame.setTitle("Student Shopping");//Adds title to the GUI
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox ClothingCheckBox = new JCheckBox();
JLabel Clothinglbl = new JLabel("Clothing");
final JTextField ClothingField = new JTextField(3);
ClothingField.setText("0");
JCheckBox BooksCheckBox = new JCheckBox();
JLabel Bookslbl = new JLabel("Books");
final JTextField BooksField = new JTextField(3);
BooksField.setText("0");
JCheckBox SuppliesCheckBox = new JCheckBox();
JLabel Supplieslbl = new JLabel("Supplies");
final JTextField SuppliesField = new JTextField(3);
SuppliesField.setText("0");
JCheckBox MealPlanCheckBox = new JCheckBox();
JLabel MealPlanlbl = new JLabel("MealPlan");
final JTextField MealPlanField = new JTextField(3);
MealPlanField.setText("0");
JCheckBox InternetAccessCheckBox = new JCheckBox();
JLabel InternetAccesslbl = new JLabel("InternetAccess");
final JTextField InternetAccessField = new JTextField(3);
InternetAccessField.setText("0");
final JTextField TotalField = new JTextField(10);
TotalField.setText("0");
JLabel ButtonLabel = new JLabel("Press Button for Total");
JButton button = new JButton("Calculate Total");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6,3));
panel.add(ClothingCheckBox);
panel.add(Clothinglbl);
panel.add(ClothingField);
panel.add(BooksCheckBox);
panel.add(Bookslbl);
panel.add(BooksField);
panel.add(SuppliesCheckBox);
panel.add(Supplieslbl);
panel.add(SuppliesField);
panel.add(MealPlanCheckBox);
panel.add(MealPlanlbl);
panel.add(MealPlanField);
panel.add(InternetAccessCheckBox);
panel.add(InternetAccesslbl);
panel.add(InternetAccessField);
panel.add(ButtonLabel);
panel.add(button);
panel.add(TotalField);
frame.add(panel);
class CalculateListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double Clothing = Double.parseDouble(ClothingField.getText());
double Books = Double.parseDouble(BooksField.getText()) ;
double Supplies = Double.parseDouble(SuppliesField.getText());
double MealPlan = Double.parseDouble(MealPlanField.getText());
double InternetAccess = Double.parseDouble(InternetAccessField.getText());
double Total = (Clothing+Books+Supplies+MealPlan+InternetAccess)*100
DecimalFormat df = new DecimalFormat("$#.00");//creates decimal in currency format
TotalField.setText(df.format(Total)); //
}
}
ActionListener listener = new CalculateListener();
button.addActionListener(listener);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…