我的程序编译并运行,但这是我的问题。我为每个项目设置了一个复选框,但我只需要它来检查已检查的项目而不是所有项目。相反,它总计所有项目,无论它们是否被检查。
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);
}
}
答案 0 :(得分:9)
因为您从不检查选中的复选框状态。它应该是这样的:
double total = 0;
if(ClothingCheckBox.isSelected() && !ClothingField.getText().isEmpty()) {
total += Double.parseDouble(ClothingField.getText());
}
if(BooksCheckBox.isSelected() && !BooksField.getText().isEmpty()) {
total += Double.parseDouble(BooksField.getText());
}
if(SuppliesCheckBox.isSelected() && !SuppliesField.getText().isEmpty()){
total += Double.parseDouble(SuppliesField.getText());
}
if(MealPlanCheckBox.isSelected() && !MealPlanField.getText().isEmpty()){
total += Double.parseDouble(MealPlanField.getText());
}
if(InternetAccessCheckBox.isSelected() && !InternetAccessField.getText().isEmpty()){
total += Double.parseDouble(InternetAccessField.getText());
}
total = total * 100;
DecimalFormat df = new DecimalFormat("$#.00");
TotalField.setText(df.format(total));
我想建议你的事情: