从存储的值中减去JTextField中的输入数量

时间:2014-06-24 18:35:59

标签: java swing decrement

我正在尝试创建一个股票系统,当用户输入他们想要的项目数时,从可用总数中减去。我该怎么做?我是初学者,所以我不太确定

对于我的下面的代码,试图获取沙发的输入并减去总数

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import java.text.DecimalFormat;
import java.util.Arrays;

/**
   RoutinePanel class

*/

public class RoutinePanel extends JPanel
{
   // Named constants for charges
       private final double SOFA_CHARGE = 599.99;
       private final double COMPUTER_DESK_CHARGE = 129.99;
       private final double COFFEE_TABLE_CHARGE = 40.0;
       private final double ARMCHAIR_CHARGE = 229.99;
       private final double TV_STAND_CHARGE = 37.0;
       private final double CUSHION_CHARGE = 8.0;
       private final double BED_CHARGE = 145.0;
       private final double MATRESS_CHARGE = 299.0;
       private final double DUVET_CHARGE = 24.99;
       private final double PILLOW_CHARGE = 9.99;


           int SofaTotal = 999;
       TextField ComputerDesk;       // Check box for lube job
       JTextField Armchair; // Check box for radiator flush
       JTextField CoffeeTable;    // Check box for transmission flush
       JTextField TvStand;    // Check box for inspection
       JTextField Cushion;       // Check box for muffler replacement
       JTextField Bed;  // Check box for tire rotation
       JTextField Matress ;
       JTextField Duvet;
       JTextField Pillow;
       JLabel SofaLbl;
       JTextField Sofa;
       private JLabel ArmchairLbl;
       private JLabel ComputerDeskLbl;
       private JLabel CoffeeTableLbl;
       private JLabel TvStandLbl;
       private JLabel CushionLbl;
       private JLabel BedLbl;
       private JLabel MatressLbl;
       private JLabel DuvetLbl;
       private JLabel PillowLbl;
       private Color Colour;
       private JButton b;


   /**
      Constructor
   */

   public RoutinePanel()
   {
      // Create a DecimalFormat object.
      DecimalFormat dollar = new DecimalFormat("#,##0.00");

       Sofa = new JTextField("0");

      SofaLbl  = new JLabel("Sofa");
      ArmchairLbl = new JLabel("Armchair");
      Armchair = new JTextField("0");
      ComputerDesk = new TextField("0");
      ComputerDeskLbl = new JLabel("Computer Desk");
      CoffeeTable = new JTextField("0");
      CoffeeTableLbl = new JLabel("Coffee Table");
      TvStand = new JTextField ("0");
      TvStandLbl = new JLabel ("TV Stand");
      Cushion = new JTextField ("0");
      CushionLbl = new JLabel ("Cushion");
      Bed = new JTextField ("0");
      BedLbl = new JLabel("0");
      Matress = new JTextField("0");
      MatressLbl = new JLabel("Matress");
      Duvet = new JTextField("0");
      DuvetLbl = new JLabel("Duvet");
      Pillow = new JTextField("0");
      PillowLbl = new JLabel("Pillow"); 

      b = new JButton("Choose a Colour");
      b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event)
      { Colour = JColorChooser.showDialog(null, "Pick your Colour", Colour);
    if (Colour==null)
        Colour=(Colour.WHITE);

      }
      }

      );




      // Create a GridLayout manager.
      setLayout(new GridLayout(18, 3));

      // Create a border.
      setBorder(BorderFactory.createTitledBorder("Routine Services"));


      add(SofaLbl);
      add(Sofa);
      add(b);
      add(ArmchairLbl);
      add(Armchair);
      add(ComputerDeskLbl);
      add(ComputerDesk);
      add(CoffeeTableLbl);
      add(CoffeeTable);
      add(TvStandLbl);
      add(TvStand);
      add(CushionLbl);
      add(Cushion);
      add(BedLbl);
      add(Bed);
      add(MatressLbl);
      add(Matress);
      add(DuvetLbl);
      add(Duvet);
      add(PillowLbl);
      add(Pillow);







   }

1 个答案:

答案 0 :(得分:1)

这很简单。你只需要从整个沙发中减去用户要求的沙发并将其显示在沙发标签上!

  int sofaRequested=Integer.parseInt(Sofa.getText());
  int sofaLeft=SofaTotal-sofaRequested;
  SofaLbl.setText("The total number of Sofa left in stock is "+sofaLeft);